1.2.2 • Published 8 years ago
any-leaf v1.2.2
any-leaf
any-leaf provides utility methods for traversing a JSON object and performing checks or returning values from leaf nodes. This library has not dependencies
Installation:
npm install --save any-leafUsage:
import { getLeaves } from "any-leaf";
const test = {
a: {
i: {
p: [ 'value1', { test: 'value2'}]
q: {
x: 12,
y: null
}
}
},
b: [
{ i: 'value3' },
{ i: 'value4', j: ['value5']}
]
};
const result = getLeaves
console.log(JSON.stringify())
//[
// {
// "path": "a.i.p.0",
// "value": "value1"
// },
// {
// "path": "a.i.p.1.test",
// "value": "value2"
// },
// {
// "path": "a.i.q.x",
// "value": 12
// },
// {
// "path": "a.i.q.y",
// "value": null
// },
// {
// "path": "b.0.i",
// "value": "value3"
// },
// {
// "path": "b.1.i",
// "value": "value4"
// },
// {
// "path": "b.1.j.0",
// "value": "value5"
// }
//]API:
In addition to the documenation below, each method takes in an optional include ([string]) and exclude ([string]) parameter used to include or exclude subtree sections of the subject object. const include = [ 'a.i.p', 'b'] // same for exclude, will exclude all nodes under the listed pathsanyLeaf(value: object, fn: function): boolean
performs the check defined by fn on all leaf nodes and returns true if any leaf node passes the check (similar to Array.prototype.some). The fn callback will be called with each value in the format: { path: string, value: any }anyLeaf(test, leaf => leaf.value === 'value1') // true
anyLeaf(test, leaf => leaf.value === 'someValNotALeaf') // falseallLeaves(value: object, fn: function): boolean
same as anyLeaf except all leaves must pass the check defined by the callback fnallLeaves(test, leaf => leaf.value === 'value1') // false
allLeaves(test, leaf => leaf.value !== undefined) // trueanyLeafTruthy(value: object): boolean
same as anyLeaf with a callback fn specified as leaf => !!leaf.value
anyLeafTruthy(test) // trueallLeavesTruthy(value: object): boolean
same as allLeaves with a callback fn specified as leaf => !!leaf.value
allLeavesTruthy(test) // false, a.i.q.y === nullanyLeafFalsey(value: object): boolean
same as anyLeaf with a callback fn specified as leaf => !leaf.value
anyLeafFalsey(test) // true, a.i.q.y === nullallLeavesFalsey(value: object): boolean
same as allLeaves with a callback fn specified as leaf => !leaf.value
allLeavesFalsey(test) // falsegetLeaves(value: object): {path: string, value: any}
same as getLeaves with a callback fn specified as leaf => leaf
getLeaves(test) // falsemapLeaves(value: object, fn: function): any
visits each leaf node in the object and runs the callback function on each leaf node in the object and returns an array of values containing the mapped objects
mapLeaves(test, leaf => ( myProp: 'my ' + leaf.value )) visitLeaves(value: object, fn: function)
visits each leaf node in the object and runs the callback function on each leaf node in the object
const myArray = []
visitLeaves(test, leaf => myArray.push(leaf))