-
-
Save webdesserts/5302790 to your computer and use it in GitHub Desktop.
Creates a transform stream that splits any stream pipped into it into lines.
This file contains 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(); | |
}; | |
//usage | |
splitter = new lineSplitter() | |
someFile.pipe(splitter).pipe(someDestination) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment