Public
Cron
Forked from stevekrouse/currency
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
import { email } from "https://esm.town/v/std/email";
import { fetch } from "https://esm.town/v/std/fetch";
function fetchJSON(url) {
return fetch(url).then(res => res.json());
}
export let currency = async (desired, base = "usd", amount = 1) => {
console.log(desired);
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;
}
};
const twd = await currency("twd");
const text = `
1 USD = ${twd.toFixed(2)} TWD
100 USD = ${(100 * twd).toFixed(2)} TWD
100 TWD = ${(100 / twd).toFixed(2)} USD
1000 TWD = ${(1000 / twd).toFixed(2)} USD
`;
await email({ subject: `1000 TWD = ${(1000 / twd).toFixed(2)} USD`, text: text });
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!
July 1, 2024