Skip to content

Instantly share code, notes, and snippets.

@sabarjp
Created September 14, 2015 04:25
Show Gist options
  • Save sabarjp/54c034def3bb81aeba61 to your computer and use it in GitHub Desktop.
Save sabarjp/54c034def3bb81aeba61 to your computer and use it in GitHub Desktop.
Node line reader example
var stream = require('stream')
var liner = new stream.Transform({objectMode: true})
liner._transform = function(chunk, encoding, done){
var data = chunk.toString();
if (this._lastLineData){
data = this._lastLineData + data;
}
var lines = data.split('\n');
this._lastLineData = lines.splice(lines.length-1, 1)[0];
lines.forEach(this.push.bind(this));
done();
};
liner._flush = function(done){
if (this._lastLineData){
this.push(this._lastLineData);
}
this._lastLineData = null;
done();
}
module.exports = liner
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment