-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbuild.js
49 lines (42 loc) · 1.52 KB
/
build.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
const path = require('path');
const fs = require('fs');
const os = require('os');
const csvParse = require('csv-parse');
function read(file, fn) {
var fpath = path.join('assets', file);
if(!fs.existsSync(fpath)) return Promise.resolve();
return new Promise((fres) => {
var stream = fs.createReadStream(fpath).pipe(csvParse({columns: true}));
stream.on('data', fn);
stream.on('end', fres);
});
};
function convert(row) {
var code = row['Code'];
var names = row['Name(s)'];
var color = (row['Colour']||'').replace('(', ', ').replace(')', '');
var type = row['Purpose']!=null? row['Purpose']:`color (${color})`;
var status = row['Status'].search(/Approved in (the EU|\d+)/)>=0? 'e ':'';
status += row['Status'].search(/Approved in the US/)>=0? 'u ':'';
status = status.trim();
return {code, names, type, status};
};
function load() {
var rdy = [], z = [];
for(var i=100; i<=1000; i++)
rdy.push(read(`e${i}.csv`, (row) => z.push(convert(row))));
return Promise.all(rdy).then(() => z);
};
load().then((rows) => {
var c = 'code,names,type,status'+os.EOL;
var z = `const CORPUS = new Map([${os.EOL}`;
rows.sort((a, b) => parseInt(a.code.substring(1), 10)-parseInt(b.code.substring(1), 10));
for(var row of rows) {
c += `${row.code},"${row.names}","${row.type}",${row.status}`+os.EOL;
z += ` ["${row.code}", ${JSON.stringify(row)}],${os.EOL}`;
}
z += `]);${os.EOL}`;
z += `module.exports = CORPUS;${os.EOL}`;
fs.writeFileSync('index.csv', c);
fs.writeFileSync('corpus.js', z);
});