-
-
Save gerrich/5244302 to your computer and use it in GitHub Desktop.
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
#!/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