Skip to content

Commit d58e381

Browse files
xandrissagarkarira
authored andcommitted
add basic CLI support, --emojis flag, [country] positional argument
1 parent 1a97543 commit d58e381

File tree

7 files changed

+320
-32
lines changed

7 files changed

+320
-32
lines changed

app.js

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const port = process.env.PORT || 3001;
77

88
const { getCountryTable, getJSONData, getJSONDataForCountry } = require('./lib/byCountry');
99
const { getCompleteTable } = require('./lib/corona');
10-
const { countryUpperCase } = require('./lib/helpers');
10+
const { countryUpperCase, lookupCountry } = require('./lib/helpers');
1111

1212

1313
function errorHandler(error, res) {
@@ -34,11 +34,10 @@ app.get('/', (req, res) => {
3434
});
3535

3636
app.get('/:country', (req, res) => {
37-
const { country } = countryUpperCase(req.params);
38-
let lookupObj = null;
37+
const { country } = req.params;
3938
const format = req.query.format ? req.query.format : '';
4039

41-
if (!country || country === 'All') {
40+
if (!country || 'ALL' === country.toUpperCase()) {
4241
if (format.toLowerCase() === 'json') {
4342
return getJSONData().then(result => {
4443
res.setHeader('Cache-Control', 's-maxage=900');
@@ -53,13 +52,8 @@ app.get('/:country', (req, res) => {
5352
}).catch(error => errorHandler(error, res));
5453
}
5554

56-
try {
57-
lookupObj = lookup.byIso(country)
58-
|| lookup.byFips(country)
59-
|| lookup.byCountry(country);
60-
} catch (error) {
61-
lookupObj = lookup.byFips(country) || lookup.byCountry(country);
62-
}
55+
let lookupObj = lookupCountry(country);
56+
6357
if (!lookupObj) {
6458
return res.send(`
6559
Country not found.
@@ -70,6 +64,7 @@ app.get('/:country', (req, res) => {
7064
- /India: for India.
7165
`);
7266
}
67+
7368
const { iso2 } = lookupObj;
7469

7570
if (format.toLowerCase() === 'json') {

bin/index.js

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,56 @@
11
#!/usr/bin/env node
22

33
const { getCompleteTable } = require('../lib/corona');
4+
const { getCountryTable } = require('../lib/byCountry');
5+
const { lookupCountry } = require('../lib/helpers');
46

5-
getCompleteTable()
7+
const {argv} = require('yargs')
8+
.command('$0 [country]', 'show COVID-19 statistics for the world or the given country', yargs =>
9+
yargs.positional('country', {
10+
coerce(arg) {
11+
console.log(arg);
12+
if('ALL' === arg.toUpperCase()) {
13+
return 'ALL';
14+
}
15+
16+
const country = lookupCountry(arg);
17+
18+
if(!country) {
19+
throw new Error(`Country '${arg}' not found.
20+
Try full country name or country code.
21+
Ex:
22+
- UK: for United Kingdom
23+
- US: for United States of America.
24+
- India: for India.`);
25+
}
26+
27+
return country.iso2;
28+
},
29+
describe: 'which country to show statistics for',
30+
default: 'all',
31+
type: 'string'
32+
})
33+
)
34+
.options({
35+
e: {
36+
alias: 'emojis',
37+
describe: 'enable the use of emojis in the table (may break alignment in some terminals)',
38+
default: false,
39+
type: 'boolean'
40+
},
41+
c: {
42+
alias: 'color',
43+
describe: 'enable the use of color in the table.',
44+
type: 'boolean'
45+
}
46+
})
47+
.strict()
48+
.help('help');
49+
50+
console.log(argv);
51+
52+
const {emojis, country} = argv;
53+
54+
(country === 'all' ? getCompleteTable(argv) : getCountryTable(country, argv))
655
.then(console.log)
756
.catch(console.error);

lib/byCountry.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,20 +79,20 @@ exports.getJSONDataForCountry = async (countryCode) => {
7979
return countryData;
8080
}
8181

82-
exports.getCountryTable = async (countryCode) => {
82+
exports.getCountryTable = async (countryCode, {emojis=false}={}) => {
8383
const head = [
8484
'',
8585
'State',
8686
'Confirmed',
87-
'Recovered',
88-
'Deaths',
89-
'Active',
87+
`Recovered${emojis ? ' 😀' : ''}`,
88+
`Deaths${emojis ? ' 😞' : ''}`,
89+
`Active${emojis ? ' 😷' : ''}`,
9090
'Mortality %',
9191
'Recovered %',
9292
'1 Day ▲',
9393
'1 Week ▲',
9494
// 'RoG',
95-
// '🏳',
95+
...( emojis ? ['🏳'] : [] ),
9696
];
9797
const table = new Table({
9898
head,
@@ -105,6 +105,11 @@ exports.getCountryTable = async (countryCode) => {
105105
const { latest, confirmed, deaths, recovered } = data;
106106
const countryData = getDataByState(confirmed, deaths, recovered)
107107
.filter(obj => obj.countryCode === countryCode);
108+
109+
if(countryData.length === 0) {
110+
throw new Error(`Country code ${countryCode} does not match anything in the database.`);
111+
}
112+
108113
const totalStats = getTotalStats(countryData);
109114
table.push({
110115
[countryData[0].country]: [
@@ -136,7 +141,7 @@ exports.getCountryTable = async (countryCode) => {
136141
getOneDayChange(cd),
137142
getOneWeekChange(cd),
138143
// getRateOfGrowth(cd),
139-
// getEmoji(cd.countryCode),
144+
... (emojis ? [countryEmoji] : [])
140145
]
141146
table.push({ [rank++]: values })
142147
});

lib/corona.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,20 +79,20 @@ function extraStats(dataArr) {
7979
);
8080
}
8181

82-
exports.getCompleteTable = async () => {
82+
exports.getCompleteTable = async ({emojis=false}={}) => {
8383
const head = [
8484
'',
8585
'Country',
8686
'Confirmed',
87-
'Recovered',
88-
'Deaths',
89-
'Active',
87+
`Recovered${emojis ? ' 😀' : ''}`,
88+
`Deaths${emojis ? ' 😞' : ''}`,
89+
`Active${emojis ? ' 😷' : ''}`,
9090
'Mortality %',
9191
'Recovered %',
9292
'1 Day ▲',
9393
'1 Week ▲',
9494
// 'RoG',
95-
// '🏳',
95+
...( emojis ? ['🏳'] : [] ),
9696
];
9797
const table = new Table({
9898
head,
@@ -117,7 +117,7 @@ exports.getCompleteTable = async () => {
117117
getOneDayChange(worldStats),
118118
getOneWeekChange(worldStats),
119119
// '',
120-
// '🌎'
120+
... ( emojis ? ['🌎'] : [] )
121121
]
122122
})
123123
let rank = 1;
@@ -134,7 +134,7 @@ exports.getCompleteTable = async () => {
134134
getOneDayChange(cd),
135135
getOneWeekChange(cd),
136136
// getRateOfGrowth(cd),
137-
// getEmoji(cd.countryCode),
137+
... (emojis ? [countryEmoji] : [])
138138
]
139139
table.push({ [rank++]: values })
140140
});

lib/helpers.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const h = require('humanize-number');
33
const emojiFlags = require('emoji-flags');
44
const _ = require('lodash');
55
const moment = require('moment');
6+
const lookup = require('country-code-lookup');
67
const e = exports;
78

89
e.getCountry = (country) => {
@@ -145,14 +146,24 @@ e.getTotalStats = (countryData) => {
145146
return worldStats;
146147
};
147148

148-
e.countryUpperCase = (countryParams) => {
149-
if(countryParams.country.length > 2 ){
150-
const country = countryParams.country.toLowerCase().split(' ').map(word => word.charAt(0).toUpperCase() + word.slice(1)).join(' ');
151-
return { country };
149+
e.countryUpperCase = country => {
150+
if(country.length > 2){
151+
return country.toLowerCase().split(/\s+/).map(word => word.charAt(0).toUpperCase() + word.slice(1)).join(' ');
152152
}
153-
return countryParams;
153+
return country;
154154
};
155155

156+
e.lookupCountry = country => {
157+
country = e.countryUpperCase(country);
158+
try {
159+
return lookup.byIso(country)
160+
|| lookup.byFips(country)
161+
|| lookup.byCountry(country);
162+
} catch (error) {
163+
return lookup.byFips(country) || lookup.byCountry(country);
164+
}
165+
}
166+
156167
e.footer = (lastUpdated) => `
157168
158169
Stay safe. Stay inside.

0 commit comments

Comments
 (0)