@committed/trie-search
Version:
A trie implementation that maps keys to objects for rapid retrieval by phrases. Most common use will be for typeahead searches.
64 lines (63 loc) • 2.55 kB
TypeScript
import HashArray from "hasharray";
declare const DEFAULT_INTERNATIONALIZE_EXPAND_REGEXES: {
regex: RegExp;
alternate: string;
}[];
export declare type TrieOptions = {
cache: boolean;
expandRegexes: typeof DEFAULT_INTERNATIONALIZE_EXPAND_REGEXES;
idFieldOrFunction?: string | ((obj: unknown) => string);
ignoreCase: boolean;
indexField?: string;
insertFullUnsplitKey: boolean;
keepAll: boolean;
keepAllKey: string;
maxCacheSize: number;
min?: number;
/**
* How to split words when using get(). If undefined uses options.splitOnRegEx. If false, does not attempt the split.
*/
splitOnGetRegEx?: RegExp | false;
/**
* How to split words when they are added to the trie. If undefined, does not attempt the split.
*/
splitOnRegEx?: RegExp;
};
export declare type ObjectKey = string;
export declare type TrieNode<O> = Record<string, (TrieNode<O> | O)[]>;
export declare type KeyFields = (ObjectKey | ObjectKey[])[];
export declare type TrieJson<O = unknown> = {
root: TrieNode<O>;
keyFields: KeyFields;
options: Omit<TrieOptions, "expandRegexes" | "idFieldOrFunction" | "splitOnGetRegEx" | "splitOnRegEx">;
};
export declare type Reducer<O = {}> = (accumulator: O[] | undefined, phrase: string, matches: O[], trie: TrieSearch) => O[];
export declare class TrieSearch<O = {}> {
static readonly DEFAULT_OPTIONS: TrieOptions;
static fromJson<O = {}>(json: TrieJson<O>): TrieSearch<O>;
static readonly UNION_REDUCER: Reducer;
static deepLookup(obj: {}, keys: ObjectKey | ObjectKey[]): any;
readonly options: TrieOptions;
readonly keyFields: KeyFields;
size: number;
root: TrieNode<O>;
getCache: HashArray;
indexed: HashArray | undefined;
constructor(keyFields?: KeyFields | string, options?: Partial<TrieOptions>);
add(obj: O, customKeys?: KeyFields): void;
expandString(value: string): string[];
addAll(arr: O[], customKeys?: KeyFields): void;
reset(): void;
clearCache(): void;
cleanCache(): void;
map(key: string, value: O): void;
addFromObject(obj: any, valueField?: string): void;
keyToArr(key: String): string[];
findNode(key: string): TrieNode<O> | undefined;
_getCacheKey(phrase: string, limit?: number): string;
_get(phrase: string, limit?: number): any;
get(phrases: string | string[], reducer?: Reducer<O>, limit?: number): O[];
getId(item: O): any;
toJson(): TrieJson;
}
export {};