Repository navigation
Expand file tree
/
Copy pathbuild-csv.mjs
More file actions
120 lines (113 loc) · 3.09 KB
/
Copy pathbuild-csv.mjs
File metadata and controls
120 lines (113 loc) · 3.09 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/env node
/**
* Rebuild the flattened CSVs from the canonical JSON envelopes.
*
* The JSON files (rates.json, limits.json, changes.json) are the source of
* truth, fetched from https://wha.tools/api/data by the refresh workflow. This
* derives the convenience CSVs from them so the two never disagree. Node stdlib
* only — no dependencies, no install step.
*
* Mirrors the CSV shape produced by wha.tools' own scripts/export-open-data.mjs.
*/
import { readFileSync, writeFileSync } from 'node:fs';
const read = (f) => JSON.parse(readFileSync(new URL(`../${f}`, import.meta.url), 'utf8'));
// RFC 4180 quoting + spreadsheet formula-injection guard.
function csvCell(v) {
if (v === null || v === undefined) return '';
let s = String(v);
if (/^[=+\-@\t\r]/.test(s)) s = `'${s}`;
if (/[",\n\r]/.test(s)) s = `"${s.replace(/"/g, '""')}"`;
return s;
}
const toCsv = (headers, rows) =>
[headers, ...rows].map((r) => r.map(csvCell).join(',')).join('\r\n') + '\r\n';
const write = (file, contents) => {
writeFileSync(new URL(`../${file}`, import.meta.url), contents);
console.log(` wrote ${file}`);
};
// --- rates.csv : one row per (currency, market) --------------------------
const rates = read('rates.json');
const rateRows = [];
for (const c of rates.currencies) {
const byMarket = rates.rates[c.code];
if (!byMarket) continue;
for (const m of rates.markets) {
const tuple = byMarket[m.id];
if (!tuple) continue;
const [marketing, utility, authentication, authInternational] = tuple;
rateRows.push([
c.code,
c.name,
m.id,
m.name,
m.isRegion,
marketing,
utility,
authentication,
authInternational ?? '',
]);
}
}
write(
'rates.csv',
toCsv(
[
'currency',
'currency_name',
'market_id',
'market_name',
'is_region',
'marketing',
'utility',
'authentication',
'authentication_international',
],
rateRows,
),
);
// --- limits.csv : flatten the per-area tables ----------------------------
const limits = read('limits.json');
const limitRows = [];
for (const area of limits.areas) {
for (const row of area.rows) {
limitRows.push([area.heading, row.id, row.label, row.value, row.appliesTo, row.note]);
}
}
write('limits.csv', toCsv(['area', 'id', 'label', 'value', 'applies_to', 'note'], limitRows));
// --- changes.csv : the dated ledger --------------------------------------
const changes = read('changes.json');
const changeRows = changes.changes.map((e) => [
e.effective,
e.announced ?? '',
e.area,
e.key ?? '',
e.from ?? '',
e.to ?? '',
e.currency ?? '',
e.provenance,
e.verifiedBy ?? '',
e.summary,
(e.sources ?? []).map((s) => `${s.label} (${s.url})`).join(' | '),
]);
write(
'changes.csv',
toCsv(
[
'effective',
'announced',
'area',
'key',
'from',
'to',
'currency',
'provenance',
'verified_by',
'summary',
'sources',
],
changeRows,
),
);
console.log(
`Done. ${rateRows.length} rate rows, ${limitRows.length} limit rows, ${changeRows.length} change rows.`,
);