Class SearchableMap<T>

A class implementing the same interface as a standard JavaScript Map with string keys, but adding support for efficiently searching entries with prefix or fuzzy search. This class is used internally by SearchIndex as the inverted index data structure. The implementation is a radix tree (compressed prefix tree).

Since this class can be of general utility beyond SlimSearch, it is exported by the slimsearch package and can be imported (or required) as slimsearch/SearchableMap.

Type Parameters

  • T = any

    The type of the values stored in the map.

Constructors

Properties

_prefix: string
_size: undefined | number = undefined
_tree: RadixTree<T>

Accessors

Methods

  • Creates and returns a mutable view of this SearchableMap, containing only entries that share the given prefix.

    Usage:

    const map = new SearchableMap()
    map.set("unicorn", 1)
    map.set("universe", 2)
    map.set("university", 3)
    map.set("unique", 4)
    map.set("hello", 5)

    const uni = map.atPrefix("uni")
    uni.get("unique") // => 4
    uni.get("unicorn") // => 1
    uni.get("hello") // => undefined

    const univer = map.atPrefix("univer")
    univer.get("unique") // => undefined
    univer.get("universe") // => 2
    univer.get("university") // => 3

    Parameters

    • prefix: string

      The prefix

    Returns SearchableMap<T>

    A SearchableMap representing a mutable view of the original Map at the given prefix

  • Fetches the value of the given key. If the value does not exist, calls the given function to create a new value, which is inserted at the given key and subsequently returned.

    Example:

    const map = searchableMap.fetch('somekey', () => new Map())
    map.set('foo', 'bar')

    Parameters

    • key: string

      The key to update

    • initial: (() => T)

      A function that creates a new value if the key does not exist

        • (): T
        • Returns T

    Returns T

    The existing or new value at the given key

  • Returns a Map of all the entries that have a key within the given edit distance from the search key. The keys of the returned Map are the matching keys, while the values are two-element arrays where the first element is the value associated to the key, and the second is the edit distance of the key to the search key.

    Usage:

    const map = new SearchableMap()
    map.set('hello', 'world')
    map.set('hell', 'yeah')
    map.set('ciao', 'mondo')

    // Get all entries that match the key 'hallo' with a maximum edit distance of 2
    map.fuzzyGet('hallo', 2)
    // => Map(2) { 'hello' => ['world', 1], 'hell' => ['yeah', 2] }

    // In the example, the "hello" key has value "world" and edit distance of 1
    // (change "e" to "a"), the key "hell" has value "yeah" and edit distance of 2
    // (change "e" to "a", delete "o")

    Parameters

    • key: string

      The search key

    • maxEditDistance: number

      The maximum edit distance (Levenshtein)

    Returns FuzzyResults<T>

    A Map of the matching keys to their value and edit distance

  • Updates the value at the given key using the provided function. The function is called with the current value at the key, and its return value is used as the new value to be set.

    Example:

    // Increment the current value by one
    searchableMap.update('somekey', (currentValue) => currentValue == null ? 0 : currentValue + 1)

    If the value at the given key is or will be an object, it might not require re-assignment. In that case it is better to use fetch(), because it is faster.

    Parameters

    • key: string

      The key to update

    • fn: ((value) => T)

      The function used to compute the new value from the current one

        • (value): T
        • Parameters

          • value: undefined | T

          Returns T

    Returns SearchableMap<T>

    The SearchableMap itself, to allow chaining

Generated using TypeDoc