Manual Reference Source
public class | source

SearchableMap

Implements:

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

Constructor Summary

Public Constructor
public

Member Summary

Public Members
public get

Method Summary

Public Methods
public

[Symbol.iterator](): Iterator

public

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

public
public
public

entries(): Iterator

public

forEach(fn: function(key: string, value: any): any): undefined

public

fuzzyGet(key: string, maxEditDistance: number): Object<string, Array>

Returns a key-value object of all the entries that have a key within the given edit distance from the search key.

public

get(key: string): any

public

has(key: string): boolean

public

keys(): Iterator

public

set(key: string, value: any): SearchableMap

public

update(key: string, fn: function(currentValue: any): any): SearchableMap

Updates the value at the given key using the provided function.

public

values(): Iterator

Public Constructors

public constructor() source

Public Members

Public Methods

public atPrefix(prefix: string): SearchableMap source

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

Params:

NameTypeAttributeDescription
prefix string

The prefix

Return:

SearchableMap

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

Example:

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")
uni.get("unique") // => undefined
uni.get("universe") // => 2
uni.get("university") // => 3

public forEach(fn: function(key: string, value: any): any): undefined source

Params:

NameTypeAttributeDescription
fn function(key: string, value: any): any

Return:

undefined

See:

public fuzzyGet(key: string, maxEditDistance: number): Object<string, Array> source

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.

Params:

NameTypeAttributeDescription
key string

The search key

maxEditDistance number

The maximum edit distance

Return:

Object<string, Array>

A key-value object of the matching keys to their value and edit distance

Example:

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

public get(key: string): any source

Params:

NameTypeAttributeDescription
key string

Return:

any

See:

public set(key: string, value: any): SearchableMap source

Params:

NameTypeAttributeDescription
key string
value any

Return:

SearchableMap

The SearchableMap itself, to allow chaining

See:

public update(key: string, fn: function(currentValue: any): any): SearchableMap source

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.

Params:

NameTypeAttributeDescription
key string

The key

fn function(currentValue: any): any

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

Return:

SearchableMap

The SearchableMap itself, to allow chaining

Example:

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