JSObjects
This small library allows you to manipulate objects via string path. It can be used in node.js or browser or any place where javascript can be run.
Node.js
npm i jsobjects --save
; // Will use this objects to show ussage in examplesconst arrstr = 'string 1' 'string 2';const arrobj = arrobjkey1: 'arrobj1key1' 'arr obj key 2': 'arr obj 1 key 2' arrobjkey1: 'arrobj2key1' 'arr obj key 2': 'arr obj 2 key 2' ;const object = key1: 'key1' 'key 2': 'key 2' arrstr arrobj obj: objkey1: 'objkey1' 'obj key 2': 'obj key 2' objarrstr: arrstr objarrobj: arrobj arr: arrkey1: 'arr1key1' 'arr key 2': 'arr 1 key 2' arrarrstr: arrstr arrarrobj: arrobj arrkey1: 'arr2key1' 'arr key 2': 'arr 2 key 2' arrarrstr: arrstr arrarrobj: arrobj ;
browser
bower install jsobjects --save
See demo in ./demo
folder.
// This module exposes require from browserifyvar jsobjects = ; // Will use this objects to show usage in examplesvar arrstr = 'string 1' 'string 2';var arrobj = arrobjkey1: 'arrobj1key1' 'arr obj key 2': 'arr obj 1 key 2' arrobjkey1: 'arrobj2key1' 'arr obj key 2': 'arr obj 2 key 2' ;var object = key1: 'key1' 'key 2': 'key 2' arrstr: arrstr arrobj: arrobj obj: objkey1: 'objkey1' 'obj key 2': 'obj key 2' objarrstr: arrstr objarrobj: arrobj arr: arrkey1: 'arr1key1' 'arr key 2': 'arr 1 key 2' arrarrstr: arrstr arrarrobj: arrobj arrkey1: 'arr2key1' 'arr key 2': 'arr 2 key 2' arrarrstr: arrstr arrarrobj: arrobj ;
API
getObjectByPath
getObjectByPath(object, path, pathReplacements, offset)
Given an object and a path or generic path, returns the value contained in the object at the specified path.
object
: The object to search forpath
path
: The path to be expanded, accepts.*
notationpathReplacements
: Every*
in path will be replaced by corresponding in pathReplacements array:('arr.*.arrarrobj.*.arrkey1', [0, 1])
will be assimilated toarr.0.arrarrobj.1.arrkey1
offset
: Number of paths to skip at the end:('arr.*.arrarrobj.*.arrkey1', [0, 1], 3)
will be assimilated toarr.0
Examples
// These statements evals to true === objectarr0arrarrobj0arrobjkey1; === objectarr0arrarrobj1'arr obj key 2'; === objectarr0arrarrobj1'arr obj key 2'; === objectarr1arrarrobj1;
expandPath
expandPath(object, path, callback)
Given an object and a generic path, returns an array with all the paths found in the object.
Complex keys (not single word) allows to be refferenced with ["name of the key"]
or ['name of the key']
.
object
: The object to search forpath
path
: The path to be expanded, accepts.*
notationcallback
: (Optional) a callback function that will be called for each entry found
Examples
; // will output: ['arrstr.0', 'arrstr.1'];; // will output: ['arrstr.0', 'arrstr.1']; ; // will output: ['arrstr.0', 'arrstr.1']; // will output: ['arrobj.0', 'arrobj.1'] ; // will output: ['obj.objarrstr.0', 'obj.objarrstr.1']; // will output: ['obj.objarrobj.0', 'obj.objarrobj.1'] ; // will output: ['obj.objarrobj.0.arrobjkey1', 'obj.objarrobj.1.arrobjkey1']; // will output: ['obj.objarrobj.0.["arr obj key 2"]', 'obj.objarrobj.1.["arr obj key 2"]'] ; // will output: ['arr.0', 'arr.1'] ; // will output: ['arr.0.arrkey1', 'arr.1.arrkey1']; // will output: ['arr.0.["arr key 2"]', 'arr.1.["arr key 2"]'] ; // will output: ['arr.0.arrarrstr', 'arr.1.arrarrstr']; // will output: ['arr.0.arrarrstr.0', 'arr.0.arrarrstr.1', 'arr.1.arrarrstr.0', 'arr.1.arrarrstr.1']; // will output: ['arr.0.arrarrobj.0', 'arr.0.arrarrobj.1', 'arr.1.arrarrobj.0', 'arr.1.arrarrobj.1'] ; // will output: ['arr.0.arrarrobj.0.arrkey1', 'arr.0.arrarrobj.1.arrkey1', 'arr.1.arrarrobj.0.arrkey1', 'arr.1.arrarrobj.1.arrkey1']; // will output: ['arr.0.arrarrobj.0.["arr key 2"]', 'arr.0.arrarrobj.1.["arr key 2"]', 'arr.1.arrarrobj.0.["arr key 2"]', 'arr.1.arrarrobj["arr key 2"]'])); ; // will output: []; // will output: []; // will output: []; // will output: []; // will output: ['arr.0.unexistingkey', 'arr.1.unexistingkey']; // will output: [] ; // will output: ['arr.0.arrarrstr', 'arr.1.arrarrstr']// cb function will be called twice with the followint arguments:// First call: cb('arr.0.arrarrstr', ['string 1', 'string 2'])// Second call: cb('arr.1.arrarrstr', ['string 1', 'string 2'])
updateObjectByPath
updateObjectByPath(object, path, newValue, pathReplacements)
Update the object at a given path with a specified value.
object
: The object to search forpath
path
: The path to be expanded, accepts.*
notationnewValue
: The new value to be assignedpathReplacements
: Every*
in path will be replaced by corresponding in pathReplacements array:('arr.*.arrarrobj.*.arrkey1', [0, 1])
will be assimilated toarr.0.arrarrobj.1.arrkey1
Examples
; // This statement evals to true === 'new value';