The TrieMap data structure root.
Returns the number of values in the TrieMap.
const trie = new TrieMap();
trie.
.set(['some', 'path'], 'value')
.count;
//=> 1
Returns an Iterable that yields each entry ([prefix, value]) in the TrieMap with the given prefix.
Optional
prefix: string[]A string array.
const trie = new TrieMap()
.set(['src', 'classes'], 2)
.set(['src', 'modules'], 2)
.set(['docs'], 8);
[...trie];
//=> [
// [['src', 'classes'], 2],
// [['src', 'modules'], 2],
// [['docs', 8]]
// ]
Deletes the value at the given prefix. Returns whether the operation was successful.
A string array.
const trie = new TrieMap();
trie.set(['some', 'path'], 'value');
trie.has(['some', 'path']);
//=> true
trie.delete(['some', 'path']);
//=> true (means operation was successful)
trie.has(['some', 'path']);
//=> false
Protected
deleteDeletes the value at the given prefix or all values with the given prefix if ´prune´ is set to true.
A string array.
if the operation was unsuccessful.
const trie = new TrieMap();
trie.delete(['nonexistent', 'path']);
//=> false (operation unsuccessful)
trie.deleteStrict(['nonexistent', 'path']);
//=> throws Error
Returns an Iterable that yields each entry ([prefix, value]) in the TrieMap with the given prefix.
A string array.
const trie = new TrieMap()
.set(['src', 'classes'], 2)
.set(['src', 'modules'], 2)
.set(['docs'], 8);
[...trie.entries()];
//=> [
// [['src', 'classes'], 2],
// [['src', 'modules'], 2],
// [['docs', 8]]
// ]
Iterates all (value, prefix) where value === ´valueToFind´.
A string array.
The value to look for.
A callback function.
const directoryFileCounts = new TrieMap()
.set(['src', 'classes'], 2)
.set(['src', 'modules'], 2)
.set(['docs'], 8);
const directoryPathsWithTwoFiles = [];
directoryFileCounts.find([], (value, prefix) => {
if(value === 2) {
directoryPathsWithTwoFiles.push(prefix);
}
});
// directoryPathsWithTwoFiles will now contain: [
// ['src', 'classes'],
// ['src', 'modules']
// ]
Iterate each (value, prefix) with the given prefix.
A string array.
A callback function. Return true to terminate iteration.
const directoryFileCounts = new TrieMap()
.set(['src', 'classes'], 2)
.set(['src', 'modules'], 6)
.set(['docs'], 8);
let totalFiles = 0;
directoryFileCounts.forEach([], (value, prefix) => {
totalFiles += value;
});
// totalFiles (2 + 6 + 8) is now = 16
let totalSourceFiles = 0;
directoryFileCounts.forEach(['src'], (value, prefix) => {
totalSourceFiles += value;
});
// totalSourceFiles (2 + 6) is now = 8
Protected
getProtected
getReturns an array of alle values that begin with a specified precix.
A string array.
const directoryFileCounts = new TrieMap()
.set(['src', 'classes'], 2)
.set(['src', 'modules'], 3)
.set(['docs'], 8);
directoryFileCounts.getAll(['src']);
//=> [2, 3]
Protected
hasReturns an Iterable that yields each prefix in the TrieMap with the given prefix.
A string array.
const trie = new TrieMap()
.set(['src', 'classes'], 2)
.set(['src', 'modules'], 2)
.set(['docs'], 8);
[...trie.keys()];
//=> [
// ['src', 'classes'],
// ['src', 'modules'],
// ['docs']
// ]
Insert multiple entries into the TrieMap.
An array or other iterable that yields entries.
this/self (chainable)
const trie = new TrieMap().load([
[['some', 'path'], 'value1'],
[['other', 'path'], 'value2']
]);
Protected
setProtected
setReturns an Iterable that yields each entry ([prefix, value]) in the TrieMap with the given prefix.
A string array.
const trie = new TrieMap()
.set(['src', 'classes'], 2)
.set(['src', 'modules'], 2)
.set(['docs'], 8);
trie.toArray();
//=> [
// [['src', 'classes'], 2],
// [['src', 'modules'], 2],
// [['docs', 8]]
// ]
Returns the trie map data structure as pretty printed JSON.
Whether to return a pretty formatted JSON string rather than a condensed machine readble string.
const trie = new TrieMap()
.set(['src', 'classes'], 2)
.set(['src', 'modules'], 2)
.set(['docs'], 8);
trie.toJson();
//=> "{root:{src:{classes:2,modules:2,},docs:8,}}"
trie.toJson(true);
//=> {
// root: {
// src: {
// classes: 2,
// modules: 2,
// },
// docs: 8,
// },
// }
Updates a value in the TrieMap.
A string array.
A function that when passed the current value, will return another replacement value.
this/self (chainable)
const trie = new TrieMap();
trie.set(['some', 'path'], 4);
trie.get(['some', 'path']);
//=> 4
trie.update(['some', 'path'], (value) => {
return value + 2
});
trie.get(['some', 'path']);
//=> 6
Iterate each (value, prefix) with the given prefix and replace all elements using a callback function.
A string array.
A callback function.
Optional
prefix: string[]const trie = new TrieMap();
trie.load([
[['a'], 0],
[['b'], 1],
[['a', 'a'], 2],
]);
trie.updateAll([], (value) => value + 1);
trie.get(['a']) // => 1
trie.get(['b']) // => 2
trie.get(['a', 'a']) // => 3
Protected
updateProtected
validateReturns an Iterable that yields each value in the TrieMap with the given prefix.
A string array.
const trie = new TrieMap()
.set(['src', 'classes'], 2)
.set(['src', 'modules'], 2)
.set(['docs'], 8);
[...trie.values()];
//=> [2, 2, 8]
Static
fromCreates a new instance from existing data.
An interable that yields entries.
const trie = TrieMap.fromIterable([
[['some', 'path'], 'value1'],
[['other', 'path'], 'value2']
]);
Static
fromJSONCreates a new instance from existing data.
A JSON-string (a previously stringified TrieMap instance).
const json = new TrieMap()
.set(['some', 'path'], 'value')
.toJson();
const trie = TrieMap.fromJSON(json);
Generated using TypeDoc
Class for a fast trie map.