[Pkg-javascript-commits] [node-jsonstream] 95/214: Updated recursive descent functionnality with an array representation and documentation.
Bastien Roucariès
rouca at moszumanska.debian.org
Fri Dec 1 12:58:45 UTC 2017
This is an automated email from the git hooks/post-receive script.
rouca pushed a commit to branch master
in repository node-jsonstream.
commit da3825101e019711d4b812360adc1eef2cde611d
Author: Paul Mougel <paul.mougel at atos.net>
Date: Fri Aug 30 12:07:02 2013 +0200
Updated recursive descent functionnality with an array representation and documentation.
---
index.js | 9 +++++++--
readme.markdown | 26 ++++++++++++++++++++++++--
test/doubledot2.js | 2 +-
3 files changed, 32 insertions(+), 5 deletions(-)
diff --git a/index.js b/index.js
index a6e2904..99f7674 100644
--- a/index.js
+++ b/index.js
@@ -34,7 +34,12 @@ exports.parse = function (path) {
if('string' === typeof path)
path = path.split('.').map(function (e) {
- return e === '*' ? true : e
+ if (e === '*')
+ return true
+ else if (e === '') // '..'.split('.') returns an empty string
+ return {recurse: true}
+ else
+ return e
})
@@ -55,7 +60,7 @@ exports.parse = function (path) {
var c
j++
- if (key !== '') {
+ if (key && !key.recurse) {
c = (j === this.stack.length) ? this : this.stack[j]
if (!c) return
if (! check(key, c.key)) return
diff --git a/readme.markdown b/readme.markdown
index dc23a0f..e737cc4 100644
--- a/readme.markdown
+++ b/readme.markdown
@@ -35,15 +35,17 @@ parse stream of values that match a path
JSONStream.parse('rows.*.doc')
```
+The `..` operator is the recursive descent operator from [JSONPath](http://goessner.net/articles/JsonPath/), which will match a child at any depth (see examples below).
+
If your keys have keys that include `.` or `*` etc, use an array instead.
`['row', true, /^doc/]`.
-If you use an array, `RegExp`s, booleans, and/or functions.
+If you use an array, `RegExp`s, booleans, and/or functions. The `..` operator is also available in array representation, using `{recurse: true}`.
any object that matches the path will be emitted as 'data' (and `pipe`d down stream)
If `path` is empty or null, no 'data' events are emitted.
-### Example
+### Examples
query a couchdb view:
@@ -93,6 +95,26 @@ stream.on('root', function(root, count) {
```
awesome!
+
+`JSONStream.parser('docs..value')` (or `JSONStream.parser('docs', {recurse: true}, 'value')` using an array) will emit every `value` object that is a child, grand-child, etc. of the `docs` object. In this example, it will match exactly 5 times at various depth levels, emitting 0, 1, 2, 3 and 4 as results.
+```js
+{
+ "total": 5,
+ "docs": [
+ {
+ "key": {
+ "value": 0,
+ "some": "property"
+ }
+ },
+ {"value": 1},
+ {"value": 2},
+ {"blbl": [{}, {"a":0, "b":1, "value":3}, 10]},
+ {"value": 4}
+ ]
+}
+```
+
## JSONStream.stringify(open, sep, close)
Create a writable stream.
diff --git a/test/doubledot2.js b/test/doubledot2.js
index f86e0df..f99d881 100644
--- a/test/doubledot2.js
+++ b/test/doubledot2.js
@@ -5,7 +5,7 @@
, it = require('it-is')
var expected = JSON.parse(fs.readFileSync(file))
- , parser = JSONStream.parse('docs..value')
+ , parser = JSONStream.parse(['docs', {recurse: true}, 'value'])
, called = 0
, ended = false
, parsed = []
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-jsonstream.git
More information about the Pkg-javascript-commits
mailing list