trie-mapping

A compact trie for mapping keys to values

Usage no npm install needed!

<script type="module">
  import trieMapping from 'https://cdn.skypack.dev/trie-mapping';
</script>

README

trie-mapping

build status npm version bundle size

A compact trie for mapping keys to values

Installing

npm install trie-mapping

API

The API mimics the native Map, with the following differences:

The size getter and the clear() method are identical to those of the native Map.

trieMapping(elements)

Returns a trie object.

It may be initialized from the given elements, which is an array or other iterable whose elements are key-value pairs, or a root object. If elements is a root object, it may be deeply mutated by the trie's methods.

import trieMapping from "trie-mapping";

// Create an empty trie
trieMapping();

// Initialize from an array
trieMapping([
  ["hey", 0],
  ["hi", 1]
]);

// Initialize from a trie's root object
trieMapping({
  h: {
    ey: { "": 0 },
    i: { "": 1 }
  }
});

root

Returns the root node, whose "" key is the label of its value, and the rest of its keys are the labels of its child nodes.

import trieMapping from "trie-mapping";

trieMapping([
  ["he", 1],
  ["hey", 5],
  ["hells", 4],
  ["hello", 3],
  ["hell", 2],
  ["bye", 0]
]).root;
// =>
// {
//   he: {
//     "": 1,
//     y: { "": 5 },
//     ll: {
//       s: { "": 4 },
//       o: { "": 3 },
//       "": 2
//     }
//   },
//   bye: { "": 0 }
// }

size

Returns the number of key-value pairs.

clear()

Removes all key-value pairs.

delete(key)

Returns true if an element with the given key existed and has been removed, or false if the element does not exist.

entries()

Returns a new Iterator object that contains an array of [key, value] for each element in alphabetical order.

forEach(callbackfn, thisArg)

Calls the given callbackfn once for each key-value pair, in alphabetical order, passing to the callbackfn the value of the item, the key of the item, and the trie object being traversed. If thisArg is given, it will be used as the this value for each callback.

get(key)

Returns the value associated to the given key, or undefined if there is none.

has(key)

Returns true if a value has been associated to the given key, or false otherwise.

keys()

Returns a new Iterator object that contains the keys for each element in alphabetical order.

set(key, value)

Returns the trie, associating the given value to the given key.

values()

Returns a new Iterator object that contains the values for each element in alphabetical order.

[@@iterator]()

Returns a new Iterator object that contains an array of [key, value] for each element in alphabetical order.

License

MIT