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

digital-tree

v2.0.4

Published

trie data structure

Downloads

153

Readme

digital tree

A trie data structure implementation.

  • thorough testing
  • utility: clonable and serializable (to/from json)
  • search values by prefix

Install

npm install --save digital-tree

version 2.0.0 is almost a complete rewrite and mostly not backwards compatible

API

create() / Ctor

using `create()`` is the recommended way to construct new digital trees:

    const Trie = require('digital-tree')
    const trie = Trie.create()

put(key, value)

Put something in the tree

    trie.put(['a', 'path', 'to'], 'something')
    trie.put(['another', 'thing']) // equivalent to trie.put(['another', 'thing'], true)
    trie.put('strings also', 'work') // equivalent to trie.put('strings also'.split(''), 'work')
    
    // ** this only work with the exact key (reference) **
    const objectKey = [{ foo: 'bar' }, { bar: 'foo'}]
    trie.put(objectKey, { some: 'thing'})

get(key)

Get something from the tree

    const trie = Trie.create()
    trie.put(['a', 'path', 'to'], 'v1')
    trie.put('also strings', 'v2')

    console.log(trie.get([])) // prints 'foo'
    console.log(trie.get(Trie.root)) // prints 'foo'
    console.log(trie.get(['a', 'path', 'to'])) // prints 'v1'
    console.log(trie.get('also strings')) // prints 'v2'

Iteration

A trie is iterable. Iteration order is either DFS or BFS

    const Trie = require('digital-tree')
    const trie = Trie.create()
    trie.put('abc', 1)
    trie.put('abd', 2)
    trie.put('abe', 3)

    for (let value of trie) {

    }

search(prefix)

Search and return all the values in the tree that are nested under the provided prefix.

The results will be an Iterator over the matching values. The order of iteration is defined based on the default ordering of the trie (BFS/DFS)

    const trie = Trie.create()
    trie.put('abc', 1)
    trie.put('abd', 2)
    trie.put('abe', 3)

    console.log(Array.from(trie.search('ab'))) // prints [ 3, 2, 1 ]
    console.log(Array.from(trie.search('ab', { includeKeys: true }))) // prints [ [['a','b','e'], 3 ], [['a','b','d'], 2], [['a','b','c'], 1] ]

getSubTrie(key, [shallow=false])

Obtain either a cloned, or shallow copy of a subtree.

trie.put('abc', 1)
trie.put('abd', 2)

const subTrie = trie.getSubTrie('ab')

console.log(subTrie.get('c')) // prints 1
console.log(subTrie.get('d')) // prints 2
console.log(subTrie.get('ab')) // prints undefined

setting shallow to true will create a view rather than cloning the sub trie

remove(key)

Remove something from the tree. This will remove the entire subtree that exists under this specified key and return it as a new trie.

    trie.put(['a', 'b'], 'ab')
    trie.put(['a', 'b', 'c'], 'abc')
    trie.put(['a', 'b', 'c', 1], 'abc1')
    trie.put(['a', 'b', 'c', 2], 'abc2')

    const removed = trie.remove(['a', 'b', 'c'])
    
    console.log(removed.get([1])) // prints 'abc1'
    console.log(removed.get([2])) // prints 'abc2'

    console.log(trie.get(['a', 'b', 'c', 1])) // prints 'undefined'
    console.log(trie.get(['a', 'b'])) // prints 'ab'