aboutsummaryrefslogtreecommitdiff
path: root/node_modules/unique-stream/index.js
diff options
context:
space:
mode:
authorFlorian Dold <florian.dold@gmail.com>2016-11-03 01:33:53 +0100
committerFlorian Dold <florian.dold@gmail.com>2016-11-03 01:33:53 +0100
commitd1291f67551c58168af43698a359cb5ddfd266b0 (patch)
tree55a13ed29fe1915e3f42f1b1b7038dafa2e975a7 /node_modules/unique-stream/index.js
parentd0a0695fb5d34996850723f7d4b1b59c3df909c2 (diff)
node_modules
Diffstat (limited to 'node_modules/unique-stream/index.js')
-rw-r--r--node_modules/unique-stream/index.js62
1 files changed, 28 insertions, 34 deletions
diff --git a/node_modules/unique-stream/index.js b/node_modules/unique-stream/index.js
index 0c13168e5..a2b292069 100644
--- a/node_modules/unique-stream/index.js
+++ b/node_modules/unique-stream/index.js
@@ -1,4 +1,22 @@
-var Stream = require('stream');
+'use strict';
+
+var filter = require('through2-filter').obj;
+var stringify = require("json-stable-stringify");
+
+var ES6Set;
+if (typeof global.Set === 'function') {
+ ES6Set = global.Set;
+} else {
+ ES6Set = function() {
+ this.keys = [];
+ this.has = function(val) {
+ return this.keys.indexOf(val) !== -1;
+ },
+ this.add = function(val) {
+ this.keys.push(val);
+ }
+ }
+}
function prop(propName) {
return function (data) {
@@ -7,48 +25,24 @@ function prop(propName) {
}
module.exports = unique;
-function unique(propName) {
- var keyfn = JSON.stringify;
+function unique(propName, keyStore) {
+ keyStore = keyStore || new ES6Set();
+
+ var keyfn = stringify;
if (typeof propName === 'string') {
keyfn = prop(propName);
} else if (typeof propName === 'function') {
keyfn = propName;
}
- var seen = {};
- var s = new Stream();
- s.readable = true;
- s.writable = true;
- var pipes = 0;
- s.write = function (data) {
+ return filter(function (data) {
var key = keyfn(data);
- if (seen[key] === undefined) {
- seen[key] = true;
- s.emit('data', data);
- }
- };
- var ended = 0;
- s.end = function (data) {
- if (arguments.length) s.write(data);
- ended++;
- if (ended === pipes || pipes === 0) {
- s.writable = false;
- s.emit('end');
+ if (keyStore.has(key)) {
+ return false;
}
- };
-
- s.destroy = function (data) {
- s.writable = false;
- };
- s.on('pipe', function () {
- pipes++;
+ keyStore.add(key);
+ return true;
});
-
- s.on('unpipe', function () {
- pipes--;
- });
-
- return s;
}