Shadow Util
A simple utility package for shadow packages. Feel free to use this if you find any of this useful.
String property accessors
JS does not allow you got get nested object properties with a single string, string paths, (e.g. 'a.b.c'). These functions all work on the process of string paths.
ShadowUtil.getPropertyByString(object, stringPath)
1ShadowUtil.getPropertyByString({a: {b: 'test'} }, 'a.b') //returns 'test'
ShadowUtil.setPropertyByString(object, stringPath, defaultValue)
1var obj = {a: {b: 'test'} }; 2ShadowUtil.setPropertyByString(obj, 'a.c', 'other test') //returns 'other test' 3obj.a.c //returns 'other test'
ShadowUtil.getSubObjectByArray(object, stringPath, force, current)
This is used by get/setPropertyByString and is a recursive function. It will return a object and key that points to the target object depth. If force is set to true it will create that path if it is not defined. If it is not set to true then will return a noop object that has no pointer. If the path exists as a non object it will throw an error. Current is used for recursion and should not be set.
1var obj = {} 2var pointerObj = ShadowUtil.getSubObjectByArray(obj, 'a.b', true) 3// returns {obj: {b: undefined}, key: 'b'} 4obj.a.b //exists as undefined 5pointerObj.obj[pointerObj.key] = 'test' 6obj.a.b //returns 'test'
ShadowUtil.getBlacklistKeys(doc, whitelist)
This is a deep omit function that returns blacklisted keys as string paths.
1var obj = { a:'a', b: {a: 'a', b: 'b'}, c: {a: 'a'}, d: 'd' } 2ShadowUtil.getBlacklistKeys(obj, ['b.a', 'd']) //returns ['a', 'b.b', 'c']
Persistence Helpers
These functions mock database actions and return the would be results.
ShadowUtil.MockUpdate(doc, modifier, options)
1var post = {_id: 'someIdString', name: 'bad name', active:true} 2ShadowUtil.MockUpdate(post, { $set: {name: 'good name'} }) 3//returns {_id: 'someIdString', name: 'good name', active:true}
TODO
- Tests