hashtree
Build up "hash of hashes" in javascript and access the data contained.
For documentation of the methods provided see the Doc
The module can be used in Node, AMD / RequireJS or straight in a browser.
Motivation
I'm a big fan of perl hashes. Unfortunately in javascript building up and accessing hash-of-hashes is not as straightforward as in perl. This lib tries to solve this by providing methods to easily build up those.
Interface
Two interfaces are offered either using a prototype function HashTree
or a functional interface using hashTree
both offering the same functionality.
To access the values in the hashtree you use the keys
either with a dot concatenated string or an array.
E.g. 'key.subkey'
or [ 'key', 'subkey' ]
.
Basic Operations
Basic operations use the methods
set
- Sets a value in the hashtree obj according to keys.get
- Gets a value in the hashtree obj according to keys.delete
- Deletes a value in the hashtree obj according to keys.
var ht = hashTree;var robj = "one": 1 ;/// add a new branch `obj.two.three` and set its value to 3 using dot-notation for keysht;// => obj = { one: 1, two: { three: 3 } }/// add a new branch `obj.four.five` and set its value to 5 using array notation for keysht;// => obj = { one: 1, two: { three: 3 }, four: { five: 5 } }/// set objects - note this replaces existing branchesht;// => obj = { one: 1, two: { six: 6 }, four: { five: 5 } }/// get the value stored in `obj.two` using dot-notation for keysr = ht;// => r = { six: 6 }/// get the value stored in `obj.four.five` using array notation for keysr = ht;// => r = 5/// you can also set arraysht;// => obj = { one: 1, two: { three: 3 }, four: { five: 5 }, arr: [ 26, 27, 28 ] }/// and delete branchesht;// => obj = { one: 1, two: { three: 3 }, arr: [ 26, 27, 28 ] }/// or items from arraysht;// => obj = { one: 1, two: { three: 3 }, arr: [ 26, , 28 ] }
Manipulate Values in the hashtree
To manipulate values in the hashtree use the methods
setAll
- Sets all leafes of the hashtree on obj to value.use
- use a reference from the hashtree to make operations upon.sort
- sort the hashtree.
var ht = hashTree;var robj = one: a: 1 b: 2 c: 3 ;r = ht;// => r = true// => obj = { one: { a: 0, b: 0, c: 0 } }
To manipulate values in the hashtree without having to constantly use get and set methods use the use
method.
All operations can be chained.
var ht = hashTree;var r obj = {};r = ht // sets the value to '7'// increments by one// decrements by one// add 10// subtract 5// multiply by 2// divide by 3// modulo 7; // finally get the value// => r = 30// => obj = { one: { a: 30 } }
Sorting a hash tree does not make a lot of sense. It does not change anything. But for human eyes, e.g. on exporting to YAML or JSON, sorted patterns are easier to read (at least for me).
var ht = hashTree;// sort in decending order{return a > b ? -1 : a == b ? 0 : 1;}var robj =a: a: 1 b: 2 c: 2e: a: 1 b: 2 c: 2d: a: 1 b: 2 z: 3;// sort in decending orderr = ht;// => obj = {// e: { c: 2, b: 2, a: 1 },// d: { z: 3, b: 2, a: 1 },// a: { c: 2, b: 2, a: 1 } }
Additionally two hashtrees can be compared with diff
or diffToBase
.
var ht = hashTree; ht;// => { diff1: {one:{two:{b:2}}}, diff2: {one:{two:{b:3}}} } ht;// => {one:{two:{b:3}}}
HashTree
All methods are offered also as a prototype function.
var HashTree = HashTree;var r;var ht = 'one': 1 ;ht;r = ht;// => r = { three: 3 }ht;ht;/// return the hashtree objectr = ht;// => r = { one: 1, three: { four: 4 } }
License
Copyright (c) 2014- commenthol
Software is released under MIT.