-
Notifications
You must be signed in to change notification settings - Fork 0
/
converter.js
105 lines (93 loc) · 2.77 KB
/
converter.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
const fs = require('node:fs/promises');
const { resolve } = require('node:path');
const xml2js = require('xml2js');
const parser = new xml2js.Parser();
function getName(ccynm) {
var name = '';
if (typeof ccynm === 'object' && ccynm !== null) {
name = ccynm['_'];
} else {
name = ccynm;
}
return name;
}
async function readXML() {
try {
const filePath = resolve('./list-one.xml');
const contents = await fs.readFile(filePath, { encoding: 'utf8' });
var countries = [];
parser.parseString(contents, (err, result) => {
if(err) {
return console.log(err);
}
countries = result['ISO_4217']['CcyTbl'][0]['CcyNtry'];
return result['ISO_4217']['CcyTbl'][0]['CcyNtry'];
});
return countries;
} catch (err) {
console.error(err.message);
}
}
async function readSymbols() {
const filePath = resolve('./currency_symbols.csv');
const contents = await fs.readFile(filePath, { encoding: 'utf8' });
var symbols = {};
var lines = contents.split('\n');
lines.forEach((line, index) => {
if (index == 0) return;
var sym = line.split(',');
if (sym[0] == '') return;
symbols[sym[0]] = sym[1];
});
return symbols;
}
async function directoryExists(path) {
try {
await fs.stat(path);
} catch (e) {
return false;
}
return true;
}
async function writeCurrencyFiles(currencies) {
if (!(await directoryExists('currencies'))) {
await fs.mkdir('currencies');
}
if (!(await directoryExists())) {
await fs.mkdir('currencies/iso4217');
}
var indexjs = '';
for (var code in currencies) {
var currency = currencies[code];
var fileData = JSON.stringify(currency, null, ' ');
fileData = fileData.replace(/\n\s\s\"/g, '\n ');
fileData = fileData.replace(/\":\s/g, ': ');
fileData = 'import { type Currency } from \'../../../../types/currency\';\n\n'
+ 'export const ' + code + ': Currency = ' + fileData + '\n';
await fs.writeFile('./currencies/iso4217/'+code+'.ts', fileData);
indexjs += 'export * from \'.\/' + code + '\';\n';
}
await fs.writeFile('./currencies/iso4217/index.ts', indexjs);
}
async function run() {
var iso4217 = await readXML();
var symbols = await readSymbols();
var currencies = {};
iso4217.forEach((country) => {
if(!country.Ccy) return;
if(!currencies.hasOwnProperty(country.Ccy)){
currencies[country.Ccy] = {
code: country.Ccy[0],
name: getName(country.CcyNm[0]),
number: Number(country.CcyNbr),
minorUnits: (country.CcyMnrUnts === 'N.A.' ? null : Number(country.CcyMnrUnts)),
symbol: symbols[country.Ccy[0]],
countries: [ country.CtryNm[0] ],
};
} else {
currencies[country.Ccy].countries.push(country.CtryNm[0]);
}
});
await writeCurrencyFiles(currencies);
}
run();