Skip to content

Commit 5bb10fb

Browse files
authored
Merge pull request #19 from xandris/yargs
add basic CLI support, --emojis flag, [country] positional argument
2 parents 1a97543 + e1c1404 commit 5bb10fb

File tree

9 files changed

+429
-53
lines changed

9 files changed

+429
-53
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: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,57 @@
11
#!/usr/bin/env node
2-
2+
const yargonaut = require('yargonaut')
3+
.style('green');
4+
const yargs = require('yargs');
5+
const chalk = require('chalk');
36
const { getCompleteTable } = require('../lib/corona');
7+
const { getCountryTable } = require('../lib/byCountry');
8+
const { lookupCountry } = require('../lib/helpers');
9+
10+
const { argv } = yargs
11+
.command('$0 [country]','Tool to COVID-19 statistics for the world or the given country', yargs =>
12+
yargs.positional('country', {
13+
coerce(arg) {
14+
if ('ALL' === arg.toUpperCase()) {
15+
return 'ALL';
16+
}
17+
const country = lookupCountry(arg);
18+
if (!country) {
19+
let error = `Country '${arg}' not found.\n`;
20+
error += 'Try full country name or country code.\n';
21+
error += 'Ex:\n';
22+
error += '- UK: for United Kingdom \n';
23+
error += '- US: for United States of America.\n';
24+
error += '- Italy: for Italy.\n';
25+
throw new Error(chalk.red.bold(error));
26+
}
27+
return country.iso2;
28+
},
29+
describe: 'Filter table by country',
30+
default: 'all',
31+
type: 'string'
32+
})
33+
)
34+
.options({
35+
e: {
36+
alias: 'emojis',
37+
describe: 'Show emojis in table',
38+
default: false,
39+
type: 'boolean'
40+
},
41+
c: {
42+
alias: 'color',
43+
describe: 'Show colors formatted output',
44+
type: 'boolean'
45+
}
46+
})
47+
.strict()
48+
.help('help');
449

5-
getCompleteTable()
50+
const { emojis, country } = argv;
51+
(
52+
country === 'ALL'
53+
? getCompleteTable(emojis)
54+
: getCountryTable(country, emojis)
55+
)
656
.then(console.log)
757
.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: 8 additions & 8 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',
86-
'Confirmed',
87-
'Recovered',
88-
'Deaths',
89-
'Active',
86+
`Confirmed ${emojis ? ' ✅': ''}`,
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,12 +146,22 @@ 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(' ');
152+
}
153+
return country;
154+
};
155+
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);
152164
}
153-
return countryParams;
154165
};
155166

156167
e.footer = (lastUpdated) => `

now.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"builds": [
44
{
55
"src": "*.js",
6-
"use": "@now/node-server"
6+
"use": "@now/node"
77
}
88
],
99
"routes": [

0 commit comments

Comments
 (0)