Giter Site home page Giter Site logo

prefix-tree's Introduction

prefix-tree

Simple prefix-tree

NPM Status Travis Status Coverage Status Dependency Status

Class to work with prefix tree. API is similiar to native Map.

Installation

npm install --save prefix-tree

Usage

var Tree = require('prefix-tree');

var tree = new Tree([['hi', 42,] ['hello', 'world'], ['xo', 'xo']]);

tree.get('h');
// → [ 42, 'world']

tree.set('xxx', { '42': 42 });
tree.get('x');
// → [ 'xo', { '42': 42 }]

API

prefixTree(items)

Parameter Type Description
items array optional Array of key-value pairs

Example:

var tree = new PrefixTree([ ['key', 'value'], ['key2', 'value2'] ]);
var tree2 = new PrefixTree();

set(key, value)

Parameter Type Description
key string key to search prefix in
value any Anything you want to store

Add value to prefix tree.

Example:

var tree = new PrefixTree();
tree.set('hello', 'world');

get(prefix)

Parameter Type Description
prefix string prefix to search values

Get values for a prefix.

Example:

var tree = new PrefixTree();

tree
    .set('hell', 666);
    .set('hello', 'world');

tree.get('he');
// → [666, 'world']

toString()

For debug purpose you could use toString() method.

NB For perfomance module load inspection only with NODE_ENV === 'development'

NODE_ENV='development' node -e "console.log('' + new (require('prefix-tree'))([['hello', 'hello'], ['hi', 'hi'], ['hell', 'hell']]))"

[root]
└── h
    ├── e
    │   └── l
    │       └── l : hell
    │           └── o : hello
    └── i : hi

License

Code released under the MIT.

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.