-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.js
35 lines (28 loc) · 887 Bytes
/
generate.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
const fs = require( 'fs' ).promises;
const csvParse = require( 'csv-parse' );
const generateNames = (names) => {
return names.map(async (name) => {
const csvData = await fs.readFile(`${__dirname}/data/${name}.csv`, 'utf8');
const data = await new Promise((resolve, reject) => {
csvParse(csvData, {
columns: true,
cast(value, context) {
const formatedValue = parseInt(value, 10);
if( context.header || isNaN(formatedValue) ){
return value.trim();
}
return formatedValue;
}
}, (err, data) => {
if( err ){
reject(err);
return;
}
resolve(data);
})
})
.catch(() => ([]));
return fs.writeFile( `${__dirname}/functions/${name}.json`, JSON.stringify(data, null, 2), 'utf8' );
});
}
generateNames(['firstnames', 'lastnames']);