node package manager


Build Status

treelib : search and create trees as easy as .path('a/b/c')

var Treelib = require('treelib');
var tree = Treelib();


var x = tree.getTree();
// creates a nested tree
> x = { a : { b : { c : 'foobar' }}}

Main Methods path and search


'a/b/c' creates object {a : {b : { c : undefined }}}

.search(match, onFinish)

Performs a Depth-First-Search for value. Call with match function which in turn, will be called on each node it visits, and onFinish which will be called with true or false if the match function succeeds. {
  return (val == 'foobar')
}, function(isFound) {
  // isFound == true

Helper methods


increments a counter at path and returns the counter value


setValue sets a value on the current path. The current path is set after .path is called.


getValue takes a path in the form of a string or array and returns the value there.

tree.getValue('Music/The Smiths'); // {price:'$12.99',SKU:'24142'}

If the path doesn't exist or there is no value set at that path, then it returns undefined.

If you want to get the root simply

tree.getValue('/') or tree.getValue('')


returns the value at the current path.

tree.path('Music/The Smiths').getValue(); // {price:'$12.99',SKU:'24142'}


clearValue takes a path in the form of a string or array and clears the value at the end of that path


checkPath takes a path in the form of a string or array and returns how far it could walk down the tree.


// {depth: 2, validPath: [ 'a', 'b' ] }


Show the tree in the console.


Returns the tree itself.


Sets the tree

Additional Helper Functions


Returns a descriptive tree when passed a hierachical tree.

node-treelib Copyright (c) 2010-2017 David Wee MIT Free software provided under the MIT License