Socket
Socket
Sign inDemoInstall

trie-mapping

Package Overview
Dependencies
0
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    trie-mapping

A compact trie for mapping keys to values


Version published
Weekly downloads
30
increased by100%
Maintainers
1
Install size
26.2 kB
Created
Weekly downloads
 

Readme

Source

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.

Keywords

FAQs

Last updated on 25 Jul 2022

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc