Skip to content

Instantly share code, notes, and snippets.

@soarez soarez/linetransform.js
Last active Dec 26, 2015

Embed
What would you like to do?
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
You can’t perform that action at this time.