[Pkg-javascript-commits] [node-stream-combiner2] 22/41: object mode

Bastien Roucariès rouca at moszumanska.debian.org
Wed Sep 6 09:57:53 UTC 2017


This is an automated email from the git hooks/post-receive script.

rouca pushed a commit to branch master
in repository node-stream-combiner2.

commit b66e83ff12bf4e4859c16439507fc5c167282298
Author: James Halliday <mail at substack.net>
Date:   Thu Jul 3 11:42:35 2014 -0700

    object mode
---
 index.js      | 22 ++++++++++++++++++----
 test/index.js | 15 +++++++++++++++
 2 files changed, 33 insertions(+), 4 deletions(-)

diff --git a/index.js b/index.js
index aca8e22..8f217fc 100644
--- a/index.js
+++ b/index.js
@@ -3,18 +3,32 @@ var through = require('through2')
 
 module.exports = function () {
   var streams
+  if(arguments.length == 1 && Array.isArray(arguments[0])) {
+    streams = arguments[0]
+  } else {
+    streams = [].slice.call(arguments)
+  }
+  return combine(streams)
+}
 
+module.exports.obj = function () {
+  var streams
   if(arguments.length == 1 && Array.isArray(arguments[0])) {
     streams = arguments[0]
   } else {
     streams = [].slice.call(arguments)
   }
+  return combine(streams, { objectMode: true })
+}
+
+  
+function combine (streams, opts) {
 
   for (var i = 0; i < streams.length; i++)
-    streams[i] = wrap(streams[i])
+    streams[i] = wrap(streams[i], opts)
 
   if(streams.length == 0)
-    return through()
+    return through(opts)
   else if(streams.length == 1)
     return streams[0]
 
@@ -47,9 +61,9 @@ module.exports = function () {
   return thepipe
 }
 
-function wrap (tr) {
+function wrap (tr, opts) {
   if (typeof tr.read === 'function') return tr
-  var opts = tr._options || {}
+  if (!opts) opts = tr._options || {}
   var input = through(opts), output = through(opts)
   input.pipe(tr).pipe(output)
   var dup = duplexer(input, output)
diff --git a/test/index.js b/test/index.js
index 59fdaae..8bc1cd3 100644
--- a/test/index.js
+++ b/test/index.js
@@ -1,4 +1,5 @@
 var es = require('event-stream')
+var through = require('through2')
 var combine = require('..')
 var test = require('tape')
 
@@ -63,3 +64,17 @@ test('0 argument through stream', function (test) {
   pipe.end('robots')
 })
 
+test('object mode', function (test) {
+  test.plan(2)
+  var pipe = combine.obj()
+   , expected = [ [4,5,6], {x:5} ]
+
+  pipe.pipe(through.obj(function(data, enc, next) {
+    test.deepEqual(data, expected.shift())
+    next()
+  }))
+  pipe.write([4,5,6])
+  pipe.write({x:5})
+  pipe.end()
+})
+

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-stream-combiner2.git



More information about the Pkg-javascript-commits mailing list