Last active
December 26, 2015 10:09
-
-
Save soarez/7134951 to your computer and use it in GitHub Desktop.
Line transform
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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