197 lines
4.9 KiB
JavaScript
197 lines
4.9 KiB
JavaScript
// Generated by CoffeeScript 2.3.2
|
|
// # Stream Transformer
|
|
|
|
// Pass all elements of an array or a stream to transform, filter and add. Features include:
|
|
|
|
// * Extends the Node.js "stream.Transform" API.
|
|
// * Both synchrounous and asynchronous support based and user callback
|
|
// arguments signature.
|
|
// * Ability to skip data.
|
|
// * Sequential and concurrent execution using the "parallel" options.
|
|
|
|
// Please look at the [README], the [samples] and the [tests] for additional
|
|
// information.
|
|
var Transformer, stream, util;
|
|
|
|
stream = require('stream');
|
|
|
|
util = require('util');
|
|
|
|
// ## Usage
|
|
|
|
// Callback approach, for ease of use:
|
|
|
|
// `transform([data], handler, [options])`
|
|
|
|
// Stream API, for maximum of power:
|
|
|
|
// `transform([data], [options], handler, [options], [callback])`
|
|
module.exports = function() {
|
|
var argument, callback, data, error, handler, i, j, k, len, options, result, transform, type, v;
|
|
options = {};
|
|
for (i = j = 0, len = arguments.length; j < len; i = ++j) {
|
|
argument = arguments[i];
|
|
type = typeof argument;
|
|
if (argument === null) {
|
|
type = 'null';
|
|
} else if (type === 'object' && Array.isArray(argument)) {
|
|
type = 'array';
|
|
}
|
|
if (i === 0) {
|
|
if (type === 'function') {
|
|
handler = argument;
|
|
} else if (type !== null) {
|
|
data = argument;
|
|
}
|
|
continue;
|
|
}
|
|
if (type === 'object') {
|
|
for (k in argument) {
|
|
v = argument[k];
|
|
options[k] = v;
|
|
}
|
|
} else if (type === 'function') {
|
|
if (handler && i === arguments.length - 1) {
|
|
callback = argument;
|
|
} else {
|
|
handler = argument;
|
|
}
|
|
} else if (type !== 'null') {
|
|
throw new Error('Invalid arguments');
|
|
}
|
|
}
|
|
transform = new Transformer(options, handler);
|
|
error = false;
|
|
if (data) {
|
|
process.nextTick(function() {
|
|
var len1, m, row;
|
|
for (m = 0, len1 = data.length; m < len1; m++) {
|
|
row = data[m];
|
|
if (error) {
|
|
break;
|
|
}
|
|
transform.write(row);
|
|
}
|
|
return transform.end();
|
|
});
|
|
}
|
|
if (callback || options.consume) {
|
|
result = [];
|
|
transform.on('readable', function() {
|
|
var record, results;
|
|
results = [];
|
|
while ((record = transform.read())) {
|
|
if (callback) {
|
|
results.push(result.push(record));
|
|
} else {
|
|
results.push(void 0);
|
|
}
|
|
}
|
|
return results;
|
|
});
|
|
transform.on('error', function(err) {
|
|
error = true;
|
|
if (callback) {
|
|
return callback(err);
|
|
}
|
|
});
|
|
transform.on('end', function() {
|
|
if (callback && !error) {
|
|
return callback(null, result);
|
|
}
|
|
});
|
|
}
|
|
return transform;
|
|
};
|
|
|
|
// ## Transformer
|
|
|
|
// Options are documented [here](http://csv.js.org/transform/options/).
|
|
Transformer = function(options1 = {}, transform1) {
|
|
var base;
|
|
this.options = options1;
|
|
this.transform = transform1;
|
|
this.options.objectMode = true;
|
|
if ((base = this.options).parallel == null) {
|
|
base.parallel = 100;
|
|
}
|
|
stream.Transform.call(this, this.options);
|
|
this.running = 0;
|
|
this.started = 0;
|
|
this.finished = 0;
|
|
return this;
|
|
};
|
|
|
|
util.inherits(Transformer, stream.Transform);
|
|
|
|
module.exports.Transformer = Transformer;
|
|
|
|
Transformer.prototype._transform = function(chunk, encoding, cb) {
|
|
var callback, err, l;
|
|
this.started++;
|
|
this.running++;
|
|
if (this.running < this.options.parallel) {
|
|
cb();
|
|
cb = null;
|
|
}
|
|
try {
|
|
l = this.transform.length;
|
|
if (this.options.params != null) {
|
|
l--;
|
|
}
|
|
if (l === 1) { // sync
|
|
this.__done(null, [this.transform.call(this, chunk, this.options.params)], cb);
|
|
} else if (l === 2) { // async
|
|
callback = (err, ...chunks) => {
|
|
return this.__done(err, chunks, cb);
|
|
};
|
|
this.transform.call(this, chunk, callback, this.options.params);
|
|
} else {
|
|
throw Error("Invalid handler arguments");
|
|
}
|
|
return false;
|
|
} catch (error1) {
|
|
err = error1;
|
|
return this.__done(err);
|
|
}
|
|
};
|
|
|
|
Transformer.prototype._flush = function(cb) {
|
|
this._ending = function() {
|
|
if (this.running === 0) {
|
|
return cb();
|
|
}
|
|
};
|
|
return this._ending();
|
|
};
|
|
|
|
Transformer.prototype.__done = function(err, chunks, cb) {
|
|
var chunk, j, len;
|
|
this.running--;
|
|
if (err) {
|
|
return this.emit('error', err);
|
|
}
|
|
this.finished++;
|
|
for (j = 0, len = chunks.length; j < len; j++) {
|
|
chunk = chunks[j];
|
|
if (typeof chunk === 'number') {
|
|
chunk = `${chunk}`;
|
|
}
|
|
if ((chunk != null) && chunk !== '') {
|
|
// We dont push empty string
|
|
// See https://nodejs.org/api/stream.html#stream_readable_push
|
|
this.push(chunk);
|
|
}
|
|
}
|
|
if (cb) {
|
|
cb();
|
|
}
|
|
if (this._ending) {
|
|
return this._ending();
|
|
}
|
|
};
|
|
|
|
// [readme]: https://github.com/wdavidw/node-stream-transform
|
|
// [samples]: https://github.com/wdavidw/node-stream-transform/tree/master/samples
|
|
// [tests]: https://github.com/wdavidw/node-stream-transform/tree/master/test
|