object-manage ![Build Status](https://travis-ci.org/snailjs/object-manage.png?branch=master)
A library for managing javascript objects and offering common getter, setter, merge support.
Works great for managing config objects and libraries that have options.
Installation
$ npm install object-manage
Usage
This helper is generally meant to be implemented into higher level API's. As such usage is
simple.
var ObjectManage = require('object-manage')
, obj = new ObjectManage({box: 'square'})
obj.load({foo: 'bar'})
obj.set('bas.boo','foo1')
obj.get('bas')
obj.get('bas.boo')
console.log(obj.data.bas.boo)
obj.exists('bas')
obj.exists('badkey')
obj.remove('bas')
obj.exists('bas')
API Reference
Constructor
The constructor sets up the object to be managed and accepts
the a single argument that gets passed to ObjectManage.load(data)
Load Object(s)
Load is used to merge an argument object into the main object.
var inst = new ObjectManage()
inst.load({mykey: 'mydata'})
inst.load({mykey2: 'mydata2'})
Load will also accept an array of objects that will
be merged on top of each other in order.
var data1 = {test1: 'val1', test2: 'val2'}
, data2 = {test3: 'val3', test4: 'val4'}
, data3 = {test5: {test6: 'val6'}}
var inst = ObjectManage()
inst.load([data1,data2,data3])
It can even be a recursive array
inst.load([data1,[data2,data3]])
Set Value
Set will recursively set a path given by a string using dot notation.
var isnt = new ObjectManage()
inst.set('mykey','mydata')
inst.set('mykey2.data','mydata')
Get Value
Get will recursively set a path given by a string using dot notation.
var isnt = new ObjectManage({mykey: 'mydata', mykey2: {data: 'mydata'}})
inst.get('mykey')
inst.get('mykey2.data')
Path Exists
Check if a path exists
NOTE This uses hasOwnProperty()
method of the object so is safe
to return an accurate value even when a path is set to undefined
var inst = new ObjectManage({mykey: 'myvalue'})
inst.exists('mykey')
inst.exists('mykey2')
Remove a Path
Remove a path and all its children
This uses delete object[property]
and does not just set the property
to undefined
var inst = new ObjectManage({mykey: {mykey2: 'myvalue'}})
inst.exists('mykey.mykey2')
inst.remove('mykey')
inst.exists('mykey.mykey')
Check Object Depth
This will take a userspace object and count the depth of the object.
var inst = new ObjectManage()
var obj = {foo: {foo: {foo: 'baz'}}}
obj.countDepth(obj)
Changelog
0.4.0
- Added max depth warning for recursive objects that would normally throw
Maximum call stack exceeded
- Added extensive testing against object size testing and warnings
- Even if maxDepth is exceeded the merge will still be attempted but is likely to fail with a
Maximum call stack exceeded
error - Max depth is adjustable using the
ObjectManage.maxDepth
property - Added
ObjectManage.countDepth(object)
to check object depth - Changed merging package to merge-recursive this
will now maintain associations to parent objects so that libraries can establish a pointer to the managed object
eg:
var obj = new ObjectManage(); var watched = obj.data
0.3.0
- No argument to
get()
now returns the entire object - Testing added against recursive merging
0.2.3
- Small fix to make sure
ObjectManage.data
is always an object
0.2.2
- Changed from merge to object-merge to allow for recursive merging
- Made private functions more robust to non-sane input
0.2.1
0.2.0
- Added
ObjectManage.exists()
- Added
ObjectManage.remove()
0.1.0
License
MIT licensed see LICENSE.md