Skip to content

Instantly share code, notes, and snippets.

@dhuang612
Last active October 29, 2021 21:36
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 dhuang612/08d799b697065fa794ae74f2b86ef20a to your computer and use it in GitHub Desktop.
Save dhuang612/08d799b697065fa794ae74f2b86ef20a to your computer and use it in GitHub Desktop.
const fs = require('fs');
const csv = require('fast-csv');
const path = require('path');
const inputFile = require('path').join(getDir() + '/sample-data/input.csv');
const outputFile = require('path').join(getDir() + '/sample-data/output.csv');
function getDir() {
if (process.pkg) {
return path.resolve(process.execPath + "/..");
} else {
return path.join(require.main ? require.main.path : process.cwd());
}
}
(async function () {
const writeStream = fs.createWriteStream(outputFile);
const parse = csv.parse(
{
ignoreEmpty: true,
discardUnmappedColumns: true,
headers: ['beta','alpha','redundant','charlie'],
});
const transform = csv.format({ headers: true })
.transform((row) => (
{
NewAlpha: row.alpha, // reordered
NewBeta: row.beta,
NewCharlie: row.charlie,
// redundant is dropped
// delta is not loaded by parse() above
}
));
const stream = fs.createReadStream(inputFile)
.pipe(parse)
.pipe(transform)
.pipe(writeStream);
})();
beta alpha redundant charlie delta
betaRow1 alphaRow1 redundantRow1 charlieRow1 deltaRow1
betaRow2 alphaRow2 redundantRow2 charlieRow2 deltaRow2
betaRow3 alphaRow3 redundantRow3 charlieRow3 deltaRow3
NewAlpha NewBeta NewCharlie
alpha beta charlie
alphaRow1 betaRow1 charlieRow1
alphaRow2 betaRow2 charlieRow2
alphaRow3 betaRow3 charlieRow3
{
"name": "fastcsv",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"fast-csv": "^4.3.6"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment