UNPKG

1.44 kBTypeScriptView Raw
1declare module 'fast-fuzzy' {
2 export enum sortKind {
3 insertOrder = "insertOrder",
4 bestMatch = "bestMatch",
5 };
6
7 interface FuzzyOptions {
8 ignoreCase?: boolean,
9 ignoreSymbols?: boolean,
10 normalizeWhitespace?: boolean,
11 useDamerau?: boolean,
12 useSellers?: boolean,
13 returnMatchData?: boolean,
14 sortBy?: sortKind,
15 }
16
17 interface AdditionalOptions<T> {
18 keySelector?: (s: T) => string | string[],
19 threshold?: number,
20 }
21
22 export type FullOptions<T> = FuzzyOptions & AdditionalOptions<T>;
23
24 export interface MatchData<T> {
25 item: T,
26 original: string,
27 key: string,
28 score: number,
29 match: {
30 index: number,
31 length: number,
32 },
33 }
34
35 export function fuzzy<T extends FuzzyOptions>(term: string, candidate: string, options?: T): T["returnMatchData"] extends true ? MatchData<string> : number;
36 export function search<T extends (string | object), U extends FullOptions<T>>(term: string, candidates: T[], options?: U):
37 U["returnMatchData"] extends true ? MatchData<T>[] : T[];
38 export class Searcher<T extends (string | object), U extends FullOptions<T>> {
39 constructor(candidates?: T[], options?: U);
40 add(...candidates: T[]): void;
41 search<V extends FullOptions<T>>(term: string, options?: V):
42 V["returnMatchData"] extends true ? MatchData<T>[]
43 : V["returnMatchData"] extends false ? T[]
44 : U["returnMatchData"] extends true ? MatchData<T>[]
45 : T[];
46 }
47}