Skip to content

Commit

Permalink
csv to xml format
Browse files Browse the repository at this point in the history
  • Loading branch information
Khanh Dao authored and Khanh Dao committed Jun 15, 2021
1 parent 0e2348d commit 702eb51
Show file tree
Hide file tree
Showing 5 changed files with 657 additions and 0 deletions.
38 changes: 38 additions & 0 deletions csv-to-xml/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const csv = require('csv-parser')
const fs = require('fs')

const myArgs = process.argv.slice(2);
console.log('myArgs: ', myArgs);

const filename = myArgs[0];
const keyText = myArgs[1] ?? 'key';
const valueText = myArgs[2] ?? 'value';

const results = [];

fs.createReadStream(filename)
.pipe(csv())
.on('data', (data) => {
// console.log('>>>> data', data);
results.push({
id: data['category_id'],
vanity_name: data['Vanity URL']?.replace('https://linkedin.com/products/categories/', ''),
});
})
.on('end', () => {
let str = '';
for (let i = 0; i < results.length; i++) {
const { id, vanity_name } = results[i];
str += `\u0020\u0020\<entry ${keyText}="${id}" ${valueText}="${vanity_name}"/>\n`;
}
const final_str = `<map>\n${str}</map>`;
if (!!final_str) {
fs.writeFile('result.txt', final_str, (err) => {
// throws an error, you could also catch it here
if (err) throw err;

// success case, the file was saved
console.log('Line saved!');
});
}
});
6 changes: 6 additions & 0 deletions csv-to-xml/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"dependencies": {
"csv-parse": "^4.15.4",
"csv-parser": "^3.0.0"
}
}
Loading

0 comments on commit 702eb51

Please sign in to comment.