A Trie is a data structure designed for quick reTRIEval of objects by string search. This was designed for use with a type-ahead search (e.g. like a dropdown) but could be used in a variety of situations.

This data structure indexes sentences / words to objects for searching by full or partial matches. So you can map 'hello' to an Object, and then search by 'hel', 'hell', or 'hello' and get the Object or an Array of all objects that match.

By default, sentences / words are split along whitespace boundaries. For example, if your inserted mapping is 'the quick brown fox', this object will be searchable by 'the', 'quick', 'brown', or 'fox' or any of their partials like 'qui' or 'qu' or 'fo'. Boundaries can be customized using the splitOnRegEx option.

By default, the trie-search is internationalized for a common set of vowels in the ASCII set. So if you insert 'รถ', then searching on 'o' will return that result. You can customize this by providing your own expandRegexes object.

Type Parameters

  • T extends object

Constructors

  • Type Parameters

    • T extends object

    Parameters

    • OptionalkeyFields: string | KeyFields

      A single string or an array of strings / arrays representing what fields on added objects are to be used as keys for the trie search / HashArray.

    • Optionaloptions: TrieSearchOptions

      Options.

    Returns TrieSearch<T>

Accessors

  • get isDestroyed(): boolean
  • Returns boolean

    Whether this TrieSearch instance has been destroyed.

  • get size(): number
  • Returns number

    Number of nodes in the trie data structure.

Methods

  • Add items or list of items to the TrieSearch instance.

    Parameters

    • Rest...items: (T | Iterable<T, any, any>)[]

      Items to add.

    Returns this

  • Clears all items.

    Returns this

    This instance.

  • Destroys this TrieSearch instance. Removing all data and preventing new data from being added. Any subscribers are notified with an undefined argument in the callback signaling that the associated instance is destroyed.

    Returns this

  • Directly maps an item to the given key.

    Parameters

    • key: string

      The key to store the item.

    • value: T

      The item to store.

    Returns this

  • Performs a search of the trie data structure with the given phrases. By default, each phrase is split by TrieSearchOptions.splitOnGetRegEx and matches found for each word resulting in a OR lookup. You may provide a reducer function to change the behavior

    Parameters

    • phrases: string | Iterable<string, any, any>

      The phrases to parse and search in the trie data structure.

    • Optionaloptions: {
          limit?: number;
          list?: T[];
          reducer?: TrieSearchReducer<T>;
      }

      Search Options.

      • Optionallimit?: number

        The limit for search results returned.

      • Optionallist?: T[]

        An external array to use for storing search results.

      • Optionalreducer?: TrieSearchReducer<T>

        A trie reducer instance to apply to this search.

    Returns T[]

    Found matches.

  • Subscribe for change notification on add / clear / destroy.

    Note: There is no data defined regarding what changed only that one of three actions occurred. This TrieSearch instance is sent as the only argument. When it is undefined this signals that the TrieSearch instance has been destroyed.

    Parameters

    Returns (() => void)

    Unsubscribe function.

      • (): void
      • Returns void