import { test, TestOptions } from "./test"; import { match } from "./match"; export type ItemIterator = (source: TItem) => string; export interface FilterOptions extends TestOptions { iterator: ItemIterator; } export interface SortOptions extends TestOptions { iterator: ItemIterator; } type FilterIterator = (item: TItem) => boolean; type SortIterator = (leftItem: TItem, rightItem: TItem) => number; /** * This array helper can be used as an `Array.prototype.filter` callback as it * will return true or false when passing it a source string. */ export function filter( query: string, options: FilterOptions ): FilterIterator { return function (item) { const source = options.iterator(item); return test(query, source, options); }; } /** * This array helper can be used as an `Array.prototype.sort` callback as it * will return `-1`/`0`/`1` when passing it two source strings. */ export function sort( query: string, options: SortOptions ): SortIterator { const cacheMap: Map = new Map(); return (leftItem, rightItem) => { const leftSource = options.iterator(leftItem); const rightSource = options.iterator(rightItem); const cachedLeftMatch = cacheMap.get(leftSource); const cachedRightMatch = cacheMap.get(rightSource); const leftScore = cachedLeftMatch ? cachedLeftMatch : match(query, leftSource, { withScore: true, caseSensitive: options.caseSensitive, }).score; const rightScore = cachedRightMatch ? cachedRightMatch : match(query, rightSource, { withScore: true, caseSensitive: options.caseSensitive, }).score; if (!cacheMap.has(leftSource)) { cacheMap.set(leftSource, leftScore); } if (!cacheMap.has(rightSource)) { cacheMap.set(rightSource, rightScore); } if (rightScore === leftScore) { return 0; } return rightScore > leftScore ? 1 : -1; }; }