All files / src/kernel ColumnSorters.tsx

87.23% Statements 41/47
67.3% Branches 35/52
85.71% Functions 6/7
87.23% Lines 41/47

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100            66x                                 24x 4x   20x 4x   16x 4x   12x         66x 31x 19x 15x 15x 15x 15x 6x   9x 3x 6x 3x   3x     12x 9x 9x 9x 9x 9x 6x   3x 1x 2x 1x   1x       3x     66x                               66x 11x 6x 6x 6x 6x        
import { CompareFn } from "antd/es/table/interface";
import { RecordProps } from "@props/RecordProps";
import { isEnumColumn, isObjectType } from "@utils/ColumnsUtils";
import { SorterResult } from 'antd/lib/table/interface';
import { TableMetaProps } from "@props/RecordProps";
 
const DefaultSortTypes = [
  "decimal",
  "string",
  "id",
  "integer",
  "text",
  "percentage",
  "currency",
  "httpMethod",
  "date",
  "datetime",
  "boolean",
  "df_single",
  "int",
];
 
function compareIfNull(valOne: unknown, valTwo: unknown): number {
  if (valOne == null && valTwo == null) {
    return 0;
  }
  if (valOne == null && valTwo != null) {
    return -1;
  }
  if (valOne != null && valTwo == null) {
    return 1;
  }
  return NaN;
}
 
export type Sorter = boolean | CompareFn<RecordProps> | { compare: CompareFn<RecordProps>; multiple: number; } | undefined;
 
export const getSorter = (type: string, key: string): Sorter => {
  if (DefaultSortTypes.includes(type) || isEnumColumn(type)) {
    return (a: RecordProps, b: RecordProps) => {
      const valOne = a[key];
      const valTwo = b[key];
      const c = compareIfNull(valOne, valTwo);
      if (!isNaN(c)) {
        return c;
      }
      if (valOne > valTwo) {
        return 1;
      } else if (valOne < valTwo) {
        return -1;
      } else {
        return 0;
      }
    };
  } else if (isObjectType(type)) {
    return (a: RecordProps, b: RecordProps) => {
      const valOne = a[key];
      const valTwo = b[key];
      const c = compareIfNull(valOne, valTwo);
      if (!isNaN(c)) {
        return c;
      }
      if (valTwo != null && valOne.id > valTwo.id) {
        return 1;
      } else if (valTwo != null && valOne.id < valTwo.id) {
        return -1;
      } else {
        return 0;
      }
    };
  }
  return false;
};
 
export const isSorterChange = (newSorter: SorterResult<RecordProps>, currentSorter?: SorterResult<RecordProps>): boolean => {
  const newSortIsEmpty = (Object.entries(newSorter).length === 0 || newSorter == null);
  if ((currentSorter == null && !newSortIsEmpty) || (currentSorter != null && newSortIsEmpty)) {
    return true;
  } else if (currentSorter == null && newSortIsEmpty) {
    return false;
  } else {
    return !(
      currentSorter?.column === newSorter?.column &&
        currentSorter?.columnKey === newSorter?.columnKey &&
        currentSorter?.field === newSorter?.field &&
        currentSorter?.order === newSorter?.order
    );
  }
};
 
export const wrapColumnSorter =(columns: Array<TableMetaProps>): void => {
  columns.forEach(column => {
    const { key, type } = column;
    const sorter = getSorter(type, key);
    Eif (sorter != null) {
      column["sorter"] = sorter;
    }
  });
};