streamlined
Version:
Collection of helper streams to deal with mapping/transforming object streams
29 lines (23 loc) • 637 B
JavaScript
var through2 = require('through2'),
selector = require('defunct/selector');
module.exports = distinct;
function distinct(selectorExpr, aggregate) {
if (typeof aggregate === 'undefined') aggregate = true;
var s = through2.obj(write, end);
var types = {};
var locator = selector(selectorExpr);
function write(data, enc, cb) {
var val = locator(data);
if (typeof val !== undefined) {
types[val] = types[val] || 0;
if (!aggregate && types[val] === 0) this.push(val);
types[val]++;
}
cb();
}
function end() {
if (aggregate) this.push(types);
this.push(null);
}
return s;
}