streamlined
Version:
Collection of helper streams to deal with mapping/transforming object streams
41 lines (34 loc) • 865 B
JavaScript
var through2 = require('through2'),
pathos = require('pathos'),
selector = require('defunct/selector');
module.exports = select;
function select(colExprs, build) {
if (typeof build === 'undefined') build = false;
if (!Array.isArray(colExprs)) {
colExprs = [colExprs];
}
var s = through2.obj(write);
var locators = [];
colExprs.forEach(function (colExpr, i) {
var locator = selector(colExpr, build);
locators[i] = locator;
});
function write(o, enc, cb) {
var data = [];
for (var i = 0; i < colExprs.length; i++) {
var locator = locators[i];
var val = locator(o);
if (typeof val !== 'undefined') {
data.push(val);
}
}
if (data.length === colExprs.length) {
if (build) {
data = pathos.build(data);
}
this.push(data);
}
cb();
}
return s;
}