[Pkg-javascript-commits] [node-labeled-stream-splicer] 02/14: example works

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

commit 26982edc2dbce6dfa04d7d3d49ababe9377bc540
Author: James Halliday <mail at substack.net>
Date:   Tue Jun 10 22:03:01 2014 -0700

    example works
---
 index.js | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 65 insertions(+)

diff --git a/index.js b/index.js
new file mode 100644
index 0000000..59a93b2
--- /dev/null
+++ b/index.js
@@ -0,0 +1,65 @@
+var Splicer = require('stream-splicer');
+var inherits = require('inherits');
+var isarray = require('isarray');
+
+module.exports = Labeled;
+inherits(Labeled, Splicer);
+
+module.exports.obj = function (streams, opts) {
+    if (!opts) opts = {};
+    opts.objectMode = true;
+    return new Labeled(streams, opts);
+};
+
+function Labeled (streams, opts) {
+    if (!(this instanceof Labeled)) return new Labeled(streams, opts);
+    Splicer.call(this, [], opts);
+    
+    var reps = [];
+    for (var i = 0; i < streams.length; i++) {
+        var s = streams[i];
+        if (typeof s === 'string') continue;
+        if (isarray(s)) {
+            s = new Labeled(s, opts);
+        }
+        if (i >= 0 && typeof streams[i-1] === 'string') {
+            s.label = streams[i-1];
+        }
+        reps.push(s);
+    }
+    if (typeof streams[i-1] === 'string') {
+        reps.push(new Labeled([], opts));
+    }
+    this.splice.apply(this, [0,0].concat(reps));
+}
+
+Labeled.prototype.indexOf = function (stream) {
+    if (typeof stream === 'string') {
+        for (var i = 0; i < this._streams.length; i++) {
+            if (this._streams[i].label === stream) return i;
+        }
+        return -1;
+    }
+    else {
+        return Splicer.prototype.indexOf.call(this, stream);
+    }
+};
+
+Labeled.prototype.get = function (key) {
+    if (typeof key === 'string') {
+        var ix = this.indexOf(key);
+        if (ix < 0) return undefined;
+        return this._streams[ix];
+    }
+    else return Splicer.prototype.get.call(this, key);
+};
+
+Labeled.prototype.splice = function (key) {
+    var ix;
+    if (typeof key === 'string') {
+        ix = this.indexOf(key);
+    }
+    else ix = key;
+    var args = [ ix ].concat([].slice.call(arguments, 1));
+    return Splicer.prototype.splice.apply(this, args);
+};

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



More information about the Pkg-javascript-commits mailing list