forked from OSSPhilippines/covid19-tracker-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregularParser.ts
More file actions
89 lines (79 loc) · 2.81 KB
/
regularParser.ts
File metadata and controls
89 lines (79 loc) · 2.81 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import { getAllInfo, getCountryInfo } from "../../getInformation";
import {
convertToPercentage,
normalizeNumbers,
} from "../../libs/numberNormalizers";
/**
*
* @returns Object containing epoch timestamp of when the data was updated, and an array containing the rows for the table
*/
export const globalInfo: () => Promise<{
timeUpdated: number;
rowsOfData: (string | string[])[];
}> = async () => {
// Get raw data from getAllInfo
const { updated, data } = await getAllInfo();
let { cases, deaths, recovered, deathRate, recoveryRate } = data;
// Parse data and convert into rows
let dataInStringArray = normalizeNumbers(cases, deaths, recovered);
let [deathRatePercent, recoveryRatePercent] = convertToPercentage(
deathRate,
recoveryRate
);
dataInStringArray.push(deathRatePercent, recoveryRatePercent);
let rowsOfData = [
[
"Cases".magenta,
"Deaths".red,
"Recovered".green,
"Mortality %".red,
"Recovered %".green,
],
dataInStringArray,
];
return { timeUpdated: updated, rowsOfData };
};
/**
*
* @param country Country string
* @returns An object containing the rows for the table,
* API countryname, the formal countryname, and the unix epoch timestamp of when the data was updated
*/
export const countryInfo: (
country: string
) => Promise<{
timeUpdated: number;
formalCountryName: string;
apiCountryName: string;
rowsOfData: (string | string[])[];
}> = async (country) => {
// prettier-ignore
const { updated, formalCountryName, data, apiCountryName } = await getCountryInfo(country);
// prettier-ignore
let {active, cases, casesPerOneMillion, critical, deathRate, deaths,recovered, recoveryRate, todayCases, todayDeaths} = data;
let [deathRatePercent, recoveryRatePercent] = convertToPercentage(
deathRate,
recoveryRate
);
// prettier-ignore
let normalizedNumbers = normalizeNumbers(cases, deaths, recovered, active, casesPerOneMillion, todayCases, todayDeaths, critical);
// First row contains the first 5 arguments that were passed to normalizeNumbers
let firstRow = normalizedNumbers.slice(0, 5);
// Second row contains the rest of the arguments
let secondRow = normalizedNumbers.slice(5);
// Then we push the percentages
secondRow.push(deathRatePercent, recoveryRatePercent);
// prettier-ignore
let rowsOfData: (string | string[])[] = [
[ "Cases".magenta, "Deaths".red, "Recovered".green, "Active".blue, "Cases/Million".blue,],
firstRow,
[ "Today Cases".magenta, "Today Deaths".red, "Critical".red, "Mortaility %".red, "Recovery %".green],
secondRow
];
return {
timeUpdated: updated,
formalCountryName,
apiCountryName,
rowsOfData,
};
};