[Pkg-javascript-commits] [node-stream-splicer] 18/71: .shift

Bastien Roucariès rouca at moszumanska.debian.org
Fri Dec 15 09:55:48 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-splicer.

commit 7fd453124cc2afb14deed0e3ae3a8bcd01514396
Author: James Halliday <mail at substack.net>
Date:   Sun Jun 8 14:26:22 2014 -0700

    .shift
---
 index.js      |  7 +++++++
 test/shift.js | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 57 insertions(+)

diff --git a/index.js b/index.js
index 41f8c99..4aa8339 100644
--- a/index.js
+++ b/index.js
@@ -83,6 +83,13 @@ Pipeline.prototype.pop = function () {
     return s;
 };
 
+Pipeline.prototype.shift = function () {
+    var s = this._streams.shift();
+    if (this._streams.length > 0) s.unpipe(this._streams[0]);
+    this.emit('_mutate');
+    return s;
+};
+
 Pipeline.prototype.splice = function (start, removeLen) {
     var self = this;
     var len = this._streams.length;
diff --git a/test/shift.js b/test/shift.js
new file mode 100644
index 0000000..a1bf995
--- /dev/null
+++ b/test/shift.js
@@ -0,0 +1,50 @@
+var pipeline = require('../');
+var through = require('through2');
+var split = require('split');
+var concat = require('concat-stream');
+var test = require('tape');
+
+test('shift', function (t) {
+    var expected = {};
+    expected.a = [ 3, 4 ];
+    expected.b = [ 300, 400, 5, 6 ];
+    expected.c = [ 310, 410, 15, 16 ];
+    expected.output = [ 155, 205, 15/2, 8 ];
+    
+    t.plan(Object.keys(expected).reduce(function (sum, key) {
+        return sum + expected[key].length;
+    }, 0));
+    
+    var a = through.obj(function (x, enc, next) {
+        var ex = expected.a.shift();
+        t.equal(x, ex, 'a');
+        this.push(x * 100);
+        next();
+    });
+    var b = through.obj(function (x, enc, next) {
+        var ex = expected.b.shift();
+        t.equal(x, ex, 'b');
+        if (expected.b.length === 2) p.shift()
+        this.push(x + 10);
+        next();
+    });
+    var c = through.obj(function (x, enc, next) {
+        var ex = expected.c.shift();
+        t.equal(x, ex, 'c');
+        this.push(x / 2);
+        next();
+    });
+    
+    var p = pipeline.obj([ a, b, c ]);
+    p.pipe(through.obj(function (x, enc, next) {
+        var ex = expected.output.shift();
+        t.equal(x, ex);
+        next();
+    }));
+    
+    p.write(3);
+    p.write(4);
+    p.write(5);
+    p.write(6);
+    p.end();
+});

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



More information about the Pkg-javascript-commits mailing list