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 fs = require("fs"), | |
split = require("split"), | |
through2 = require("through2"); | |
var destinations = {}; | |
fs.createReadStream("all.txt",{encoding:"utf8"}) | |
.pipe(split()) | |
.pipe(through2({decodeStrings:false},transform,flush)); | |
function transform(row, enc, callback) { | |
getDestination(row[3]).write(row + "\n","utf8",callback); | |
} | |
function flush() { | |
// unnecessary? | |
for (var char in destinations) { | |
destinations[char].end(); | |
} | |
} | |
function getDestination(char) { | |
if (!destinations[char]) { | |
destinations[char] = fs.createWriteStream(char + ".txt"); | |
} | |
return destinations[char]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment