0.0.4 • Published 9 years ago
treestruct v0.0.4
treestruct
Makes working with unstructured data in JavaScript more convenient and elegant. With this tree structure, you can access your data without worrying about property initialization or error checking.
Installation
$ npm install treestruct --save
Usage
var tree = new TreeStruct();
tree.set('root', 1);
tree.get('root'); // returns: 1
tree.set('some', 'random', 'element', 42);
tree.get('some', 'random', 'element'); // returns: 42
tree.keys(); // returns: ['root', 'some']
tree.keys('some'); // returns: ['random']
tree.push('some', 'array', 'oak');
tree.push('some', 'array', 'pine');
tree.get('some', 'array'); // returns: ['oak', 'pine']
tree.get('does', 'this', 'value', 'exist?'); // returns: null
API
set(node, node, ..., value)
get(node, node, ...)
// Set a value to a given node
tree.set('any', 'name', 'here', 42);
// Get the value back
tree.get('any', 'name', 'here'); // returns: 42
getArray(node, node, ...)
// Set a value to a given node
tree.set('list', 'oak');
// getArray() ensures, that you always get the value back as an array
tree.getArray('list'); // returns: ['oak']
// Even if you ask for an inexistent value
tree.getArray('empty', 'list'); // returns: []
push(node, node, ..., value)
// Push a value to a any node. It will be convereted to an array automatically
tree.push('array', 'first');
// Push another item
tree.push('array', 'second');
// Get the value back
tree.get('array'); // returns: ['first', 'second']
increment(node, node, ..., value)
// Set a node to a numeric value
tree.set('numeric', 'value', 10);
// Increment the value
tree.increment('numeric', 'value');
// Get the value back
tree.get('numeric', 'value'); // returns: 11
add(node, node, ..., value)
// Set a node to a numeric value
tree.set('numeric', 'value', 10);
// Add a given number to the value
tree.add('numeric', 'value', 10);
// Get the value back
tree.get('numeric', 'value'); // returns: 20
keys(node, node, ...)
tree.set('root', 'child one', 1);
tree.set('root', 'child two', 2);
tree.set('root', 'child three', 3);
// Get the existing keys of root nodes
tree.keys(); // returns: ['root']
// Get the existing keys of nodes under 'root'
tree.keys('root'); // returns: ['child one', 'child two', 'child three']