Last active
January 6, 2019 23:00
-
-
Save akirattii/83316e88e4e6a0e8979905d8129fbf63 to your computer and use it in GitHub Desktop.
[NodeJS] CSV data transform example using stream
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
// Define the transform rule | |
const transformRules = [ | |
upperCase, // col idx 0: To uppercase. ``upperCase` is function. | |
{ "1":"1st", "2":"2nd", "3":"3rd" }, // col idx 1: 1=>"1st", 2=>"2nd", ... | |
{ "A":"PlanA", "B":"PlanB", "C":"PlanC" },// col idx 2: "A"=>"PlanA", ... | |
]; | |
const fs = require("fs"); | |
const readStream = fs.createReadStream(__dirname + "/input.csv" ); | |
const writeStream = fs.createWriteStream(__dirname + "/output.csv", { encoding: "utf8" } ); | |
const lineReader = require('readline').createInterface({ | |
input: readStream, | |
output: writeStream, | |
}); | |
lineReader.on('line', function (line) { | |
const cols = line.split(","); | |
const newCols = transform(cols, transformRules); | |
console.log(cols, "=>", newCols); | |
writeStream.write(newCols.join(",") + "\n"); | |
}); | |
lineReader.on('close', function() { | |
console.log("close"); | |
process.exit(0); | |
}); | |
function transform(cols, rules){ | |
const newCols = []; | |
for (let len = cols.length, i = 0; i < len; i++){ | |
const col = cols[i]; | |
const rule = rules[i]; | |
if (!rule) { | |
newCols[i] = col; | |
continue; | |
} | |
if (typeof rule === "function"){ | |
newCols[i] = rule(col); | |
continue; | |
} | |
newCols[i] = rule[col]; | |
} | |
return newCols; | |
} | |
function upperCase(s) { | |
if (!s) return s; | |
return s.toUpperCase(); | |
} |
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
aaa | 1 | A | ||
---|---|---|---|---|
bbb | 2 | B | ||
ccc | 3 | C |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment