UNPKG

2.48 kBTypeScriptView Raw
1type KeyAttributes = {
2 threshold?: Ranking;
3 maxRanking: Ranking;
4 minRanking: Ranking;
5};
6interface RankingInfo {
7 rankedValue: string;
8 rank: Ranking;
9 keyIndex: number;
10 keyThreshold: Ranking | undefined;
11}
12interface ValueGetterKey<ItemType> {
13 (item: ItemType): string | Array<string>;
14}
15interface IndexedItem<ItemType> {
16 item: ItemType;
17 index: number;
18}
19interface RankedItem<ItemType> extends RankingInfo, IndexedItem<ItemType> {
20}
21interface BaseSorter<ItemType> {
22 (a: RankedItem<ItemType>, b: RankedItem<ItemType>): number;
23}
24interface Sorter<ItemType> {
25 (matchItems: Array<RankedItem<ItemType>>): Array<RankedItem<ItemType>>;
26}
27interface KeyAttributesOptions<ItemType> {
28 key?: string | ValueGetterKey<ItemType>;
29 threshold?: Ranking;
30 maxRanking?: Ranking;
31 minRanking?: Ranking;
32}
33type KeyOption<ItemType> = KeyAttributesOptions<ItemType> | ValueGetterKey<ItemType> | string;
34interface MatchSorterOptions<ItemType = unknown> {
35 keys?: ReadonlyArray<KeyOption<ItemType>>;
36 threshold?: Ranking;
37 baseSort?: BaseSorter<ItemType>;
38 keepDiacritics?: boolean;
39 sorter?: Sorter<ItemType>;
40}
41declare const rankings: {
42 readonly CASE_SENSITIVE_EQUAL: 7;
43 readonly EQUAL: 6;
44 readonly STARTS_WITH: 5;
45 readonly WORD_STARTS_WITH: 4;
46 readonly CONTAINS: 3;
47 readonly ACRONYM: 2;
48 readonly MATCHES: 1;
49 readonly NO_MATCH: 0;
50};
51type Ranking = typeof rankings[keyof typeof rankings];
52declare const defaultBaseSortFn: BaseSorter<unknown>;
53/**
54 * Takes an array of items and a value and returns a new array with the items that match the given value
55 * @param {Array} items - the items to sort
56 * @param {String} value - the value to use for ranking
57 * @param {Object} options - Some options to configure the sorter
58 * @return {Array} - the new sorted array
59 */
60declare function matchSorter<ItemType = string>(items: ReadonlyArray<ItemType>, value: string, options?: MatchSorterOptions<ItemType>): Array<ItemType>;
61declare namespace matchSorter {
62 var rankings: {
63 readonly CASE_SENSITIVE_EQUAL: 7;
64 readonly EQUAL: 6;
65 readonly STARTS_WITH: 5;
66 readonly WORD_STARTS_WITH: 4;
67 readonly CONTAINS: 3;
68 readonly ACRONYM: 2;
69 readonly MATCHES: 1;
70 readonly NO_MATCH: 0;
71 };
72}
73export { matchSorter, rankings, defaultBaseSortFn };
74export type { MatchSorterOptions, KeyAttributesOptions, KeyOption, KeyAttributes, RankingInfo, ValueGetterKey, };