-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
161 lines (126 loc) · 4.75 KB
/
index.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import fetch from 'node-fetch';
import * as XLSX from 'xlsx/xlsx.mjs';
const yearRanges = [
{ range: [1983, 1986], slug: '1983-1986' },
{ range: [1987, 1990], slug: '1987-1990' },
{ range: [1991, 1994], slug: '1991-1994' },
{ range: [1995, 1998], slug: '1995-1998' },
{ range: [1999, 2002], slug: '1999-2002' },
{ range: [2003, 2006], slug: '2003-2006' },
{ range: [2007, 2009], slug: '2007-2009' },
{ range: [2010, 2013], slug: '2010-2013' },
{ range: [2014, 2017], slug: '2014-2017' },
{ range: [2018, 2022], slug: '2018-2022' },
{ range: [2023, 3000], slug: '2023-current' },
];
const minYear = yearRanges.reduce((currMinYear, { range }) => Math.min(currMinYear, ...range), Infinity);
const MAX_DAYS_BETWEEN = 7;
const currencyNameMap = {
USD: "FXRUSD",
TWI: "FXRTWI",
CNY: "FXRCR",
JPY: "FXRJY",
EUR: "FXREUR",
KRW: "FXRSKW",
GBP: "FXRUKPS",
SGD: "FXRSD",
INR: "FXRIRE",
THB: "FXRTB",
NZD: "FXRNZD",
TWD: "FXRNTD",
MYR: "FXRMR",
IDR: "FXRIR",
VND: "FXRVD",
AED: "FXRUAED",
PGK: "FXRPNGK",
HKD: "FXRHKD",
CAD: "FXRCD",
ZAR: "FXRSARD",
CHF: "FXRSF",
PHP: "FXRPHP",
SDR: "FXRSDR"
};
const DATE_KEY = 'Series ID';
const cachedRates = {};
export default async function getExchangeRate(currency, exchangeRateDate, { defaultToClosestPriorRate = false, defaultToNull = false } = {}) {
if (typeof currency !== 'string') {
throw new TypeError(`Expected a string, got ${typeof currency}`);
}
if (!Object.hasOwn(currencyNameMap, currency)) {
throw new TypeError(`Unexpected currency code, got ${currency}`);
}
if (typeof exchangeRateDate !== 'object') {
throw new TypeError(`Expected a date object, got ${typeof exchangeRateDate}`);
}
const date = new Date(exchangeRateDate);
date.setHours(0, 0, 0, 0);
if (date.getFullYear() < minYear) {
return null;
}
const getRate = (rateList) => defaultToNull
? rateList.find(rate => rate[DATE_KEY].toDateString() === date.toDateString())
: findClosestRate(rateList, date, currencyNameMap[currency], defaultToClosestPriorRate);
const year = date.getFullYear();
let rates = await getRatesForYear(year);
let rate = getRate(rates);
const isBeforeMinDate = (rateList) => date.getFullYear() <= minYear && rateList && rateList[0] && date < rateList[0][DATE_KEY];
if (isBeforeMinDate(rates)) {
return null;
}
if (!rate) {
// could not find rate for given date - may be at end of the dataset (ie 31/12/2022), need to try next dataset
const increment = defaultToClosestPriorRate ? -1 : 1;
rates = await getRatesForYear(year + increment);
rate = getRate(rates);
if (isBeforeMinDate(rates)) {
return null;
}
}
return rate ? rate[currencyNameMap[currency]] : null;
}
async function getRatesForYear(year) {
const { slug } = yearRanges.find(({ range }) => range[0] <= year && year <= range[1]) ?? { slug: null };
if (!slug) return null;
return await getRatesFromRba(slug);
}
async function getRatesFromRba(slug) {
if (cachedRates[slug]) return await cachedRates[slug];
let promiseResolve;
cachedRates[slug] = new Promise((resolve, _) => promiseResolve = resolve);
const response = await fetch(`https://www.rba.gov.au/statistics/tables/xls-hist/${slug}.xls`);
const body = await response.arrayBuffer();
const workbook = XLSX.read(body, { cellDates: true });
// Assuming the data is in the first sheet
const sheetName = workbook.SheetNames[0];
const sheet = workbook.Sheets[sheetName];
// Convert the sheet to JSON
const rates = XLSX.utils.sheet_to_json(sheet, { blankrows: false, range: 10 });
cachedRates[slug] = rates;
promiseResolve(rates);
return rates;
}
function findClosestRate(rates, date, currency, defaultToClosestPriorRate) {
if (!rates) return null;
if (defaultToClosestPriorRate) rates = rates.slice().reverse();
for (let rate of rates) {
if ((rate[DATE_KEY] < date && !defaultToClosestPriorRate) ||
(rate[DATE_KEY] > date && defaultToClosestPriorRate) ||
!rate[currency]) {
continue;
}
// Too many days between the target date and the found record's date - can't use
if (daysBetween(rate[DATE_KEY], date) > MAX_DAYS_BETWEEN) return null;
return rate;
}
return null;
}
// https://stackoverflow.com/a/7763654
function daysBetween(date1, date2) {
const MS_PER_DAY = 24 * 60 * 60 * 1000;
const a = new Date(date1);
const b = new Date(date2);
// Set to noon - avoid DST errors
a.setHours(12, 0, 0, 0);
b.setHours(12, 0, 0, 0);
return Math.abs(Math.round((a - b) / MS_PER_DAY));
}