UNPKG

@picosearch/radix-tree

Version:

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

81 lines (80 loc) 3.07 kB
import type { IFuzzySearchOptions, IRadixTree, IRadixTreeMap, RadixTreeMapNode } from './types'; export declare class RadixTreeMap<T> implements IRadixTreeMap<T> { private root; constructor(root?: RadixTreeMapNode<T>); /** * Inserts a key into the tree. * * @param key The key to insert. * @param values The values to associate with the key. */ insert(key: string, ...values: T[]): void; /** * Returns the values for the given key. * * @param key The key to look up. * @returns The values for the given key or null if the key is not found. */ lookup(key: string): T[] | null; /** * Returns the fuzzy matches for the given query. * * Please note that a RadixTree is not optimized for fuzzy search and this function implements a * dynamic programming approach to compute the edit distance for each path in the tree. 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 search for. * @param options The options to use for the search. * @returns The fuzzy matches for the given query. */ getFuzzyMatches(query: string, options?: IFuzzySearchOptions & { includeValues?: false; }): string[]; /** * Returns the fuzzy matches for the given query including the values. * * Please note that a RadixTree is not optimized for fuzzy search and this function implements a * dynamic programming approach to compute the edit distance for each path in the tree. 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 search for. * @param options The options to use for the search. * @returns The fuzzy matches for the given query including the values. */ getFuzzyMatches(query: string, options?: IFuzzySearchOptions & { includeValues: true; }): [string, T[]][]; toJSON(): string; static fromJSON<T>(jsonStr: string): RadixTreeMap<T>; } export declare class RadixTree implements IRadixTree { private tree; constructor(options?: { root?: RadixTreeMapNode<1>; }); /** * Inserts a key into the tree. * * @param key The key to insert. */ insert(key: string): void; /** * Checks if the tree contains the given key. * * @param key The key to check. * @returns Whether the tree contains the given key. */ has(key: string): boolean; /** * Returns the fuzzy matches for the given query. * * @param query The query to search for. * @param options The options to use for the search. * @returns The fuzzy matches for the given query. */ getFuzzyMatches(query: string, options?: Omit<IFuzzySearchOptions, 'includeValues'>): string[]; toJSON(): string; static fromJSON(jsonStr: string): RadixTree; }