Public
Script
Forked from stevekrouse/currency
October 23, 2023
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import { fetchJSON } from "https://esm.town/v/stevekrouse/fetchJSON?v=41";
export let convertCurrency = async (desired, base = "usd", amount = 1) => {
let { rates } = await fetchJSON(
`https://open.er-api.com/v6/latest/${base}`,
);
if (rates && rates[desired.toUpperCase()])
return amount * (rates[desired.toUpperCase()]);
else {
let { rates } = await fetchJSON(
"https://api.coingecko.com/api/v3/exchange_rates",
);
return amount * rates[desired.toLowerCase()]?.value /
rates[base.toLowerCase()]?.value;
}
};
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