[Pkg-javascript-commits] [node-jsonstream] 94/214: Add support for recursive descent '..' operator in path.
Bastien Roucariès
rouca at moszumanska.debian.org
Fri Dec 1 12:58:44 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 677d35c2123e1c7275d4dd9f6cdd481e446824a6
Author: Paul Mougel <paul.mougel at atos.net>
Date: Fri Aug 30 11:18:39 2013 +0200
Add support for recursive descent '..' operator in path.
This commit improves the path matching capabilities of JSONStream, by adding a new operator: .. which will match a child at any depth
Ex: The path 'docs..value' will match exactly 5 times at various depth levels, yielding 0,1,2,3 and 4 as results.
{
"total": 5,
"docs": [
{
"key": {
"value": 0,
"some": "property"
}
},
{"value": 1},
{"value": 2},
{"blbl": [{}, {"a":0, "b":1, "value":3}, 10]},
{"value": 4}
]
}
---
index.js | 50 +++++++++++++++++++++++++-----------------------
test/doubledot1.js | 29 ++++++++++++++++++++++++++++
test/doubledot2.js | 29 ++++++++++++++++++++++++++++
test/fixtures/depth.json | 15 +++++++++++++++
4 files changed, 99 insertions(+), 24 deletions(-)
diff --git a/index.js b/index.js
index 8707f95..a6e2904 100644
--- a/index.js
+++ b/index.js
@@ -43,38 +43,40 @@ exports.parse = function (path) {
path = null
parser.onValue = function () {
- if(!this.root && this.stack.length == 1){
+ if (!this.root && this.stack.length == 1)
stream.root = this.value
- }
- if(!path || this.stack.length !== path.length)
- return
- var _path = []
- for( var i = 0; i < (path.length - 1); i++) {
- var key = path[i]
- var c = this.stack[1 + (+i)]
- if(!c) {
- return
- }
- var m = check(key, c.key)
- _path.push(c.key)
-
- if(!m)
- return
+ if(! path) return
+ var i = 0 // iterates on path
+ var j = 0 // iterates on stack
+ while (i < path.length) {
+ var key = path[i]
+ var c
+ j++
+
+ if (key !== '') {
+ c = (j === this.stack.length) ? this : this.stack[j]
+ if (!c) return
+ if (! check(key, c.key)) return
+ i++
+ } else {
+ i++
+ var nextKey = path[i]
+ if (! nextKey) return
+ while (true) {
+ c = (j === this.stack.length) ? this : this.stack[j]
+ if (!c) return
+ if (check(nextKey, c.key)) { i++; break}
+ j++
+ }
+ }
}
- var c = this
-
- var key = path[path.length - 1]
- var m = check(key, c.key)
- if(!m)
- return
- _path.push(c.key)
+ if (j !== this.stack.length) return
count ++
stream.queue(this.value[this.key])
delete this.value[this.key]
-
}
parser._onToken = parser.onToken;
diff --git a/test/doubledot1.js b/test/doubledot1.js
new file mode 100644
index 0000000..78149b9
--- /dev/null
+++ b/test/doubledot1.js
@@ -0,0 +1,29 @@
+var fs = require ('fs')
+ , join = require('path').join
+ , file = join(__dirname, 'fixtures','all_npm.json')
+ , JSONStream = require('../')
+ , it = require('it-is')
+
+var expected = JSON.parse(fs.readFileSync(file))
+ , parser = JSONStream.parse('rows..rev')
+ , called = 0
+ , ended = false
+ , parsed = []
+
+fs.createReadStream(file).pipe(parser)
+
+parser.on('data', function (data) {
+ called ++
+ parsed.push(data)
+})
+
+parser.on('end', function () {
+ ended = true
+})
+
+process.on('exit', function () {
+ it(called).equal(expected.rows.length)
+ for (var i = 0 ; i < expected.rows.length ; i++)
+ it(parsed[i]).deepEqual(expected.rows[i].value.rev)
+ console.error('PASSED')
+})
diff --git a/test/doubledot2.js b/test/doubledot2.js
new file mode 100644
index 0000000..f86e0df
--- /dev/null
+++ b/test/doubledot2.js
@@ -0,0 +1,29 @@
+ var fs = require ('fs')
+ , join = require('path').join
+ , file = join(__dirname, 'fixtures','depth.json')
+ , JSONStream = require('../')
+ , it = require('it-is')
+
+ var expected = JSON.parse(fs.readFileSync(file))
+ , parser = JSONStream.parse('docs..value')
+ , called = 0
+ , ended = false
+ , parsed = []
+
+ fs.createReadStream(file).pipe(parser)
+
+ parser.on('data', function (data) {
+ called ++
+ parsed.push(data)
+ })
+
+ parser.on('end', function () {
+ ended = true
+ })
+
+ process.on('exit', function () {
+ it(called).equal(5)
+ for (var i = 0 ; i < 5 ; i++)
+ it(parsed[i]).deepEqual(i)
+ console.error('PASSED')
+ })
diff --git a/test/fixtures/depth.json b/test/fixtures/depth.json
new file mode 100644
index 0000000..868062f
--- /dev/null
+++ b/test/fixtures/depth.json
@@ -0,0 +1,15 @@
+{
+ "total": 5,
+ "docs": [
+ {
+ "key": {
+ "value": 0,
+ "some": "property"
+ }
+ },
+ {"value": 1},
+ {"value": 2},
+ {"blbl": [{}, {"a":0, "b":1, "value":3}, 10]},
+ {"value": 4}
+ ]
+}
\ No newline at end of file
--
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