$ show joshjung/trie-search
trie-search is a JavaScript/TypeScript library implementing a Trie (prefix tree) data structure for efficient string-based object lookup. It enables mapping string keys to objects and retrieving all objects whose keys match a given prefix. The library supports features like case-insensitive search, customizable tokenization via regex, international character handling, caching of search results, and flexible result combination through a reducer pattern. Designed for use in autocomplete/typeahead interfaces, it indexes objects by breaking keys into character trees and returns all objects in subtrees matching the search prefix. The architecture centers around a TrieSearch class with methods for adding, removing, and searching items, using nested objects for internal node representation and hasharray/md5 dependencies for caching and ID generation.
The LLM identified 3 files worth highlighting for reuse:
The expandString method and DEFAULT_INTERNATIONALIZE_EXPAND_REGEXES constant provide a self-contained solution for handling international character equivalence in text search. This component converts accented characters to their base forms (e.g., 'รฅรคร รกรขรฃรฆ' โ 'a') using configurable regex mappings, enabling searches for base letters to match accented variants. The logic is isolated, dependency-free, and directly applicable to any text processing scenario requiring diacritic-insensitive matching.
The getId method implements an adaptive object identification strategy that solves the common problem of uniquely identifying objects in collections for deduplication. It supports three approaches: using existing $tsid property, applying a custom ID function, or auto-generating MD5 hashes from specified object fields. This pattern cleanly separates concerns, handles edge cases (undefined/null fields), and provides a reusable approach for any system needing reliable object identity without imposing strict interface requirements.
The reducer pattern used in the get()/search() methods provides a flexible mechanism for combining results from multiple search criteria. Rather than hardcoding logic like AND/OR operations, the system accepts a reducer function that defines how to combine individual result sets, with UNION_REDUCER (intersection) as the default. This follows functional programming principles, allowing callers to implement custom combination logic such as ranking, weighting, or union operations without modifying the core search algorithm, making it a highly reusable pattern for any multi-criteria search system.