Skip to content

Instantly share code, notes, and snippets.

@gerrich
Forked from touv/splitline.js
Last active December 15, 2015 10:18
Show Gist options
  • Save gerrich/5244302 to your computer and use it in GitHub Desktop.
Save gerrich/5244302 to your computer and use it in GitHub Desktop.
#!/usr/bin/env node
process.stdin.resume();
process.stdin.setEncoding('utf8');
function line_reader(stream, call_back) {
var remainder = '';
stream.on('data', function (chunk) {
var lines = chunk.toString().split('\n');
lines.unshift(remainder + lines.shift());
remainder = lines.pop();
lines.forEach(function(line) {
call_back(line, false);
});
});
stream.on('end', function() {call_back(remainder, true)});
};
/* ----------- example use ------------ */
line_reader(process.stdin, function(line, eof){
process.stdout.write(line);
if (!eof) process.stdout.write("\n");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment