import { AnyArray, TKeyofArrayElement } from '../../types'; import { KeyOption, matchSorter } from 'match-sorter'; export const search = ({ data, searchBy, search, }: { data: TArray; searchBy: Array>; search: string; }) => { // Avoid errors stemming from calling array methods on non-arrays. if (!Array.isArray(data)) { return []; } // Don't filter when not needed. if (!search || typeof search !== 'string' || !data?.length) { return data; } // Split words const terms = search?.split(' '); if (!terms) { return data; } // Fuzzy search return terms.reduceRight( (results, term) => matchSorter(results, term, { keys: searchBy as ReadonlyArray>>, }), data, ); };