1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import { getExchangeRates } from "https://esm.town/v/mgruel/getExchangeRates";
/** Calls https://www.exchangerate-api.com to retrieve the currency exchange rates
* for the provided base currency or "eur" if undefined
* @param {string} desired - The desired currency convert to
* @param {number} amount - The amount of target currency
* @param {string} base - The base currency, from which the exchange should be calculated
*/
export async function exchangeCurrency(
desired: string,
amount = 1,
base = "eur",
): Promise<number | undefined> {
let { rates } = await getExchangeRates(base);
if (rates && rates[desired.toUpperCase()])
return (amount ?? 1) * (rates[desired.toUpperCase()]);
else {
return undefined;
}
}
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
October 23, 2023