Object-Scan
Find Keys using Wildcard matching and optional value function.
Install
Install with npm:
$ npm install --save object-scan
Usage
const objectScan = require('object-scan');
objectScan(["a.*.f"])({ a: { b: { c: 'd' }, e: { f: 'g' } } });
Options
filterFn
Type: function
Default: undefined
Takes arguments key
(dot joined and escaped) and value
(value for given key) and called for every potential result.
If function is defined and returns false, the entry is filtered from the result.
breakFn
Type: function
Default: undefined
Takes arguments key
(dot joined and escaped) and value
(value for given key) and called for every potential result.
If function is defined and returns true, no nested entries are checked.
joined
Type: boolean
Default: true
Can be set to false to return each key as a list. When dealing with special characters this can be useful.
Examples
More extensive examples can be found in the tests.
const objectScan = require('object-scan');
const obj = {
a: {
b: {
c: 'd'
},
e: {
f: 'g'
},
h: ["i", "j"]
},
k: "l"
};
objectScan(["*"])(obj);
objectScan(["a.*.f"])(obj);
objectScan(["*.*.*"])(obj);
objectScan(["a.*.{c,f}"])(obj);
objectScan(["a.*.{c,f}"], { joined: false })(obj);
objectScan(["*.*[*]"])(obj);
objectScan(["*[*]"])(obj);
objectScan(["**"])(obj);
objectScan(["**.f"])(obj);
objectScan(["**[*]"])(obj);
objectScan(["**"], { filterFn: (key, value) => typeof value === "string" })(obj);
objectScan(["**"], { breakFn: key => key === "a.b" })(obj);
Special Characters
The following Characters are considered special and need to
be escaped if they should be matched in a key: [
, ]
, {
, }
, ,
, .
and *
.
When dealing with special characters the joined
option might be desirable to set to false
.