|
| 1 | + |
| 2 | +/** |
| 3 | + * The options that can be passed to {@link Matcher#match}. |
| 4 | + */ |
| 5 | +export type MatcherOptions = { |
| 6 | + /** Whether matching is case-sensitive. Defaults to `false`. */ |
| 7 | + caseSensitive?: boolean; |
| 8 | + |
| 9 | + /** How many results to return at the maximum. Defaults to no limit. */ |
| 10 | + maxResults?: number; |
| 11 | + |
| 12 | + /** |
| 13 | + * Maximum “gap” to allow between consecutive letters for a match candiate. |
| 14 | + * Provide a smaller value to speed up query results. Defaults to no limit. |
| 15 | + */ |
| 16 | + maxGap?: number; |
| 17 | + |
| 18 | + /** |
| 19 | + * How many threads to use while searching. Defaults to `1`. |
| 20 | + */ |
| 21 | + numThreads?: number; |
| 22 | + |
| 23 | + /** |
| 24 | + * Whether to return metadata about the indices of the characters that |
| 25 | + * matched in each returned max. Defaults to `false`. |
| 26 | + */ |
| 27 | + recordMatchIndexes?: boolean; |
| 28 | + |
| 29 | + /** |
| 30 | + * The algorithm to use for fuzzy-matching. If `"fuzzaldrin"`, will use that |
| 31 | + * algorithm for legacy support. Any other value, including the default of |
| 32 | + * `undefined`, will trigger use of the default algorithm. |
| 33 | + */ |
| 34 | + algorithm?: 'fuzzaldrin' | undefined; |
| 35 | +}; |
| 36 | + |
| 37 | +/** |
| 38 | + * A single result returned by {@link Matcher#match}. |
| 39 | + */ |
| 40 | +export type MatchResult = { |
| 41 | + /** A unique identifier for the match. */ |
| 42 | + id: number; |
| 43 | + |
| 44 | + /** The string value of the match. */ |
| 45 | + value: string; |
| 46 | + |
| 47 | + /** |
| 48 | + * A number in the range (0, 1] — i.e., the maximum value is `1` and the |
| 49 | + * minimum value is the smallest possible positive value. Higher scores mean |
| 50 | + * more relevant matches. `0` means “no match” and will never be returned. |
| 51 | + */ |
| 52 | + score: number; |
| 53 | + |
| 54 | + /** |
| 55 | + * Matching charcter index in `value` for each character in `query`. This can |
| 56 | + * be costly, so this information is returned only when |
| 57 | + * {@link MatcherOptions.recordMatchIndexes} is `true`. |
| 58 | + */ |
| 59 | + matchIndexes?: number[]; |
| 60 | +} |
| 61 | + |
| 62 | +export class Matcher { |
| 63 | + /** |
| 64 | + * Construct a new {@link Matcher} object. |
| 65 | + * |
| 66 | + * You may specify candidates at instantiation time (with the same arguments |
| 67 | + * used by {@link addCandidates} and {@link setCandidates}) or you may wait |
| 68 | + * and add candidates later. |
| 69 | + * |
| 70 | + * @param ids A list of numeric IDs. Must correspond to the candidates |
| 71 | + * themselves. |
| 72 | + * @param candidates A list of candidates against which we will be matching. |
| 73 | + */ |
| 74 | + constructor(); |
| 75 | + constructor(ids: number[], candidates: string[]); |
| 76 | + |
| 77 | + /** |
| 78 | + * Find all candidates that match the given query. |
| 79 | + * |
| 80 | + * @param query The input against which candidates will be searched. |
| 81 | + * @param options Any {@link MatcherOptions}. |
| 82 | + */ |
| 83 | + match(query: string, options?: MatcherOptions): MatchResult[]; |
| 84 | + |
| 85 | + /** |
| 86 | + * Add candidates to the list. |
| 87 | + * |
| 88 | + * You are responsible for ensuring that the IDs you use do not match the IDs |
| 89 | + * of any candidates that are already present in the `Matcher`. Any |
| 90 | + * candidates whose IDs already exist in the `Matcher` are silently ignored. |
| 91 | + * |
| 92 | + * @param ids A list of numeric IDs. Must correspond to the candidates |
| 93 | + * themselves. |
| 94 | + * @param candidates A list of candidates against which we will be matching. |
| 95 | + */ |
| 96 | + addCandidates(ids: number[], candidates: string[]): void; |
| 97 | + |
| 98 | + /** |
| 99 | + * Remove candidates from the list. |
| 100 | + * |
| 101 | + * @param ids The unique identifiers for each of the candidates you want to |
| 102 | + * remove. Must be an array; if you want to remove only one candidate, wrap |
| 103 | + * the value in an array first. |
| 104 | + */ |
| 105 | + removeCandidates(ids: number[]): void; |
| 106 | + |
| 107 | + /** |
| 108 | + * Set a complete list of candidates, removing any candidate that may already |
| 109 | + * be defined. |
| 110 | + * |
| 111 | + * If you want to add candidates instead without removing any that may |
| 112 | + * already exist, use {@link addCandidates}. |
| 113 | + * |
| 114 | + * @param ids A list of numeric IDs. Must correspond to the candidates |
| 115 | + * themselves. |
| 116 | + * @param candidates A list of candidates against which we will be matching. |
| 117 | + */ |
| 118 | + setCandidates(ids: number[], candidates: string[]): void; |
| 119 | +} |
0 commit comments