[Pkg-javascript-commits] [node-jsonstream] 47/214: always emit 'root' event: function(root, count)

Bastien Roucariès rouca at moszumanska.debian.org
Fri Dec 1 12:58:37 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 7481cf119ab30f476d00b7d4236c2ad6c8c312ea
Author: Malte-Thorben Bruns <skenqbx at googlemail.com>
Date:   Sun Jul 15 17:32:42 2012 +0200

    always emit 'root' event: function(root, count)
---
 index.js        | 33 +++++++++++++++------------------
 readme.markdown | 16 +++++++++++++---
 2 files changed, 28 insertions(+), 21 deletions(-)

diff --git a/index.js b/index.js
index a6f96d5..1bd26a7 100644
--- a/index.js
+++ b/index.js
@@ -4,14 +4,14 @@ var Parser = require('jsonparse')
 /*
 
   the value of this.stack that creationix's jsonparse has is weird.
-  
+
   it makes this code ugly, but his problem is way harder that mine,
   so i'll forgive him.
 
 */
 
 exports.parse = function (path) {
-  
+
   var stream = new Stream()
   var parser = new Parser()
   var count = 0
@@ -27,19 +27,19 @@ exports.parse = function (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
-      
+
     }
     var c = this
- 
+
     var key = path[path.length - 1]
       var m = check(key, c.key)
      if(!m)
@@ -71,10 +71,7 @@ exports.parse = function (path) {
   stream.end = function (data) {
     if(data)
       stream.write(data)
-    if(!count && !path)
-      stream.emit('data', stream.root)
-    else if(!count)
-      stream.emit('root', stream.root)
+    stream.emit('root', stream.root, count)
     stream.emit('end')
   }
   return stream
@@ -98,15 +95,15 @@ exports.stringify = function (op, sep, cl) {
     sep = '\n'
     cl = ''
   } else if (op == null) {
-  
+
     op = '[\n'
     sep = '\n,\n'
     cl = '\n]\n'
-  
+
   }
 
   //else, what ever you like
-  
+
   var stream = new Stream ()
     , first = true
     , ended = false
@@ -124,7 +121,7 @@ exports.stringify = function (op, sep, cl) {
     if(data) stream.write(data)
     if(!anyData) stream.emit('data', op)
     stream.emit('data', cl)
-    
+
     stream.emit('end')
   }
   stream.writable = true
@@ -139,15 +136,15 @@ exports.stringifyObject = function (op, sep, cl) {
     sep = '\n'
     cl = ''
   } else if (op == null) {
-  
+
     op = '{\n'
     sep = '\n,\n'
     cl = '\n}\n'
-  
+
   }
 
   //else, what ever you like
-  
+
   var stream = new Stream ()
     , first = true
     , ended = false
@@ -164,7 +161,7 @@ exports.stringifyObject = function (op, sep, cl) {
     if(data) stream.write(data)
     if(!anyData) stream.emit('data', op)
     stream.emit('data', cl)
-    
+
     stream.emit('end')
   }
   stream.writable = true
diff --git a/readme.markdown b/readme.markdown
index 62faf48..361d6cb 100644
--- a/readme.markdown
+++ b/readme.markdown
@@ -43,9 +43,9 @@ usally, a json API will return a list of objects.
 `path` should be an array of property names, `RegExp`s, booleans, and/or functions.
 any object that matches the path will be emitted as 'data' (and `pipe`d down stream)
 
-if `path` is empty or null, JSONStream.parse will only one 'data': the root object.
+a 'root' event is emitted when all data has been received. The 'root' event passes the root object & the count of matched objects.
 
-if `path` was supplied, but there where no matches for some reason, the 'root' event will be emitted with the root object. This is for cases where an external API like CouchDB returns an error or differently formatted result instead of the expected result.
+if `path` is empty or null, no 'data' events are emitted.
 
 ### example
 
@@ -83,7 +83,17 @@ we are probably most interested in the `rows.*.docs`
 create a `Stream` that parses the documents from the feed like this:
 
 ``` js
-JSONStream.parse(['rows', true, 'doc']) //rows, ANYTHING, doc
+var stream = JSONStream.parse(['rows', true, 'doc']) //rows, ANYTHING, doc
+
+stream.on('data', function(data) {
+  console.log('received:', data);
+});
+
+stream.on('root', function(root, count) {
+  if (!count) {
+    console.log('no matches found:', root);
+  }
+});
 ```
 awesome!
 

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