Skip to content

Commit 529fec6

Browse files
committed
simple OpenExchange demo
1 parent 286cc5f commit 529fec6

File tree

2 files changed

+155
-0
lines changed

2 files changed

+155
-0
lines changed

test/OpenExchangeRates.js

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
var OpenExchangeRates = function () {
2+
/*! (c) Andrea Giammarchi */
3+
return function OpenExchangeRates(APP_ID) {
4+
var cache = {};
5+
return {
6+
format: (amount, currency) =>
7+
format(amount, currency),
8+
clear: (base) =>
9+
delete cache[(base || 'USD').toUpperCase()],
10+
convert: (amount, from, to) =>
11+
latest(from).then(info =>
12+
format(info.rates[to.toUpperCase()] * amount, to)),
13+
currencies: () =>
14+
cache.currencies || (cache.currencies = new Promise((res, rej) => {
15+
load(urlFor('currencies'), res, rej);
16+
})),
17+
latest: (base) => {
18+
base = (base || 'USD').toUpperCase();
19+
return cache[base] || (cache[base] = new Promise((res, rej) => {
20+
load(`${urlFor('latest')}&base=${base}`, res, rej);
21+
}));
22+
}
23+
};
24+
function urlFor(key) {
25+
return `https://openexchangerates.org/api/${key}.json?app_id=${APP_ID}`;
26+
}
27+
function latest(base) {
28+
base = (base || 'USD').toUpperCase();
29+
return cache[base] || (cache[base] = new Promise((res, rej) => {
30+
load(`${urlFor('latest')}&base=${base}`, res, rej);
31+
}));
32+
}
33+
34+
};
35+
function format(amount, currency) {
36+
return new Intl.NumberFormat('en-US', {
37+
style: 'currency',
38+
currency: currency,
39+
minimumFractionDigits: 2,
40+
maximumFractionDigits: 8
41+
})
42+
.format(amount)
43+
.replace(/^NaN$/, '')
44+
.replace(/^(\D+)/, '$1 ')
45+
.replace('BTC', '₿');
46+
}
47+
function load(url, res, rej) {
48+
var xhr = new XMLHttpRequest();
49+
xhr.open('get', url, true);
50+
xhr.onload = () => res(JSON.parse(xhr.responseText));
51+
xhr.onerror = rej;
52+
xhr.send(null);
53+
}
54+
}();

test/exchange.html

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1">
6+
<title>OpenExchangeRates hyperHTML Demo</title>
7+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.5.1/css/bulma.min.css">
8+
<script defer src="https://unpkg.com/hyperhtml@latest/min.js"></script>
9+
<script defer src="OpenExchangeRates.js"></script>
10+
<script>
11+
addEventListener('DOMContentLoaded', () => {
12+
var hyper = hyperHTML;
13+
var oer = OpenExchangeRates(API_KEY);
14+
var render = hyper(document.querySelector('#exchange'));
15+
var db = JSON.parse(localStorage.getItem('exchange') ||
16+
'{"from": "USD", "to": "EUR"}');
17+
update();
18+
addEventListener('beforeunload', () => {
19+
localStorage.setItem('exchange', JSON.stringify(db));
20+
});
21+
22+
function convert(amount, from, to) {
23+
oer.convert(
24+
parseFloat(db.amount = amount),
25+
db.from = from,
26+
db.to = to
27+
).then(value => update(db.converted = value));
28+
}
29+
30+
function options(selected) {
31+
return oer.currencies().then(currencies =>
32+
Object.keys(currencies).map(currency => hyper`
33+
<option value=${currency} selected=${currency === selected}>
34+
${currency}
35+
</option>`
36+
));
37+
}
38+
39+
function update() {
40+
render`
41+
<div class="field has-addons">
42+
<p class="control">
43+
<input
44+
value=${db.amount}
45+
oninput=${e => convert(e.currentTarget.value, db.from, db.to)}
46+
class="input" type="text" placeholder="amount">
47+
</p>
48+
<p class="control">
49+
<span class="select">
50+
<select disabled
51+
onchange=${e => convert(db.amount, e.currentTarget.value, db.to)}>
52+
${options(db.from)}
53+
</select>
54+
</span>
55+
</p>
56+
</div>
57+
<div class="field has-addons">
58+
<p class="control">
59+
<input value=${db.converted}
60+
class="input" type="text" placeholder="converted">
61+
</p>
62+
<p class="control">
63+
<span class="select">
64+
<select
65+
onchange=${e => convert(db.amount, db.from, e.currentTarget.value)}>
66+
${options(db.to)}
67+
</select>
68+
</span>
69+
</p>
70+
</div>`;
71+
}
72+
}, {once: true});
73+
</script>
74+
<style>
75+
section {
76+
position: absolute;
77+
right: 0;
78+
left: 0;
79+
bottom: 25%;
80+
}
81+
#exchange {
82+
display: inline-block;
83+
}
84+
p.logo {
85+
margin-top: 25%;
86+
}
87+
</style>
88+
</head>
89+
<body class="has-text-centered">
90+
<span id="logo"></span>
91+
<p class="logo is-size-1">💱</p>
92+
<p class="is-size-7 has-text-grey-light">
93+
powered by <a href="https://viperhtml.js.org/">hyper(HTML)</a>
94+
</p>
95+
<section>
96+
<div id="exchange" class="container"></div>
97+
</section>
98+
<script>Object.defineProperty(this,'API_KEY',
99+
{get:()=>atob('MGQxOTU1NWU1N2NiNDJjZGFkOTk4YmQwNjNiMzczN2I=')})</script>
100+
</body>
101+
</html>

0 commit comments

Comments
 (0)