heynote/src/currency.js
Jonatan Heyman a415247e48 Support currency conversions with lower case currency codes
(except for the currency CUP since it clashes with the measurement unit "cup")
2023-07-04 14:12:17 +02:00

19 lines
809 B
JavaScript

let currenciesLoaded = false
export async function loadCurrencies() {
const data = await window.heynote.getCurrencyData()
if (!currenciesLoaded)
math.createUnit(data.base, {override:currenciesLoaded, aliases:[data.base.toLowerCase()]})
Object.keys(data.rates)
.filter(function (currency) {
return currency !== data.base
})
.forEach(function (currency) {
math.createUnit(currency, {
definition: math.unit(1 / data.rates[currency], data.base),
aliases: currency === "CUP" ? [] : [currency.toLowerCase()], // Lowercase CUP clashes with the measurement unit cup
}, {override: currenciesLoaded})
})
currenciesLoaded = true
window.document.dispatchEvent(new Event("currenciesLoaded"))
}