Created
April 2, 2013 20:17
-
-
Save deoxxa/5295806 to your computer and use it in GitHub Desktop.
split something into lines
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 stream = require("stream"), | |
util = require("util"); | |
var LineSplitter = function LineSplitter(options) { | |
options = options || {}; | |
options.objectMode = true; | |
stream.Transform.call(this, options); | |
this.buffer = ""; | |
}; | |
util.inherits(LineSplitter, stream.Transform); | |
LineSplitter.prototype._transform = function _transform(input, encoding, done) { | |
this.buffer += input.toString(encoding); | |
var lines = this.buffer.split(/\n/g); | |
this.buffer = lines.pop(); | |
lines.forEach(this.push.bind(this)); | |
return done(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment