All files / src/components/EditableTableV2 functions.ts

100% Statements 24/24
88% Branches 22/25
100% Functions 10/10
100% Lines 21/21

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                        20x 29x 29x   29x                     29x     70x   20x 19x 19x   19x           20x       11x       8x       1x       1x       1x       102x 102x   102x   1x       20x  
import { rankItem, rankings } from '@tanstack/match-sorter-utils';
import {
  constructFilterFn,
  filterFn_greaterThanOrEqualTo,
  filterFn_lessThan,
  filterFn_lessThanOrEqualTo,
  filterFns,
  sortFns,
} from '@tanstack/react-table';
 
type ComparisonValue = string | number | null | undefined;
 
const compare = (value: ComparisonValue, bound: ComparisonValue) => {
  const numericValue = Number(value);
  const numericBound = Number(bound);
  const [left, right] =
    !Number.isNaN(numericValue) && !Number.isNaN(numericBound)
      ? [numericValue, numericBound]
      : [
          String(value ?? '')
            .toLowerCase()
            .trim(),
          String(bound ?? '')
            .toLowerCase()
            .trim(),
        ];
 
  return left > right ? 1 : left < right ? -1 : 0;
};
 
const isOpen = (bound: unknown) => bound === '' || bound === undefined || bound === null;
 
const between = (value: ComparisonValue, bounds: unknown, inclusive: boolean) => {
  const [min, max] = Array.isArray(bounds) ? bounds : [];
  const reversed = !isOpen(min) && !isOpen(max) && Number(min) > Number(max);
 
  return (
    (isOpen(min) || (inclusive ? compare(value, min) >= 0 : compare(value, min) > 0)) &&
    (isOpen(max) || reversed || (inclusive ? compare(value, max) <= 0 : compare(value, max) < 0))
  );
};
 
export const namedFilterFns = {
  ...filterFns,
  between: constructFilterFn({
    ...filterFns.between,
    filter: (value, bounds) => between(value, bounds, false),
  }),
  betweenInclusive: constructFilterFn({
    ...filterFns.betweenInclusive,
    filter: (value, bounds) => between(value, bounds, true),
  }),
  greaterThanOrEqualTo: constructFilterFn({
    ...filterFn_greaterThanOrEqualTo,
    filter: (value, bound) => compare(value, bound) >= 0,
  }),
  lessThan: constructFilterFn({
    ...filterFn_lessThan,
    filter: (value, bound) => compare(value, bound) < 0,
  }),
  lessThanOrEqualTo: constructFilterFn({
    ...filterFn_lessThanOrEqualTo,
    filter: (value, bound) => compare(value, bound) <= 0,
  }),
  fuzzy: constructFilterFn({
    filter: (value, filterValue, _row, _columnId, addMeta) => {
      const rank = rankItem(value, String(filterValue), { threshold: rankings.MATCHES });
      addMeta?.(rank);
 
      return rank.passed;
    },
    autoRemove: (value) => !value,
  }),
};
 
export const namedSortFns = sortFns;