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

param-trie

v2.0.0

Published

A trie with holes for parameters

Downloads

22

Readme

What's a param trie?

It's like a trie but with holes that get filled in when you look up a result. So, given the map

we get the trie

and those red parameters get filled in when we look up:

Usage

const ParamTrie = require('param-trie');
const {param, branch} = ParamTrie;

var t = ParamTrie.ofPath([
	branch('a'),
	branch('b'),
	param('c')
], 'foo');

t.lookup(['a', 'b', 'x']); //⇒ [{value: 'foo', params: {c: 'x'}}]

var t2 = t.insertPath([
	branch('a'),
	param('d'),
	branch('e')
], 'bar');

t2.lookup(['a', 'x', 'e']); //⇒ [{value: 'bar', params: {d: 'x'}}]

Api

Creating a ParamTrie

The hard way: ParamTrie constructor

new ParamTrie(values, {param, branch})

Takes single value or an array of values and an object listing the trie's children. The object should have keys param and branch, each being an object listing children of that type, with keys as names of params or text of branches and values as the ParamTrie child.

For little tries: empty and of

ParamTrie.empty()

Returns a trie with no values and no children.

ParamTrie.of(value)

Returns a trie with a single value or array of values and no children.

For narrow tries: ofPath

ParamTrie.ofPath(path, value)

Creates a nested trie with the heirarchy as given by path. ParamTrie.ofPath([], x) is equivalent to ParamTrie.of(x).

The easy way: fromMap

ParamTrie.fromMap(map)

Given a Map of paths to values, build an entire trie with the correct heirarchy.

Methods

trie.merge(other)

Combines two tries immutably; returns a new trie and leaves the originals unmodified. When paths collide, they're merged recursively. Values at the same path are concatenated.

trie.insertPath(path, value)

Special case of merge for a single path. Equivalent to trie.merge(ParamTrie.ofPath(path, value)).

trie.lookup(path)

Returns all of the matches of a particular path. Since multiple parameterised paths can match a given lookup path and a trie can have multiple values, lookup returns an array of results.

The values of the returned array are objects with keys value and params. value is the value at the resolved point in the tree. params is an object containing concrete values of params found in the heirarchy, filled in from the lookup path. If the lookup resolves to a trie with multiple values, returns one result for each value.

trie.indent(path)

Returns the trie nested under the path. ParamTrie.ofPath(p, v) is equivalent to ParamTrie.of(v).indent(p).

Licence

MIT.