Skip to content

Instantly share code, notes, and snippets.

@gnarf gnarf/1-file.txt forked from cowboy/1-file.txt
Last active Aug 29, 2015

What would you like to do?
foo bar
last line (there may or may not be a trailing newline after this line)
0 foo bar
1 baz
2 qux
4 last line (there may or may not be a trailing newline after this line)
var stream = require("stream");
var util = require("util");
function LineParser() {
stream.Transform.apply(this, arguments);
// start off our stream with our "start" and some default "buffer"
this._line = "";
this._count = 0;
util.inherits(LineParser, stream.Transform);
// emit that line with the counter and the newline added again
LineParser.prototype._sendLine = function(line) {
this.push(this._count++ + " " + line + "\n");
// _transform gets called with every incoming bit of data, done() when done
// in case you are async...
LineParser.prototype._transform = function(data, encoding, done) {
var lines = (this._line + (data.toString())).split('\n');
// take the last line (which didn't have a trailing newline) for the next
// buffer chunk
this._line = lines.pop();
lines.forEach(this._sendLine, this);
// _flush gets called when there will be no more _transform's called
// after the "readable" ends, you get _flush, then your stream ends
LineParser.prototype._flush = function(done) {
if (this._line) {
module.exports = LineParser;
#!/usr/bin/env node
// Streaming IO is great, but far more complex than Synchronous or
// Asynchronous IO
// you should probably abstract it into a reusable thing and make a module
// ... unless you use a pre-built third party library. you can write your own streams
var fs = require("fs");
var LineParser = require("./line-parser");
.pipe(new LineParser)
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.