Skip to content

Instantly share code, notes, and snippets.

@akirattii
Last active January 6, 2019 23:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akirattii/83316e88e4e6a0e8979905d8129fbf63 to your computer and use it in GitHub Desktop.
Save akirattii/83316e88e4e6a0e8979905d8129fbf63 to your computer and use it in GitHub Desktop.
[NodeJS] CSV data transform example using stream
// 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();
}
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