streamlined
Version:
Collection of helper streams to deal with mapping/transforming object streams
32 lines (24 loc) • 529 B
JavaScript
var through2 = require('through2');
function noop() { }
module.exports = collect;
function collect(onEnd) {
var fns = Array.prototype.slice.call(arguments);
var s = through2.obj(write, end);
var results = [];
var err = null;
onEnd = fns.length ? fns : [ noop ];
function write(data, enc, cb) {
results.push(data);
this.push(data);
cb();
}
s.on('error', function (err_) {
err = err_;
});
function end() {
fns.forEach(function (fn) {
fn(err, results);
});
}
return s;
}