UNPKG

@picosearch/trie

Version:

Simple, zero dependency, type-safe implementation of a trie data structure.

81 lines (80 loc) 3.2 kB
import type { IFuzzySearchOptions, ITrie, ITrieMap, TrieMapNode } from './types'; export declare class TrieMap<T> implements ITrieMap<T> { private root; constructor(root?: TrieMapNode<T>); /** * Insert values into the TrieMap for the given key. You can pass multiple values. * * @param key - The key to insert. * @param values - The values associated with the key. */ insert(key: string, ...values: T[]): void; /** * Lookup the values associated with the given key. * * @param key - The key to lookup. * @returns An array of values associated with the given key, or `null` if the key is not found. */ lookup(query: string): T[] | null; /** * Get all words that match the given query. * * Please note that a Trie is not optimized for fuzzy search and this function implements a * dynamic programming approach to compute the edit distance for each path in the Trie. This * approach is faster than a naive implementation, but for larger data sets you should consider * using other data structures like a BK-Tree. * * @param query - The query to match. * @param options - Optional options to control the behavior of the function. * @returns An array of words that match the given query. */ getFuzzyMatches(query: string, options?: IFuzzySearchOptions & { includeValues?: false; }): string[]; /** * Get all words together with their values that match the given query. * * Please note that a Trie is not optimized for fuzzy search and this function implements a * dynamic programming approach to compute the edit distance for each path in the Trie. This * approach is faster than a naive implementation, but for larger data sets you should consider * using other data structures like a BK-Tree. * * @param query - The query to match. * @param options - Optional options to control the behavior of the function. * @returns An array of words together with their values that match the given query. */ getFuzzyMatches(query: string, options?: IFuzzySearchOptions & { includeValues: true; }): [string, T[]][]; toJSON(): string; static fromJSON<T>(jsonStr: string): TrieMap<T>; } export declare class Trie implements ITrie { private trieMap; constructor(options?: { root?: TrieMapNode<1>; }); /** * Insert a key into the Trie. * * @param key - The key to insert. */ insert(key: string): void; /** * Check if the Trie contains the given key. * * @param key - The key to check. * @returns `true` if the Trie contains the key, `false` otherwise. */ has(key: string): boolean; /** * Get all words that match the given query. * * @param query - The query to match. * @param options - Optional options to control the behavior of the function. * @returns An array of words that match the given query. */ getFuzzyMatches(query: string, options?: Omit<IFuzzySearchOptions, 'includeValues'>): string[]; toJSON(): string; static fromJSON(jsonStr: string): Trie; }