[Pkg-javascript-commits] [node-jsonstream] 179/214: feat: add option for emitting keys

Bastien Roucariès rouca at moszumanska.debian.org
Fri Dec 1 12:58:57 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 9aa24468e8913883930acaf17741397a7c3eb2b3
Author: Joscha Feth <jfeth at atlassian.com>
Date:   Thu Dec 17 13:20:35 2015 +1100

    feat: add option for emitting keys
---
 index.js                        | 12 ++++--
 readme.markdown                 | 13 +++++++
 test/fixtures/couch_sample.json | 18 +++++++++
 test/keys.js                    | 85 +++++++++++++++++++++++++++++++++++++++++
 4 files changed, 125 insertions(+), 3 deletions(-)

diff --git a/index.js b/index.js
index a9de1b3..a2b8f9e 100755
--- a/index.js
+++ b/index.js
@@ -30,7 +30,9 @@ exports.parse = function (path, map) {
 
   if('string' === typeof path)
     path = path.split('.').map(function (e) {
-      if (e === '*')
+      if (e === '$*')
+        return {emitKey: true}
+      else if (e === '*')
         return true
       else if (e === '') // '..'.split('.') returns an empty string
         return {recurse: true}
@@ -51,6 +53,7 @@ exports.parse = function (path, map) {
 
     var i = 0 // iterates on path
     var j  = 0 // iterates on stack
+    var emitKey = false;
     while (i < path.length) {
       var key = path[i]
       var c
@@ -60,6 +63,7 @@ exports.parse = function (path, map) {
         c = (j === this.stack.length) ? this : this.stack[j]
         if (!c) return
         if (! check(key, c.key)) return
+        emitKey = !!key.emitKey;
         i++
       } else {
         i++
@@ -84,8 +88,10 @@ exports.parse = function (path, map) {
     var actualPath = this.stack.slice(1).map(function(element) { return element.key }).concat([this.key])
     var data = this.value[this.key]
     if(null != data)
-      if(null != (data = map ? map(data, actualPath) : data))
+      if(null != (data = map ? map(data, actualPath) : data)) {
+        data = emitKey ? { value: data, key: this.key } : data;
         stream.queue(data)
+      }
     delete this.value[this.key]
     for(var k in this.stack)
       this.stack[k].value = null
@@ -119,7 +125,7 @@ function check (x, y) {
     return y == x
   else if (x && 'function' === typeof x.exec)
     return x.exec(y)
-  else if ('boolean' === typeof x)
+  else if ('boolean' === typeof x || 'object' === typeof x)
     return x
   else if ('function' === typeof x)
     return x(y)
diff --git a/readme.markdown b/readme.markdown
index 4a6531a..e674557 100644
--- a/readme.markdown
+++ b/readme.markdown
@@ -38,6 +38,8 @@ any object that matches the path will be emitted as 'data' (and `pipe`d down str
 
 If `path` is empty or null, no 'data' events are emitted.
 
+If you want to have keys emitted, you can prefix your `*` operator with `$`: `obj.$*` - in this case the data passed to the stream is an object with a `key` holding the key and a `value` property holding the data.
+
 ### Examples
 
 query a couchdb view:
@@ -82,6 +84,17 @@ stream.on('data', function(data) {
 ```
 awesome!
 
+In case you wanted the contents the doc emitted:
+
+``` js
+var stream = JSONStream.parse(['rows', true, 'doc', {emitKey: true}]) //rows, ANYTHING, doc, items in docs with keys
+
+stream.on('data', function(data) {
+  console.log('key:', data.key);
+  console.log('value:', data.value);
+});
+```
+
 ### recursive patterns (..)
 
 `JSONStream.parse('docs..value')` 
diff --git a/test/fixtures/couch_sample.json b/test/fixtures/couch_sample.json
new file mode 100644
index 0000000..b154c86
--- /dev/null
+++ b/test/fixtures/couch_sample.json
@@ -0,0 +1,18 @@
+{"total_rows":129,"offset":0,"rows":[
+  { "id":"change1_0.6995461115147918"
+  , "key":"change1_0.6995461115147918"
+  , "value":{"rev":"1-e240bae28c7bb3667f02760f6398d508"}
+  , "doc":{
+      "_id":  "change1_0.6995461115147918"
+    , "_rev": "1-e240bae28c7bb3667f02760f6398d508","hello":1}
+  },
+  { "id":"change2_0.6995461115147918"
+  , "key":"change2_0.6995461115147918"
+  , "value":{"rev":"1-13677d36b98c0c075145bb8975105153"}
+  , "doc":{
+      "_id":"change2_0.6995461115147918"
+    , "_rev":"1-13677d36b98c0c075145bb8975105153"
+    , "hello":2
+    }
+  },
+]}
diff --git a/test/keys.js b/test/keys.js
new file mode 100644
index 0000000..c60088a
--- /dev/null
+++ b/test/keys.js
@@ -0,0 +1,85 @@
+var test = require('tape');
+var fs = require ('fs');
+var join = require('path').join;
+var couch_sample_file = join(__dirname, 'fixtures','couch_sample.json');
+var JSONStream = require('../');
+
+var fixture = {
+  obj: {
+    one: 1,
+    two: 2,
+    three: 3
+  }
+};
+
+function assertFixtureKeys(stream, t) {
+    var keys = [];
+    var values = [];
+    stream.on('data', function(data) {
+        keys.push(data.key);
+        values.push(data.value);
+    });
+
+    stream.on('end', function() {
+        t.deepEqual(keys, ['one', 'two', 'three']);
+        t.deepEqual(values, [1,2,3]);
+        t.end();
+    });
+    stream.write(JSON.stringify(fixture));
+    stream.end();
+}
+
+test('keys via string', function(t) {
+    var stream = JSONStream.parse('obj.$*');
+    assertFixtureKeys(stream, t);
+});
+
+test('keys via array', function(t) {
+    var stream = JSONStream.parse(['obj',{emitKey: true}]);
+    assertFixtureKeys(stream, t);
+});
+
+test('advanced keys', function(t) {
+    var advanced = fs.readFileSync(couch_sample_file);
+    var stream = JSONStream.parse(['rows', true, 'doc', {emitKey: true}]);
+
+    var keys = [];
+    var values = [];
+    stream.on('data', function(data) {
+        keys.push(data.key);
+        values.push(data.value);
+    });
+
+    stream.on('end', function() {
+        t.deepEqual(keys, [
+            '_id', '_rev', 'hello',
+            '_id', '_rev', 'hello'
+        ]);
+        t.deepEqual(values, [
+            "change1_0.6995461115147918", "1-e240bae28c7bb3667f02760f6398d508", 1,
+            "change2_0.6995461115147918", "1-13677d36b98c0c075145bb8975105153", 2
+        ]);
+        t.end();
+    });
+    stream.write(advanced);
+    stream.end();
+});
+
+test('parent keys', function(t) {
+    var stream = JSONStream.parse('$*');
+    var d = null;
+    stream.on('data', function(data) {
+        if(d) t.fail('should only be called once');
+        d = data;
+    });
+
+    stream.on('end', function() {
+        t.deepEqual(d,{
+            key: 'obj',
+            value: fixture.obj
+        });
+        t.end();
+    });
+    stream.write(JSON.stringify(fixture));
+    stream.end();
+})

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