simple-object-query
npm install simple-object-query
bower install simple-object-query
Really simple lib to find a deep value in an object
I'll use this source object for all examples
var source = data: item: name: 'select' options: length: 4 property: name: 'input' item: name: 'group' options: length: 2 property: name: 'input' type: 'number' ;
get
This function will return value of deep field if it is own property. You can use *
if you don't know exact index in array.
var get = get; ; // 'select'; // 'group'; // 'number'
Basically you will use this method only when you need a *
because without it you can get value just with regular code.
where
This is filter function for array. It will return all items which deep fields will be equal to query values. You can use regular expression to test deep field value.
var where = where; ;/* [ { item: { name: 'group', type: 'number', options: {...}, } } ]*/
You can do even more complicated query with array of queries. Items of this array can be one of three types:
Object
- regular queryFunction
- should be map function which will return new value instead of previous resultString
- shortcut for map function withget
function with current query string(item) => get(item, string)
var src = root: node: a: b: 1 a: c: 2 a: d: 3 g: e: 3f: 4 a: d: 3 g: e: 5f: 5 a: d: 4 g: e: 3f: 4 a: d: 4 g: e: 5f: 5 ; var q = _ = ; q;/* [ {a: {d: 3, g: [{e: 5},{f: 5}]}} ]*/
find
This function will recursively find a deep object which has deep fields as in query object and their values are equal to values from query object. As values in query object you can use regular expressions.
var find = find; ;/* [ {name: 'select', options: {...}}, {name: 'group', options: {...}} ]*/ ;/* [ {name: 'group', options: {...}} ]*/ ;/* [ {name: 'select', options: {...}}, {name: 'group', options: {...}} ]*/
search
Difference between find
is that it takes parameters as object of type
source: object query: object // same object as for "find" exclude: array // array of names of properties which are links to other objects in source (circular links) recursion: true // deep search or not {} // optional callback for each found target
If you will not set callback then search
will return array of objects of next type
parent: object // link to parent object field: 'string' // name of parent property of target object target: object // searched object path: 'path' 'to' 'object' // this path means that target is in source.path.to.object property
Warning: if your input object has circular links (like parent
fields or like previousSibling
in DOM) then you should set path to this fields in exclude
array to prevent endless recursion.
var search = search; ;/* [ { parent: {item: {...}}, field: 'item', path: ['data', '0', 'item'], target: {name: 'select', options: {...}} }, { parent: {item: {...}}, field: 'item', path: ['data', '1', 'item'], target: {name: 'group', options: {...}} } ]*/ sourcedata0itemlist = sourcedata; ;/* [ { parent: {name: 'group', options: {...}}, field: 'options', path: ['data', '1', 'item', 'options'], target: {length: 2, property: {...}} } ]*/
replace
This method will replace or remove (if callback will return undefined
) target object.
var replace = replace; ; console; // 'test'console; // false
Off course if you don't want to remove target
just return itself.
Instead of callback you can pass some value.
; console; // 'test'console; // 'test'
Or if you will not pass anything it will remove all targets
; console; // falseconsole; // false
If your target
is in array, it will be removed correctly
; console; // 1console; // 'group'
If you need set exclude
parameter then you should pass all parameters as object just like for search
only with callback
parameter
;