json-collection-decorator
efficiently run a collection of records by conditions and decorate them when conditions match, return results - filtered items
Inputs
- config object
- collection of jsonobjects
var objConfig={
filters:[
{ path:"path.to.key",op:"eq",val:"value to match" }
]
,decorate:[
{
find:[{ path:"path.to.key",op:"eq",val:"value to match" }]
,do:[{ path:"path.to.key",act:"set",val:"value to set" }]
},{
,find:{ path:"path.to.key",op:"eq",val:"value to match" }
,do:{path:"path.to.key",act:"stack",val:"value to add to array" }
}
]
}
arrResults = decorate(objConfig,arrCollection);
multiple matching conditions
multiple conditions in an array are treated as an AND, they ALL must match. OR conditions can be listed as a separate line. Multiple actions are ALL executed
Action Parameters
- filter : conditions to meet for filtering out the object from the collection to return
- decorate: pairs of conditions and decorator actions to perform on each object in the collection
- decorate.find: the condiion for a decoration
- decorate.act: the action to perform on hat is matched
Comparison Parameters
- path : dot notation path to the field
- op : operand
- val : stauc value to compare against
- path2 : used for comparisons between values instead of setting a static one
Operands
- eq : ==
- ne : !=
- gt : >
- lt : <
- in : found in a string or simple array
- ni : opposite of in
- has : more than a count of occurences of in
- any: doesnt matter
- empty: no data, null, or undefined
- data: populated field
- find: run a nested comparison, comparison will be in val:
Actions
- log: console.log what was found
- set: static value to set
- stack: static value to add to an array
- unstack: remove an exact value from an array
- add: numeric value to add, this will also convert existing values to a number
- append: string to append
- prepend: string to append
- remove: remove the field from the object
- rename: rename a field of an object
- prioritize: adds 1 to existings _priority, unless a different number is specified
- focus: array of paths to keep, remove all others ['path1', 'path2.child']
- implode: convert an array into a comma separated string
- explode: convert a value separated string into an array
- copy: get the value from path deinfed in val: copy it to path:
- findCopy same as copy but has 2 paths in val to find items in an array {path:'_image',act:'findCopy',val:{path:'preview.images',path2:'source.url'}}
- parseUrl: add properties to a destination val: from a parsed path requires url-parse repo library included
19: rand: set a random integrer given a min and max {path:,val:[min,max]}