1const createCsvWriter = require('csv-writer').createObjectCsvWriter;
2const csvWriter = createCsvWriter({
3 path: 'out.csv',
4 header: [
5 {id: 'name', title: 'Name'},
6 {id: 'surname', title: 'Surname'},
7 {id: 'age', title: 'Age'},
8 {id: 'gender', title: 'Gender'},
9 ]
10});
11
12const data = [
13 {
14 name: 'John',
15 surname: 'Snow',
16 age: 26,
17 gender: 'M'
18 }, {
19 name: 'Clair',
20 surname: 'White',
21 age: 33,
22 gender: 'F',
23 }, {
24 name: 'Fancy',
25 surname: 'Brown',
26 age: 78,
27 gender: 'F'
28 }
29];
30
31csvWriter
32 .writeRecords(data)
33 .then(()=> console.log('The CSV file was written successfully'));
34
1const fs = require('fs')
2const csv = require('csv-parser')
3const randomWords = require('random-words')
4const users = [];
5function generateUsername(firstname, surname) {
6 return `${firstname[0]}-${surname}`.toLowerCase();
7}
8fs.createReadStream('input.csv')
9 .pipe(csv())
10 .on('data', function (row) {
11 const username = generateUsername(row.Firstname, row.Surname);
12 const password = randomWords(3).join("-");
13
14 const user = {
15 username,
16 firstname: row.Firstname,
17 surname: row.Surname,
18 roles: row.Roles,
19 password
20 }
21 users.push(user)
22 })
23 .on('end', function () {
24 console.table(users)
25 // TODO: SAVE users data to another file
26 })
27
1const csv = require('csv-parser');
2const fs = require('fs');
3
4fs.createReadStream('data.csv')
5 .pipe(csv())
6 .on('data', (row) => {
7 console.log(row);
8 })
9 .on('end', () => {
10 console.log('CSV file successfully processed');
11 });
12