import { rankItem, rankings, RankingInfo } from '@tanstack/match-sorter-utils'; import { filterFns, type Row } from '@tanstack/react-table'; const fuzzy = = {}>( row: Row, columnId: string, filterValue: string | number, addMeta: (item: RankingInfo) => void, ) => { const itemRank = rankItem(row.getValue(columnId), filterValue as string, { threshold: rankings.MATCHES, }); addMeta(itemRank); return itemRank.passed; }; fuzzy.autoRemove = (val: any) => !val; const contains = = {}>( row: Row, id: string, filterValue: string | number, ) => row .getValue(id) .toString() .toLowerCase() .trim() .includes(filterValue.toString().toLowerCase().trim()); contains.autoRemove = (val: any) => !val; const startsWith = = {}>( row: Row, id: string, filterValue: string | number, ) => row .getValue(id) .toString() .toLowerCase() .trim() .startsWith(filterValue.toString().toLowerCase().trim()); startsWith.autoRemove = (val: any) => !val; const endsWith = = {}>( row: Row, id: string, filterValue: string | number, ) => row .getValue(id) .toString() .toLowerCase() .trim() .endsWith(filterValue.toString().toLowerCase().trim()); endsWith.autoRemove = (val: any) => !val; const equals = = {}>( row: Row, id: string, filterValue: string | number, ) => row.getValue(id).toString().toLowerCase().trim() === filterValue.toString().toLowerCase().trim(); equals.autoRemove = (val: any) => !val; const notEquals = = {}>( row: Row, id: string, filterValue: string | number, ) => row.getValue(id).toString().toLowerCase().trim() !== filterValue.toString().toLowerCase().trim(); notEquals.autoRemove = (val: any) => !val; const greaterThan = = {}>( row: Row, id: string, filterValue: string | number, ) => !isNaN(+filterValue) && !isNaN(+row.getValue(id)) ? +row.getValue(id) > +filterValue : row.getValue(id).toString().toLowerCase().trim() > filterValue.toString().toLowerCase().trim(); greaterThan.autoRemove = (val: any) => !val; const greaterThanOrEqualTo = = {}>( row: Row, id: string, filterValue: string | number, ) => equals(row, id, filterValue) || greaterThan(row, id, filterValue); greaterThanOrEqualTo.autoRemove = (val: any) => !val; const lessThan = = {}>( row: Row, id: string, filterValue: string | number, ) => !isNaN(+filterValue) && !isNaN(+row.getValue(id)) ? +row.getValue(id) < +filterValue : row.getValue(id).toString().toLowerCase().trim() < filterValue.toString().toLowerCase().trim(); lessThan.autoRemove = (val: any) => !val; const lessThanOrEqualTo = = {}>( row: Row, id: string, filterValue: string | number, ) => equals(row, id, filterValue) || lessThan(row, id, filterValue); lessThanOrEqualTo.autoRemove = (val: any) => !val; const between = = {}>( row: Row, id: string, filterValues: [string | number, string | number], ) => ((['', undefined] as any[]).includes(filterValues[0]) || greaterThan(row, id, filterValues[0])) && ((!isNaN(+filterValues[0]) && !isNaN(+filterValues[1]) && +filterValues[0] > +filterValues[1]) || (['', undefined] as any[]).includes(filterValues[1]) || lessThan(row, id, filterValues[1])); between.autoRemove = (val: any) => !val; const betweenInclusive = = {}>( row: Row, id: string, filterValues: [string | number, string | number], ) => ((['', undefined] as any[]).includes(filterValues[0]) || greaterThanOrEqualTo(row, id, filterValues[0])) && ((!isNaN(+filterValues[0]) && !isNaN(+filterValues[1]) && +filterValues[0] > +filterValues[1]) || (['', undefined] as any[]).includes(filterValues[1]) || lessThanOrEqualTo(row, id, filterValues[1])); betweenInclusive.autoRemove = (val: any) => !val; const empty = = {}>( row: Row, id: string, _filterValue: string | number, ) => !row.getValue(id).toString().trim(); empty.autoRemove = (val: any) => !val; const notEmpty = = {}>( row: Row, id: string, _filterValue: string | number, ) => !!row.getValue(id).toString().trim(); notEmpty.autoRemove = (val: any) => !val; export const MRT_FilterFns = { ...filterFns, between, betweenInclusive, contains, empty, endsWith, equals, fuzzy, greaterThan, greaterThanOrEqualTo, lessThan, lessThanOrEqualTo, notEmpty, notEquals, startsWith, };