diff options
Diffstat (limited to 'node_modules/unique-stream/index.js')
-rw-r--r-- | node_modules/unique-stream/index.js | 62 |
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; } |