forked from sagarkarira/coronavirus-tracker-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.js
More file actions
165 lines (143 loc) · 4.05 KB
/
helpers.js
File metadata and controls
165 lines (143 loc) · 4.05 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
const chalk = require('chalk');
const h = require('humanize-number');
const emojiFlags = require('emoji-flags');
const _ = require('lodash');
const moment = require('moment');
const e = exports;
e.getCountry = (country) => {
return chalk.cyan.bold(country);
};
e.getState = (state) => {
if (state) {
return chalk.red(state);
}
return chalk.red('ALL');
};
e.getConfirmed = (confirmed) => {
return {
content: chalk.blueBright(h(confirmed)),
hAlign: 'right',
};
};
e.getRecovered = (recovered) => {
return {
content: chalk.yellowBright(h(recovered)),
hAlign: 'right',
};
};
e.getDeaths = (deaths) => {
return {
content: chalk.whiteBright(h(deaths)),
hAlign: 'right',
};
};
e.getActive = (active) => {
return {
content: chalk.magentaBright(h(active)),
hAlign: 'right'
};
};
e.getMortalityPer = (mortalityPer) => ({
content: chalk.redBright(mortalityPer),
hAlign: 'right',
});
e.getRecoveredPer = (recoveredPer) => ({
content: chalk.greenBright(recoveredPer),
hAlign: 'right',
});
e.getEmoji = (countryCode) => {
if (countryCode && emojiFlags.countryCode(countryCode)) {
return emojiFlags.countryCode(countryCode).emoji;
}
return '';
};
e.calActive = ({ confirmed, recovered, deaths }) => confirmed - (recovered + deaths);
e.calMortalityPer = ({ confirmed, deaths }) => ((deaths / confirmed) * 100).toFixed(2);
e.calRecoveredPer = ({ confirmed, recovered }) => ((recovered / confirmed) * 100).toFixed(2);
/**
historyObj = {
"1/22/20": 0,
"1/23/20": 1,
"1/24/20": 4,
"1/25/20": 7,
}
Returns sorted arr of above elements - [0, 1, 4, 7]
*/
e.historyObjToArr = (historyObj) => {
const sortedTimestampArr = _.sortBy(
Object.keys(historyObj).map(date => new Date(date).getTime()),
Number
);
return sortedTimestampArr.map(timestamp => {
const dateFormatted = moment(timestamp).format('M/D/YY');
return historyObj[dateFormatted];
});
};
/**
* Given both arr1 and arr2 has same number of elements
* Returns -> sum[n] = arr1[n] + arr2[n]
*
*/
e.addArr = (arr1, arr2) => {
if (arr1.length === 0 ) {
return arr2;
}
if (arr2.length === 0) {
return arr1;
}
return arr1.map((val, index) => arr1[index] + arr2[index]);
};
e.getOneDayChange = ({ confirmedByDay }) => {
const present = confirmedByDay.length - 1;
const dailyChange = confirmedByDay[present] - confirmedByDay[present - 1];
return {
content: `${h(dailyChange)} ▲`,
hAlign: 'right',
};
};
e.getOneWeekChange = ({ confirmedByDay }) => {
const present = confirmedByDay.length - 1;
const weeklyChange = confirmedByDay[present] - confirmedByDay[present - 7];
return {
content: `${h(weeklyChange)} ▲`,
hAlign: 'right',
};
};
e.getRateOfGrowth = ( { confirmedByDay }) => {
return confirmedByDay[0];
};
e.getTotalStats = (countryData) => {
const worldStats = countryData.reduce((acc, countryObj) => {
acc.confirmed += countryObj.confirmed;
acc.recovered += countryObj.recovered;
acc.deaths += countryObj.deaths;
acc.confirmedByDay = e.addArr( acc.confirmedByDay, countryObj.confirmedByDay);
acc.recoveredByDay = e.addArr( acc.recoveredByDay, countryObj.recoveredByDay);
acc.deathsByDay = e.addArr( acc.deathsByDay, countryObj.deathsByDay);
return acc;
}, {
confirmed: 0,
recovered: 0,
deaths: 0,
confirmedByDay: [],
recoveredByDay: [],
deathsByDay: [],
});
worldStats.active = e.calActive(worldStats);
worldStats.recoveredPer = e.calRecoveredPer(worldStats);
worldStats.mortalityPer = e.calMortalityPer(worldStats);
return worldStats;
};
e.countryUpperCase = (countryParams) => {
if(countryParams.country.length > 2 ){
const country = countryParams.country.toLowerCase().split(' ').map(word => word.charAt(0).toUpperCase() + word.slice(1)).join(' ');
return { country };
}
return countryParams;
};
e.footer = (lastUpdated) => `
Stay safe. Stay inside.
Code: https://github.com/sagarkarira/coronavirus-tracker-cli
Twitter: https://twitter.com/ekrysis
Last Updated on: ${moment(lastUpdated).utc().format('DD-MMM-YYYY HH:MM')} UTC
`;