pathway
v1.0.1
Published
follow key-paths through nested objects
Downloads
16
Maintainers
Readme
pathway
follow key-paths through nested objects
example
var pathway = require('pathway');
var xs = [
{ x : { y : { z : 555 } } },
{ beep : 'boop' },
{ x : { y : { z : 444 } }, w : 4 },
{ x : { y : 'zzz' } },
{ x : { y : { z : 333 } } },
{ X : { y : { z : 222 } } }
];
var ys = pathway(xs, [ true, /x/i, 'y', 'z' ]);
console.dir(ys);
[ 555, 444, 333, 222 ]
methods
var pathway = require('pathway')
pathway(obj, path)
Return an array of all the matching paths through the nested object obj
that
match the key path route path
.
Key paths determine how to proceed deeper into the object for each element. Key paths may contain these kinds of elements:
- string, number - used as raw keys
- RegExp - match keys
- boolean - match all or no keys
- function - match keys with
f(key, value)
, return truthiness - array - match any of these strings or regexes
- object - with
key: true
property, the result of the expression in the output at this key is the array path instead of the value
Some types may select multiple matching results at a given node, in which case all the matching nodes at that level will be followed forward into the result until a later condition isn't satisfied.
This behavior is heavily inspired by how
JSONStream's .parse()
function
works.
install
With npm do:
npm install pathway
notes
This module was written high up in a tree at Mosswood Park.
license
MIT