diff --git a/README.md b/README.md index e9647c8..c037cf1 100644 --- a/README.md +++ b/README.md @@ -77,7 +77,10 @@ for (;;) { console.log(value); } ``` -### Outputs +## Outputs + +### [Default options](#traversejsonoptions--object) + __{}__ ``` [ '/foo', 0 ] @@ -87,6 +90,10 @@ __{}__ [ '/nested/nested/nested/nested/depth', 4 ] [ '/bar', 1 ] ``` + + +### Return eather the nested and flatten values + __{ [nested](#traversejsonoptions--object): true }__ ``` [ '/foo', 0 ] @@ -102,6 +109,10 @@ __{ [nested](#traversejsonoptions--object): true }__ [ '/nested/nested/nested/nested/depth', 4 ] [ '/bar', 1 ] ``` + + +### Only traverse on depth 1 + __{ [recursive](#traversejsonoptions--object): false }__ ``` [ '/foo', 0 ] @@ -109,11 +120,19 @@ __{ [recursive](#traversejsonoptions--object): false }__ { depth: 1, nested: { depth: 2, nested: [Object] } } ] [ '/bar', 1 ] ``` + + +### Skips even entries + __{ [step](#traversejsonoptions--object): 2 }__ ``` [ '/foo', 0 ] [ '/bar', 1 ] ``` + + +### Match only the paths ending with _depth_ + __{ [test](#traversejsonoptions--object): /depth$/ }__ ``` [ '/nested/depth', 1 ] @@ -121,7 +140,11 @@ __{ [test](#traversejsonoptions--object): /depth$/ }__ [ '/nested/nested/nested/depth', 3 ] [ '/nested/nested/nested/nested/depth', 4 ] ``` -__{ [test](#traversejsonoptions--object): /nested$/, nested: true }__ + + +### Return eather the nested and flatten values ending with _nested_ + +__{ [test](#traversejsonoptions--object): /nested$/, [nested](#traversejsonoptions--object): true }__ ``` [ '/nested', { depth: 1, nested: { depth: 2, nested: [Object] } } ] @@ -130,6 +153,10 @@ __{ [test](#traversejsonoptions--object): /nested$/, nested: true }__ [ '/nested/nested/nested', { depth: 3, nested: { depth: 4 } } ] [ '/nested/nested/nested/nested', { depth: 4 } ] ``` + + +### Match only the paths ending with _foo_ or _depth_ + __{ [test](#traversejsonoptions--object): "**/{depth,foo}" }__ ``` [ '/foo', 0 ] @@ -138,11 +165,28 @@ __{ [test](#traversejsonoptions--object): "**/{depth,foo}" }__ [ '/nested/nested/nested/depth', 3 ] [ '/nested/nested/nested/nested/depth', 4 ] ``` + + +### Match entries which has a number value equal or greater than 3 + __{ [test](#traversejsonoptions--object): ([,value]) => typeof value === 'number' && value >= 3 }__ ``` [ '/nested/nested/nested/depth', 3 ] [ '/nested/nested/nested/nested/depth', 4 ] ``` + + +### Traverse recursively through the same key + +__{ [test](#traversejsonoptions--object): "@nested" }__ +``` +[ '/nested', + { depth: 1, nested: { depth: 2, nested: [Object] } } ] +[ '/nested/nested', + { depth: 2, nested: { depth: 3, nested: [Object] } } ] +[ '/nested/nested/nested', { depth: 3, nested: { depth: 4 } } ] +[ '/nested/nested/nested/nested', { depth: 4 } ] +``` ## createIterator(obj, [opts]) ⇒ Iterable @@ -195,12 +239,12 @@ for (let [k, v] of ientries) { **Kind**: global typedef **Properties** -| Name | Type | Description | -| --- | --- | --- | -| [opts.recursive] | Boolean | enable/disable nested arrays and objects recursion | -| [opts.nested] | Boolean | also emit nested array or objects | -| [opts.step] | Boolean | the step to increment, default 1 | -| [opts.test] | String \| function \| RegeExp | regexp, string [minimatch](https://www.npmjs.com/package/minimatch) or function to filter properties | +| Name | Type | Default | Description | +| --- | --- | --- | --- | +| [opts.recursive] | Boolean | true | enable/disable nested arrays and objects recursion | +| [opts.nested] | Boolean | false | also emit nested array or objects | +| [opts.step] | Boolean | 1 | the step to increment, default 1 | +| [opts.test] | String \| function \| RegeExp | | regexp, string [minimatch](https://www.npmjs.com/package/minimatch) or function to filter properties | diff --git a/index.js b/index.js index a986536..1f042cc 100644 --- a/index.js +++ b/index.js @@ -9,10 +9,10 @@ const { /** * @typedef {Object} TraverseJsonOptions - * @prop {Boolean} [opts.recursive] enable/disable nested arrays and objects recursion - * @prop {Boolean} [opts.nested] also emit nested array or objects - * @prop {Boolean} [opts.step] the step to increment, default 1 - * @prop {String|Function|RegeExp} [opts.test] regexp, string [minimatch](https://www.npmjs.com/package/minimatch) or function to filter properties + * @prop {Boolean} [opts.recursive=true] enable/disable nested arrays and objects recursion + * @prop {Boolean} [opts.nested=false] also emit nested array or objects + * @prop {Boolean} [opts.step=1] the step to increment, default 1 + * @prop {String|Function|RegeExp} [opts.test=null] regexp, string [minimatch](https://www.npmjs.com/package/minimatch) or function to filter properties */ /** @@ -69,7 +69,10 @@ const { * console.log(value); * } * ``` - * ### Outputs + * ## Outputs + * + * ### [Default options](#traversejsonoptions--object) + * * __{}__ * ``` * [ '/foo', 0 ] @@ -79,6 +82,10 @@ const { * [ '/nested/nested/nested/nested/depth', 4 ] * [ '/bar', 1 ] * ``` + * + * + * ### Return eather the nested and flatten values + * * __{ [nested](#traversejsonoptions--object): true }__ * ``` * [ '/foo', 0 ] @@ -94,6 +101,10 @@ const { * [ '/nested/nested/nested/nested/depth', 4 ] * [ '/bar', 1 ] * ``` + * + * + * ### Only traverse on depth 1 + * * __{ [recursive](#traversejsonoptions--object): false }__ * ``` * [ '/foo', 0 ] @@ -101,11 +112,19 @@ const { * { depth: 1, nested: { depth: 2, nested: [Object] } } ] * [ '/bar', 1 ] * ``` + * + * + * ### Skips even entries + * * __{ [step](#traversejsonoptions--object): 2 }__ * ``` * [ '/foo', 0 ] * [ '/bar', 1 ] * ``` + * + * + * ### Match only the paths ending with _depth_ + * * __{ [test](#traversejsonoptions--object): /depth$/ }__ * ``` * [ '/nested/depth', 1 ] @@ -113,7 +132,11 @@ const { * [ '/nested/nested/nested/depth', 3 ] * [ '/nested/nested/nested/nested/depth', 4 ] * ``` - * __{ [test](#traversejsonoptions--object): /nested$/, nested: true }__ + * + * + * ### Return eather the nested and flatten values ending with _nested_ + * + * __{ [test](#traversejsonoptions--object): /nested$/, [nested](#traversejsonoptions--object): true }__ * ``` * [ '/nested', * { depth: 1, nested: { depth: 2, nested: [Object] } } ] @@ -122,6 +145,10 @@ const { * [ '/nested/nested/nested', { depth: 3, nested: { depth: 4 } } ] * [ '/nested/nested/nested/nested', { depth: 4 } ] * ``` + * + * + * ### Match only the paths ending with _foo_ or _depth_ + * * __{ [test](#traversejsonoptions--object): "**/{depth,foo}" }__ * ``` * [ '/foo', 0 ] @@ -130,11 +157,19 @@ const { * [ '/nested/nested/nested/depth', 3 ] * [ '/nested/nested/nested/nested/depth', 4 ] * ``` + * + * + * ### Match entries which has a number value equal or greater than 3 + * * __{ [test](#traversejsonoptions--object): ([,value]) => typeof value === 'number' && value >= 3 }__ * ``` * [ '/nested/nested/nested/depth', 3 ] * [ '/nested/nested/nested/nested/depth', 4 ] * ``` + * + * + * ### Traverse recursively through the same key + * * __{ [test](#traversejsonoptions--object): "@nested" }__ * ``` * [ '/nested',