A data structure for quickly retrieving objects by a string prefix. Note that this works well for languages with alphabets (latin, cyrillic, korean, etc.), but may need more nuanced handling for languages that compose characters and letters.

Hierarchy (view full)

Properties

#root: object = ...

The tree's root.

Accessors

  • get leaves(): symbol
  • The key symbol that stores the leaves of any given node.

    Returns symbol

Methods

  • Insert an entry into the tree.

    Parameters

    • string: string

      The string key for the entry.

    • entry: WordTreeEntry

      The entry to store.

    Returns object

    The node the entry was added to.

  • Return entries that match the given string prefix.

    Parameters

    • prefix: string

      The prefix.

    • Optional options: {
          limit: number;
          filterEntries: StringTreeEntryFilter;
      } = {}

      Additional options to configure behaviour.

      • limit: number

        The maximum number of items to retrieve. It is important to set this value as very short prefixes will naturally match large numbers of entries.

      • filterEntries: StringTreeEntryFilter

        A filter function to apply to each candidate entry.

    Returns WordTreeEntry[]

    A number of entries that have the given prefix.

  • Returns the node at the given prefix.

    Parameters

    • prefix: string

      The prefix.

    Returns object

  • Protected

    Perform a breadth-first search starting from the given node and retrieving any entries reachable from that node, until we reach the limit.

    Parameters

    • node: object

      The starting node.

    • entries: any[]

      The accumulated entries.

    • queue: object[]

      The working queue of nodes to search.

    • Optional options: {
          limit: number;
          filterEntries: StringTreeEntryFilter;
      } = {}
      • limit: number

        The maximum number of entries to retrieve before stopping.

      • filterEntries: StringTreeEntryFilter

        A filter function to apply to each candidate entry.

    Returns void