Skip to content

Instantly share code, notes, and snippets.

@soarez
Last active December 26, 2015 10:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save soarez/7134951 to your computer and use it in GitHub Desktop.
Save soarez/7134951 to your computer and use it in GitHub Desktop.
Line transform
var Transform = require('stream').Transform;
var util = require('util');
module.exports = LineTransform;
util.inherits(LineTransform, Transform);
function LineTransform() {
Transform.apply(this, arguments);
this.setEncoding('utf8');
}
LineTransform.prototype._transform = _transformLineTransform;
function _transformLineTransform(chunk, encoding, cb) {
var lines = chunk.toString().split('\n');
this.push((this.buffer || '') + lines.shift());
this.buffer = lines.pop();
lines.forEach(this._pushOut.bind(this));
cb();
}
LineTransform.prototype._pushOut = _pushOutLineTransform;
function _pushOutLineTransform(line) {
this.push(line);
}
LineTransform.prototype._flush = _flushLineTransform;
function _flushLineTransform(cb) {
if (this.buffer)
this.push(this.buffer);
cb();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment