Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Node Split Lines Transform Stream
var strDec = require('string_decoder');
var StringDecoder = strDec.StringDecoder;
var stream = require('stream');
var Transform = stream.Transform;
var util = require('util');
function LineSplitter(options) {
if (!(this instanceof LineSplitter)) {
return new LineSplitter(options);
}
options || (options = {});
options.readableObjectMode = true;
Transform.call(this, options);
var state = this._writableState;
this._decoder = new StringDecoder(state.defaultEncoding);
this._lineBuffer = '';
this._eol = options.eol || '\n';
}
util.inherits(LineSplitter, Transform);
LineSplitter.prototype._transform = function(chunk, encoding, callback) {
if (encoding === 'buffer') {
chunk = this._decoder.write(chunk);
}
this._readChunk(chunk);
callback();
};
LineSplitter.prototype._readChunk = function(chunk) {
var chunk = this._lineBuffer + chunk;
var eol = this._eol;
var eolSize = eol.length;
var eolIndex;
while (-1 !== (eolIndex = chunk.indexOf(eol))) {
var line = chunk.substr(0, eolIndex);
chunk = chunk.substr(eolIndex + eolSize);
this.push(line);
}
this._lineBuffer = chunk;
};
LineSplitter.prototype._flush = function() {
this._readChunk(this._decoder.end());
var lastLine = this._lineBuffer;
this._lineBuffer = null;
if (lastLine.length) {
this.push(lastLine);
}
};
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.