[Pkg-javascript-commits] [node-jsonstream] 202/214: added emitPath option

Bastien Roucariès rouca at moszumanska.debian.org
Fri Dec 1 12:59:00 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 8f18d5f3a70e473dfd52cf22ee3fc0cae8cda588
Author: nathan <nathanj.wills at gmail.com>
Date:   Fri Dec 16 10:01:09 2016 +1100

    added emitPath option
---
 index.js     | 11 ++++++++++-
 test/keys.js | 20 ++++++++++++++++++++
 2 files changed, 30 insertions(+), 1 deletion(-)

diff --git a/index.js b/index.js
index 5546d46..c164100 100755
--- a/index.js
+++ b/index.js
@@ -58,6 +58,7 @@ exports.parse = function (path, map) {
     var i = 0 // iterates on path
     var j  = 0 // iterates on stack
     var emitKey = false;
+    var emitPath = false;
     while (i < path.length) {
       var key = path[i]
       var c
@@ -71,6 +72,7 @@ exports.parse = function (path, map) {
           return
         }
         emitKey = !!key.emitKey;
+        emitPath = !!key.emitPath;
         i++
       } else {
         i++
@@ -99,7 +101,14 @@ exports.parse = function (path, map) {
     var data = this.value[this.key]
     if(null != data)
       if(null != (data = map ? map(data, actualPath) : data)) {
-        data = emitKey ? { value: data, key: this.key } : data;
+        if (emitKey || emitPath) {
+          data = { value: data };
+          if (emitKey)
+            data["key"] = this.key;
+          if (emitPath)
+            data["path"] = actualPath;
+        }
+
         stream.queue(data)
       }
     delete this.value[this.key]
diff --git a/test/keys.js b/test/keys.js
index c60088a..747723d 100644
--- a/test/keys.js
+++ b/test/keys.js
@@ -39,6 +39,26 @@ test('keys via array', function(t) {
     assertFixtureKeys(stream, t);
 });
 
+test('path via array', function(t) {
+    var stream = JSONStream.parse(['obj',{emitPath: true}]);
+    
+    var paths = [];
+    var values = [];
+    stream.on('data', function(data) {
+        console.log(JSON.stringify(data));
+        paths.push(data.path);
+        values.push(data.value);
+    });
+
+    stream.on('end', function() {
+        t.deepEqual(paths, [['obj', 'one'], ['obj', 'two'], ['obj', 'three']]);
+        t.deepEqual(values, [1,2,3]);
+        t.end();
+    });
+    stream.write(JSON.stringify(fixture));
+    stream.end();
+});
+
 test('advanced keys', function(t) {
     var advanced = fs.readFileSync(couch_sample_file);
     var stream = JSONStream.parse(['rows', true, 'doc', {emitKey: true}]);

-- 
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