Skip to content

Commit 5b1ae1d

Browse files
committed
init
0 parents  commit 5b1ae1d

File tree

6 files changed

+520
-0
lines changed

6 files changed

+520
-0
lines changed

.eslintrc.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"env": {
3+
"browser": true,
4+
"commonjs": true,
5+
"es6": true
6+
},
7+
"extends": "eslint:recommended",
8+
"parserOptions": {
9+
"sourceType": "module"
10+
},
11+
"rules": {
12+
"indent": [
13+
"error",
14+
2
15+
],
16+
"linebreak-style": [
17+
"error",
18+
"unix"
19+
],
20+
"quotes": [
21+
"error",
22+
"single"
23+
],
24+
"semi": [
25+
"error",
26+
"always"
27+
]
28+
}
29+
}

bin/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/env node
2+
3+
const { getCompleteTable } = require('../lib/corona');
4+
5+
getCompleteTable()
6+
.then(console.log)
7+
.catch(console.error);

lib/corona.js

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
const Table = require('cli-table3');
2+
const axios = require('axios');
3+
const _ = require('lodash');
4+
const chalk = require('chalk');
5+
const emojiFlags = require('emoji-flags');
6+
const h = require('humanize-number');
7+
8+
function getConfirmed(confirmed) {
9+
return {
10+
content: chalk.blueBright(h(confirmed)),
11+
hAlign: 'right',
12+
};
13+
}
14+
15+
function getRecoverd(recovered) {
16+
return {
17+
content: chalk.yellowBright(h(recovered)),
18+
hAlign: 'right',
19+
};
20+
}
21+
22+
function getDeaths(deaths) {
23+
return {
24+
content: chalk.grey(h(deaths)),
25+
hAlign: 'right',
26+
}
27+
}
28+
29+
function getActive(confirmed, recovered, deaths) {
30+
return {
31+
content: h(confirmed - recovered - deaths),
32+
hAlign: 'right'
33+
}
34+
}
35+
36+
function getMortalityRate(deaths, confirmed, average = null) {
37+
const mortalityRate = ((deaths / confirmed) * 100).toFixed(2);
38+
return chalk.redBright(mortalityRate);
39+
}
40+
41+
function getRecoveredRate(recovered, confirmed, average = null) {
42+
const recoveredRate = ((recovered / confirmed) * 100).toFixed(2);
43+
return chalk.greenBright(recoveredRate);
44+
}
45+
46+
function getEmoji(countryCode) {
47+
if (countryCode && emojiFlags.countryCode(countryCode)) {
48+
return emojiFlags.countryCode(countryCode).emoji;
49+
}
50+
return '';
51+
}
52+
53+
function getDataByCountry(confirmed, deaths, recovered) {
54+
const countryMap = {};
55+
const confirmedMap = _.keyBy(confirmed.locations, (i) => i.country + i.province);
56+
const recoveredMap = _.keyBy(recovered.locations, (i) => i.country + i.province);
57+
const deathsMap = _.keyBy(deaths.locations, (i) => i.country + i.province);
58+
confirmed.locations.forEach(obj => {
59+
const countryName = obj.country;
60+
const provinceName = obj.province;
61+
const mapKey = countryName + provinceName;
62+
if (!countryMap[countryName]) {
63+
countryMap[countryName] = {
64+
country: countryName,
65+
countryCode: obj.country_code,
66+
confirmed: confirmedMap[mapKey].latest,
67+
recovered: recoveredMap[mapKey].latest,
68+
deaths: deathsMap[mapKey].latest,
69+
};
70+
} else {
71+
countryMap[countryName].confirmed += confirmedMap[mapKey].latest;
72+
countryMap[countryName].recovered += recoveredMap[mapKey].latest;
73+
countryMap[countryName].deaths += deathsMap[mapKey].latest;
74+
}
75+
});
76+
const countryArr = Object.keys(countryMap).map(key => countryMap[key]);
77+
return _.sortBy(countryArr, (o) => -o.confirmed)
78+
}
79+
80+
exports.getCompleteTable = async () => {
81+
const head = [
82+
'',
83+
'Country',
84+
'Confirmed ✅',
85+
'Recovered 😃',
86+
'Deaths 😞',
87+
'Active 😷',
88+
'Mortality %',
89+
'Recovered %',
90+
'Flag 🏳'
91+
];
92+
const table = new Table({
93+
head,
94+
chars: { 'top': '═' , 'top-mid': '╤' , 'top-left': '╔' , 'top-right': '╗'
95+
, 'bottom': '═' , 'bottom-mid': '╧' , 'bottom-left': '╚' , 'bottom-right': '╝'
96+
, 'left': '║' , 'left-mid': '╟' , 'mid': '─' , 'mid-mid': '┼'
97+
, 'right': '║' , 'right-mid': '╢' , 'middle': '│' }
98+
});
99+
const result = await axios('https://coronavirus-tracker-api.herokuapp.com/all');
100+
const { latest, confirmed, deaths, recovered } = result.data;
101+
const countryData = getDataByCountry(confirmed, deaths, recovered)
102+
table.push({
103+
'': [
104+
'World',
105+
getConfirmed(latest.confirmed),
106+
getRecoverd(latest.recovered),
107+
getDeaths(latest.deaths),
108+
getActive(latest.confirmed, latest.recovered, latest.deaths),
109+
getMortalityRate(latest.deaths, latest.confirmed),
110+
getRecoveredRate(latest.recovered, latest.confirmed),
111+
'🌎'
112+
]
113+
})
114+
let rank = 1;
115+
countryData.forEach(cd => {
116+
const countryEmoji = getEmoji(cd.countryCode);
117+
const values = [
118+
chalk.cyanBright(cd.country),
119+
getConfirmed(cd.confirmed),
120+
getRecoverd(cd.recovered),
121+
getDeaths(cd.deaths),
122+
getActive(cd.confirmed, cd.recovered, cd.deaths),
123+
getMortalityRate(cd.deaths, cd.confirmed),
124+
getRecoveredRate(cd.deaths, cd.confirmed),
125+
getEmoji(cd.countryCode),
126+
]
127+
table.push({ [rank++]: values })
128+
});
129+
return table.toString();
130+
}

0 commit comments

Comments
 (0)