The constructor is normally called without arguments, creating an empty map. In order to create a SearchableMap from an iterable or from an object, check SearchableMap.from and SearchableMap.fromObject.
The constructor arguments are for internal use, when creating derived mutable views of a map at a prefix.
Creates and returns a mutable view of this SearchableMap, containing only entries that share the given prefix.
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
The prefix
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.
const map = searchableMap.fetch('somekey', () => new Map())
map.set('foo', 'bar')
The existing or new value at the given key
Iteration function
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.
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")
The search key
The maximum edit distance (Levenshtein)
A Map of the matching keys to their value and edit distance
Key to set
Value to associate to the key
The SearchableMap itself, to allow chaining
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.
// 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.
The SearchableMap itself, to allow chaining
Static
fromCreates a SearchableMap from an Iterable
of entries
Entries to be inserted in the SearchableMap
A new SearchableMap with the given entries
Static
fromCreates a SearchableMap from the iterable properties of a JavaScript object
Object of entries for the SearchableMap
A new SearchableMap with the given entries
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) asslimsearch/SearchableMap
.