npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

radix-trie-js

v1.1.0

Published

Radix Trie in Javascript

Downloads

10,472

Readme

Radix Trie (in Javascript)

NPM build

Usage

Install from NPM:

npm i radix-trie-js
const RadixTrie = require("radix-trie-js");
// create a trie with the key of foo and value of 5
const trie = new RadixTrie().add("foo", 5);

or create the tree from an object, array or map:

const trie = new Trie([
  ["foo", 5],
  ["foobar", 9],
]);

const trie = new Trie({
  foo: 5,
  foobar: 9,
});

const map = new Map([
  ["foo", 5],
  ["foobar", 9],
]);
const trie = new Trie(map);

Methods

add

Inserts a key with the given value.

const trie = new RadixTrie().add("bar", 4);

or insert many at once:

const trie = new Trie().add([
  ["foo", 5],
  ["foobar", 9],
]);

const map = new Map([
  ["foo", 5],
  ["foobar", 9],
]);
const trie = new Trie().add(map);

const trie = new Trie().add({
  foo: 5,
  foobar: 9,
});

delete

Deletes a key/value pair.

const trie = new RadixTrie().add("foo", 1).add("bar", 8);
trie.get("foo"); // 1

trie.delete("foo");

trie.get("foo"); // null

get

Gets the value for a given key.

const trie = new RadixTrie().add("bar", 4);

trie.get("bar");
// 4

fuzzyGet

Returns an iterator for all the keys and values in the trie that match or partially match a given value regardless of case (upper or lowercase). This is similar to an autocomplete feature, which many tries are used for.

const trie = new RadixTrie();
trie.add("hi").add("Hello", false);

trie.fuzzyGet("h").next();
// { value: ["hi", true], done: false }

[...trie.fuzzyGet("h")];
// [ ["hi", true], ["Hello", false]]

Array.from(trie.fuzzyGet("hel"));
// [ ["Hello", false] ]

has

Returns true if the given value exists.

const trie = new RadixTrie().add("bar", 4);

trie.has("bar");
// true

entries

Returns an iterator for all the keys and values in the trie.

NOTE: that order cannot be preserved as a trie is constantly being compressed or expanded with each addition/deletion. In the below example, "ten" is first, but is removed later with the addition of "three", and the prefix "t" is added to consolidate them. So, now "five" will be first.

const trie = new RadixTrie();
trie.add("ten", 10).add("five", 5).add("three", 3);

trie.entries().next();
// { value: ["five", 5], done: false }

[...trie.entries()];
// [ [ "five", 5 ], [ "ten", 10 ], [ "three", 3 ] ]

Array.from(trie.entries());
// [ [ "five", 5 ], [ "ten", 10 ], [ "three", 3 ] ]

keys

Returns an iterator for all the keys in the trie.

NOTE: that order cannot be preserved as a trie is constantly being compressed or expanded with each addition/deletion. In the below example, "ten" is first, but is removed later with the addition of "three", and the prefix "t" is added to consolidate them. So, now "five" will be first.

const trie = new RadixTrie();
trie.add("ten", 10).add("five", 5).add("three", 3);

trie.keys().next();
// { value: "five", done: false }

[...trie.keys()];
// [ "five", "ten", "three" ]

Array.from(trie.keys());
// [ "five", "ten", "three" ]

values

Returns an iterator for all the values in the trie.

NOTE: that order cannot be preserved as a trie is constantly being compressed or expanded with each addition/deletion. In the below example, "ten" is first, but is removed later with the addition of "three", and the prefix "t" is added to consolidate them. So, now "five" will be first.

const trie = new RadixTrie();
trie.add("ten", 10).add("five", 5).add("three", 3);

trie.values().next();
// { value: 5, done: false }

[...trie.values()];
// [ 5, 10, 3 ]

Array.from(trie.values());
// [ 5, 10, 3 ]

toJSON

Returns stringified JSON of all entries.

const trie = new RadixTrie({
  ten: 10,
  active: false,
  hello: "world"
});

trie.toJSON();
// {"ten":10,"active":false,"hello":"world"}

forEach

Executes a callback once for each key/value pair. It takes an optional second argument for a this value that the callback will be called with.

const trie = new RadixTrie().add("bar", 15).add("barstool", false);

let thisObj = {};
const callback = function (key, value) {
  this[key] = value;
};

trie.forEach(callback, thisObj);

thisObj.bar;
// 15
thisObj.barstool;
// false