-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.ts
32 lines (26 loc) · 958 Bytes
/
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
import { Observable, fromEvent, combineLatest } from 'rxjs';
import { map } from 'rxjs/operators';
import { PeriodLifeExpectancyData } from './types';
const data: PeriodLifeExpectancyData = require('./data/ssa-period/data.json');
const age = document.getElementById('age');
const visits = document.getElementById('visits');
const remaining = document.getElementById('remaining');
function getNumber$(element: HTMLElement): Observable<number> {
return fromEvent(element, 'input').pipe(
map(e => parseInt((e.target as HTMLInputElement).value, 10))
);
}
const age$ = getNumber$(age!);
const visits$ = getNumber$(visits!);
const remaining$ = combineLatest([age$, visits$]).pipe(
map(([age, visits]) => {
const expectancy = data.male[age] ? data.male[age].expectancy : 0.63
return Math.max(expectancy * visits, 0)
})
);
remaining$.subscribe(r => {
if (isNaN(r)) {
return;
}
remaining!.innerText = Math.floor(r).toString();
});