create trees as simple as path('a/b/c')!

treelib : create tree's as easy as .path('a/b/c')

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


// creates a nested tree
> { a : { b : { c : 'foobar' }}}


path takes a string or array to indicate a path, i.e. 'a/b/c' refers to this path {a : {b : { c : '' }}}

Treelib creates the path if it doesn't exist.



> {foo: {bar: {baz: 'PERL!'}}}

tree.path('foo/bar/bitter').setValue('Randal Schwartz');

> {foo: {bar: {baz: 'PERL!', bitter: 'Randal Schwartz'}}}

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


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

> {Music:{'The Smiths':{price:'$12.99',SKU:'24142'}}}

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 getValue is called without arguments, it 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, i.e. clears the leaf value.

example: tree.path('a/b/c'); tree.checkPath('a/b/cauliflower');

// returns

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

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

Show the tree in the console.

Returns the tree itself.

Returns the 50 most recent history of path creation and setValues executions.


to run tests, nodeunit test/

testing results from ./test/test.js (nodeunit)

$ nodeunit *

✔ testBasics
✔ testOverwrite
✔ testBranching
✔ testBranching2
✔ testSetAndClearValues

OK: 14 assertions (13ms)

node-treelib Copyright (c) 2010 David Wee

Free software provided under the MIT License