[Pkg-javascript-commits] [node-stream-splicer] 13/71: splice test passes

Bastien Roucariès rouca at moszumanska.debian.org
Fri Dec 15 09:55:47 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 5153fb687750d3efa349893ecde8ceb13f976b9b
Author: James Halliday <mail at substack.net>
Date:   Sun Jun 8 06:44:17 2014 -0700

    splice test passes
---
 index.js       | 52 +++++++++++++++++++++++++++++++++++++++++++++++++---
 test/splice.js |  5 ++---
 2 files changed, 51 insertions(+), 6 deletions(-)

diff --git a/index.js b/index.js
index 4669554..0c5e469 100644
--- a/index.js
+++ b/index.js
@@ -72,6 +72,12 @@ Pipeline.prototype._write = function (buf, enc, next) {
 };
 
 Pipeline.prototype.push = function (stream) {
+    this._push(stream);
+    this.emit('_mutate');
+    return this._streams.length;
+};
+
+Pipeline.prototype._push = function (stream) {
     var self = this;
     stream.on('error', function (err) {
         err.stream = this;
@@ -90,9 +96,6 @@ Pipeline.prototype.push = function (stream) {
             Duplex.prototype.push.call(self, null);
         }
     });
-    this.emit('_mutate');
-    
-    return this;
 };
 
 Pipeline.prototype.pop = function () {
@@ -104,6 +107,49 @@ Pipeline.prototype.pop = function () {
     return s;
 };
 
+Pipeline.prototype.splice = function (start, removeLen) {
+    var self = this;
+    var removed = this._streams.splice.apply(this._streams, arguments);
+    var n = start < 0 ? this._streams.length - start : start;
+    
+    if (this._streams[n] && removed.length > 0) {
+        this._streams[n].unpipe(removed[0]);
+    }
+    for (var i = 1; i < removed.length; i++) {
+        removed[i-1].unpipe(removed[i]);
+    }
+    var nextIndex = n + 1 - removeLen + arguments.length - 2;
+    if (removed[i-1] && this._streams[nextIndex]) {
+        removed[i-1].unpipe(this._streams[nextIndex]);
+    }
+    for (var i = 2; i < arguments.length; i++) (function (stream, i) {
+        var j = n + 1 - removeLen + i - 2;
+        stream.on('error', function (err) {
+            err.stream = this;
+            self.emit('error', err);
+        });
+        stream = self._streams[j] = self._wrapStream(stream);
+        
+        if (self._streams[j-1]) {
+            self._streams[j-1].pipe(stream);
+        }
+        
+        stream.once('end', function () {
+            var ix = self._streams.indexOf(stream);
+            if (ix === self._streams.length - 1) {
+                Duplex.prototype.push.call(self, null);
+            }
+        });
+    })(arguments[i], i);
+    
+    if (self._streams[nextIndex-1] && self._streams[nextIndex]) {
+        self._streams[nextIndex-1].pipe(self._streams[nextIndex]);
+    }
+    
+    this.emit('_mutate');
+    return removed;
+};
+
 Pipeline.prototype.indexOf = function (stream) {
     return this._streams.indexOf(stream);
 };
diff --git a/test/splice.js b/test/splice.js
index a4f3964..b73f8ac 100644
--- a/test/splice.js
+++ b/test/splice.js
@@ -6,7 +6,7 @@ var test = require('tape');
 
 test('splice', function (t) {
     var expected = {};
-    expected.replacer = [ '333', '444', '5000' ];
+    expected.replacer = [ '333', '444', '5000', '6000' ];
     
     t.plan(Object.keys(expected).reduce(function (sum, key) {
         return sum + expected[key].length;
@@ -33,11 +33,10 @@ test('splice', function (t) {
     var replacer = through(function (buf, enc, next) {
         var ex = expected.replacer.shift();
         t.equal(buf.toString(), ex);
-        this.push(buf.toString('hex') + '\n');
-        
         if (expected.replacer.length === 2) {
             stream.splice(3, 1, thousander);
         }
+        this.push(buf.toString('hex') + '\n');
         next();
     });
     

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