-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.ts
43 lines (37 loc) · 1.22 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
import NationalData from "./models/national-data.ts";
import DayStats from "./models/day-stats.ts";
import { getDateString } from "./util.ts";
const API_URL = "https://api.covid19india.org/data.json";
main();
/**
* The main function.
* Entry point of the app.
*/
async function main() {
let data: NationalData = await getNationalData();
const yesterdaysData = getYesterdaysData(data);
const totalConfirmed = yesterdaysData.totalconfirmed;
const totalRecovered = yesterdaysData.totalrecovered;
console.log(
`${totalRecovered} out of ${totalConfirmed} people are healthy again 😄`,
);
}
/**
* Fetches national-level COVID 19 data from the API and returns it.
*/
async function getNationalData(): Promise<any> {
const res = await fetch(API_URL);
const data = await res.json();
return data;
}
/**
* Returns one day prior's data from given national data.
*/
export function getYesterdaysData(data: NationalData): DayStats {
const date = new Date();
date.setDate(date.getDate() - 1);
const yesterdayString = getDateString(date) + " "; // space added because of bug in source data
const yesterdaysStats =
data.cases_time_series?.filter((s) => s.date === yesterdayString)[0];
return yesterdaysStats;
}