value-store
Get/set values on hierarchy of objects.
For example, a hierarchy of configuration files could be turned into a 'value-store'. Then it can be queried for the closest key with a value, or, for all values for a key.
Note, I'm using this in nuc.
Install
npm install --save value-store
Usage
// require the packagevar buildStore = // we can provide an array of objects as the initial hierarchyvar initialValues = first: true name:'first' second: true name:'second' third: true name:'third' // build the instance, provide the array of initial valuesvar values = // Or:var values = // outputs: values // 3values // truevalues // truevalues // truevalues // truevalues // firstvaluesall'name' // [ 'first', 'second', 'third' ]values // 2 values // falsevalues // { value:true, in:1, overridden:false }values // { value:'first', in:0, overridden:false }values // { value:'second', in:1, overridden:true } valuessource0 // constructorvaluessource5 // undefined // let's change it by setting other values in there // this puts enabled:true into the first object.values; // this overrides the current value in the first objectvalues; // this overrides the current value in the third objectvalues; // add an entirely new object as the last objectvalues; // add an entirely new object as the first object.// this essentially overrides all the others.values; // add values from a file (specify path)values; // rerun the above series and the new output will be: values // 6, was 3 values // 'overrider', was 'first' // now: [ 'overrider', 'primary', 'second', 'tertiary', 'last' ]// was: [ 'first', 'second', 'third' ]valuesall'name' values // 3, was 2 // now: { value:true, in:2, overridden:false }// was: { value:true, in:1, overridden:false }values // now: { value:'overrider', in:0, overridden:false }// was: { value:'first', in:0, overridden:false }values // now: { value:'primary', in:1, overridden:true }// was: { value:'second', in:1, overridden:true }values valuessource0 // prependvaluessource5 // { file: './some/file.json' } // others: shift() is like array.shift(). removes the first array elementvar result = values;// is: {// removed: [// { /* the object with name='overrider' */ }// ]// }// // others: pop() is like array.shift(). removes the last array elementresult = values;// is: {// removed: [// { /* the object with name='overrider' */ }// ]// } // the result is an array because you can shift/pop more than one by// specifying how many:// this removes the first twovalues; // this removes the last twovalues;// so, now there are the 2 middle ones left. // when a file is added via its path the path is recorded as its 'source'values; // then, that object can be written back to its source.// let's say the above append put that file as source 3 (4th in array).// then this would write it back outvalues; // it will be written as json because the extension is .json// to alter where it's written to and the format you may pass options:values; // the above will be written as an INI file because the extension is '.ini'.// you may also specify the format:values;// JSON is the default format. INI is the alternate format.