Code Monkey home page Code Monkey logo

paramtrie's Introduction

param-trie
Build status

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.

paramtrie's People

Contributors

apaleslimghost avatar

Stargazers

 avatar Jeff Moore avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.