Created
February 23, 2018 08:53
-
-
Save clakech/7bb721fa659257e5a56b3bd9f937813a to your computer and use it in GitHub Desktop.
parse stream csv file to create another csv file with only vanilla NodeJS (readline)
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 readline = require('readline'); | |
const fs = require('fs'); | |
const uuid = require('uuid/v4'); | |
const rl = readline.createInterface({ | |
input: fs.createReadStream('./input.csv'), | |
output: fs.createWriteStream('./output.csv'), | |
}); | |
rl | |
.on('line', function convert(line) { | |
rl.pause(); | |
try { | |
const [firstname, lastname, hobby] = line.split(';'); | |
this.output.write([uuid(), `${firstname} ${lastname}`, hobby, '\n'].join(';')); | |
rl.resume(); | |
} catch (error) { | |
console.error('error handling line %j', line); | |
rl.resume(); | |
} | |
}) | |
.on('close', function onClose() { | |
console.log(`Successfully streamed`); | |
this.output.end(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This
.split
is VERY dangerous. Take the following example, your code won't work: