Options
All
  • Public
  • Public/Protected
  • All
Menu

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 MiniSearch as the inverted index data structure. The implementation is a radix tree (compressed prefix tree).

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

Type parameters

  • T = any

    The type of the values stored in the map.

Hierarchy

  • SearchableMap

Index

Constructors

constructor

Accessors

size

  • get size(): number

Methods

[Symbol.iterator]

  • [Symbol.iterator](): TreeIterator<T, Entry<T>>

atPrefix

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

    Usage:

    let map = new SearchableMap()
    map.set("unicorn", 1)
    map.set("universe", 2)
    map.set("university", 3)
    map.set("unique", 4)
    map.set("hello", 5)
    
    let uni = map.atPrefix("uni")
    uni.get("unique") // => 4
    uni.get("unicorn") // => 1
    uni.get("hello") // => undefined
    
    let 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

clear

  • clear(): void

delete

  • delete(key: string): void

entries

  • entries(): TreeIterator<T, Entry<T>>

forEach

  • forEach(fn: (key: string, value: T, map: SearchableMap) => void): void

fuzzyGet

  • fuzzyGet(key: string, maxEditDistance: number): FuzzyResults<T>
  • Returns a key-value object of all the entries that have a key within the given edit distance from the search key. The keys of the returned object are the matching keys, while the values are two-elements 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:

    let 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)
    // => { "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 key-value object of the matching keys to their value and edit distance

get

  • get(key: string): T | undefined

has

  • has(key: string): boolean

keys

  • keys(): TreeIterator<T, string>

set

update

  • 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)
    

    Parameters

    • key: string

      The key to update

    • fn: (value: T) => T

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

        • (value: T): T
        • Parameters

          • value: T

          Returns T

    Returns SearchableMap<T>

    The SearchableMap itself, to allow chaining

values

  • values(): TreeIterator<T, T>

Static from

  • from<T>(entries: Iterable<Entry<T>> | Entry<T>[]): SearchableMap<any>

Static fromObject

Generated using TypeDoc