From 9a880fc73eafefaaf5b8dea0182e90669cae34b6 Mon Sep 17 00:00:00 2001 From: DullReferenceException Date: Fri, 21 Nov 2014 16:15:24 -0800 Subject: [PATCH] Non-object JSON roots Updated parser to emit the `root` event for the top-level JSON value, not just objects & arrays. --- .gitignore | 1 + index.js | 6 +++--- test/non_object_roots.js | 16 ++++++++++++++++ 3 files changed, 20 insertions(+), 3 deletions(-) create mode 100644 test/non_object_roots.js diff --git a/.gitignore b/.gitignore index 13abef4..fff3b1a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ node_modules node_modules/* npm_debug.log +.idea diff --git a/index.js b/index.js index 83848af..2422c9c 100755 --- a/index.js +++ b/index.js @@ -41,9 +41,9 @@ exports.parse = function (path, map) { if(!path || !path.length) path = null - parser.onValue = function () { - if (!this.root && this.stack.length == 1) - stream.root = this.value + parser.onValue = function (value) { + if (!this.root) + stream.root = value if(! path) return diff --git a/test/non_object_roots.js b/test/non_object_roots.js new file mode 100644 index 0000000..8055ad6 --- /dev/null +++ b/test/non_object_roots.js @@ -0,0 +1,16 @@ +var JSONStream = require('../'); +var it = require('it-is'); +var parser = JSONStream.parse(); + +var succeeded = false; + +parser.on('root', function (value) { + it(value).equal(true); + succeeded = true; +}); + +parser.on('end', function() { + it(succeeded).equal(true); +}); + +parser.end('true');