Last active
October 29, 2021 21:36
-
-
Save dhuang612/08d799b697065fa794ae74f2b86ef20a to your computer and use it in GitHub Desktop.
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
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); | |
})(); |
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
beta | alpha | redundant | charlie | delta | |
---|---|---|---|---|---|
betaRow1 | alphaRow1 | redundantRow1 | charlieRow1 | deltaRow1 | |
betaRow2 | alphaRow2 | redundantRow2 | charlieRow2 | deltaRow2 | |
betaRow3 | alphaRow3 | redundantRow3 | charlieRow3 | deltaRow3 |
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
NewAlpha | NewBeta | NewCharlie | |
---|---|---|---|
alpha | beta | charlie | |
alphaRow1 | betaRow1 | charlieRow1 | |
alphaRow2 | betaRow2 | charlieRow2 | |
alphaRow3 | betaRow3 | charlieRow3 |
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
{ | |
"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