object-manage
Advanced tools
Comparing version
@@ -114,2 +114,22 @@ 'use strict'; | ||
/** | ||
* Walk the object and return an array of the paths | ||
* @param obj | ||
* @returns {Array} | ||
*/ | ||
var walkTree = function(obj){ | ||
var keys = []; | ||
for(var key in obj){ | ||
if(!obj.hasOwnProperty(key)) continue | ||
keys.push(key) | ||
if('object' === typeof obj[key]){ | ||
var subKeys = walkTree(obj[key]) | ||
for(var i = 0; i < subKeys.length; i++){ | ||
keys.push(key + '.' + subKeys[i]) | ||
} | ||
} | ||
} | ||
return keys | ||
} | ||
/** | ||
* Run validation of an action | ||
@@ -180,9 +200,10 @@ * | ||
* and accepts the same set of parameters | ||
* @param data | ||
* Takes any number of Objects to be loaded and merged at call time | ||
* @constructor | ||
*/ | ||
var ObjectManage = function(data){ | ||
var ObjectManage = function(){ | ||
events.EventEmitter.call(this) | ||
this.data = {} | ||
this.load(data) | ||
//send any arguments to be merged into the main data object | ||
this.load.apply(this,arguments) | ||
//setup the memory driver as the default | ||
@@ -319,2 +340,10 @@ var MemoryDriver = require('../drivers/memory') | ||
/** | ||
* Get the paths of the object in dot notation in an array | ||
* @returns {Array} | ||
*/ | ||
ObjectManage.prototype.getPaths = function(){ | ||
return walkTree(this.data) | ||
} | ||
/** | ||
* Validation of removal directives | ||
@@ -329,11 +358,13 @@ * @type {null} | ||
* in the order they are passed | ||
* @param data | ||
* Takes any number of Objects tobe merged together in order of passing | ||
*/ | ||
ObjectManage.prototype.load = function(data){ | ||
ObjectManage.prototype.load = function(){ | ||
var self = this | ||
if(util.isArray(data)){ | ||
data.forEach(function(v){ | ||
self.load(v) | ||
}) | ||
, data | ||
if(arguments.length > 1){ | ||
for(var i = 0; i < arguments.length; i++){ | ||
self.load(arguments[i]) | ||
} | ||
} else { | ||
data = arguments[0] | ||
if('object' === typeof data){ | ||
@@ -340,0 +371,0 @@ var depth = self.countDepth(data) |
@@ -6,3 +6,3 @@ { | ||
"bugs": "https://github.com/snailjs/object-manage/issues", | ||
"version": "0.6.0", | ||
"version": "0.7.0", | ||
"author": "Bryan Tong <contact@nullivex.com>", | ||
@@ -9,0 +9,0 @@ "license": "MIT", |
@@ -62,2 +62,5 @@ object-manage [](https://travis-ci.org/snailjs/object-manage) | ||
//see paths currently in object | ||
obj.getPaths() // ['foo','foo.test'] | ||
``` | ||
@@ -334,2 +337,6 @@ | ||
**NOTICE** Be careful when changing this as it can have unexpected results on other packages | ||
that may depend on ObjectManage. It would be best to only override the merge value of the | ||
specific instance rather than the prototype. | ||
## Validation | ||
@@ -385,3 +392,3 @@ | ||
The constructor sets up the object to be managed and accepts | ||
the a single argument that gets passed to `ObjectManage.load(data)` | ||
the a arbitrary numbers of arguments that get passed to `ObjectManage.load()` | ||
@@ -407,3 +414,3 @@ ```js | ||
Load will also accept an array of objects that will | ||
Load will also accept an arbitrary numbers of objects that will | ||
be merged on top of each other in order. | ||
@@ -416,11 +423,5 @@ | ||
var inst = new ObjectManage() | ||
inst.load([data1,data2,data3]) | ||
inst.load(data1,data2,data3) | ||
``` | ||
It can even be a recursive array | ||
```js | ||
inst.load([data1,[data2,data3]]) | ||
``` | ||
### Set Value | ||
@@ -483,2 +484,16 @@ | ||
### Get Paths | ||
It may be useful to be able to iterate the paths that are contained within the managed | ||
object. | ||
In order to do this use the `ObjectManage.getPaths()` method. | ||
Example | ||
```js | ||
var obj = new ObjectManage() | ||
obj.load({foo: {bar: 'baz'}}) | ||
obj.getPaths() // ['foo','foo.bar'] | ||
```` | ||
## Events | ||
@@ -696,2 +711,9 @@ | ||
### 0.7.0 | ||
* Fixes #2 load and the constructor now accept multiple arguments rather than an array of objects. | ||
The previous would cause problems when passing an actual array to the object manager. This change breaks | ||
backwards compatibility and will require upgraded code that passes arrays of objects. | ||
* Added feature to print the topology of a managed object in an array of dot separated paths. | ||
Use `ObjectManage.getPaths()` | ||
### 0.6.0 | ||
@@ -698,0 +720,0 @@ * Added support for storage drivers |
@@ -23,3 +23,3 @@ 'use strict'; | ||
it('should be able to merge in data after constructing',function(){ | ||
var obj = new ObjectManage([data1,data2]) | ||
var obj = new ObjectManage(data1,data2) | ||
expect(obj.data.test3).to.equal('val3') | ||
@@ -32,7 +32,7 @@ expect(obj.data.test4).to.equal('val4') | ||
it('should be able to get a nested key',function(){ | ||
var obj = new ObjectManage([data1,data3]) | ||
var obj = new ObjectManage(data1,data3) | ||
expect(obj.get('test5.test6')).to.equal('val6') | ||
}) | ||
it('should be able to set a nested key',function(){ | ||
var obj = new ObjectManage([data1,data3]) | ||
var obj = new ObjectManage(data1,data3) | ||
obj.set('test5.test7','val7') | ||
@@ -42,3 +42,3 @@ expect(obj.data.test5.test7).to.equal('val7') | ||
it('should overwrite a key to object if nested below',function(){ | ||
var obj = new ObjectManage([data1,data2,data3]) | ||
var obj = new ObjectManage(data1,data2,data3) | ||
obj.set('test5.test6.new','val8') | ||
@@ -51,2 +51,29 @@ expect(obj.data.test5.test6.new).to.equal('val8') | ||
}) | ||
it('should return a path tree as an array',function(){ | ||
var obj = new ObjectManage( | ||
data1, | ||
data2, | ||
data3, | ||
{ | ||
test7: { | ||
test8: { | ||
test9: 'foo', | ||
test10: 'bar' | ||
} | ||
} | ||
} | ||
) | ||
expect(obj.getPaths()).to.include.members([ | ||
'test1', | ||
'test2', | ||
'test3', | ||
'test4', | ||
'test5', | ||
'test5.test6', | ||
'test7', | ||
'test7.test8', | ||
'test7.test8.test9', | ||
'test7.test8.test10' | ||
]) | ||
}) | ||
}) | ||
@@ -86,3 +113,3 @@ | ||
it('should remove a property and all its children',function(){ | ||
var obj = new ObjectManage([data1,data2,data3]) | ||
var obj = new ObjectManage(data1,data2,data3) | ||
obj.remove('test5') | ||
@@ -92,3 +119,3 @@ expect(obj.data.hasOwnProperty('test5')).to.equal(false) | ||
it('should reset the data object',function(){ | ||
var obj = new ObjectManage([data1,data2,data3]) | ||
var obj = new ObjectManage(data1,data2,data3) | ||
obj.reset() | ||
@@ -106,3 +133,3 @@ expect(obj.get('test1')).to.equal(undefined) | ||
it('should return the whole object if no argument if passed to get',function(){ | ||
var obj = new ObjectManage([data1,data2,data3]) | ||
var obj = new ObjectManage(data1,data2,data3) | ||
expect(Object.keys(obj.get()).length).to.equal(5) | ||
@@ -109,0 +136,0 @@ }) |
65037
3.86%1442
4.04%765
2.96%