Skip to content

Instantly share code, notes, and snippets.

@webdesserts
Forked from deoxxa/line-splitter.js
Last active December 15, 2015 18:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save webdesserts/5302790 to your computer and use it in GitHub Desktop.
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.
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