-
Notifications
You must be signed in to change notification settings - Fork 27
/
index.ts
45 lines (38 loc) · 1.12 KB
/
index.ts
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import { combine, Stream } from "@funkia/hareactive";
import { elements, runComponent, component } from "../../src";
const { input, div, label } = elements;
type On = {
fahrenChange: Stream<string>;
celsiusChange: Stream<string>;
};
const getValue = (ev: any) => ev.currentTarget.value;
const parseNumbers = (s: Stream<string>) =>
s.map(parseFloat).filter((n) => !isNaN(n));
const main = component<On>((on) => {
const fahrenNrChange = parseNumbers(on.fahrenChange);
const celsiusNrChange = parseNumbers(on.celsiusChange);
const celsius = combine(
celsiusNrChange,
fahrenNrChange.map((f) => (f - 32) / 1.8)
);
const fahren = combine(
fahrenNrChange,
celsiusNrChange.map((c) => (c * 9) / 5 + 32)
);
return div([
div([
label("Fahrenheit"),
input({ value: fahren }).use((o) => ({
fahrenChange: o.input.map(getValue)
}))
]),
div([
label("Celsius"),
input({ value: celsius }).use((o) => ({
celsiusChange: o.input.map(getValue)
}))
])
]);
});
// `runMain` should be the only impure function in application code
runComponent("#mount", main);