Skip to content

Commit a6e12a5

Browse files
committed
added minimal table, refactored code, added changelog, version 0.5.0
1 parent 66bd918 commit a6e12a5

File tree

7 files changed

+147
-66
lines changed

7 files changed

+147
-66
lines changed

app.js

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,21 @@ const express = require('express');
22
const app = express();
33
const lookup = require('country-code-lookup');
44
const morgan = require('morgan');
5+
const stripAnsi = require('strip-ansi');
56

67
const port = process.env.PORT || 3001;
78

89
const { getCountryTable, getJSONData, getJSONDataForCountry } = require('./lib/byCountry');
910
const { getCompleteTable } = require('./lib/corona');
10-
const { countryUpperCase, lookupCountry } = require('./lib/helpers');
11+
const { lookupCountry } = require('./lib/helpers');
1112

1213

1314
function errorHandler(error, res) {
1415
console.error(error);
15-
return res.send('I am sorry. Something went wrong. Please report it');
16+
return res.send(`
17+
I am sorry. Something went wrong. Please report it \n
18+
${error.message}
19+
`);
1620
}
1721

1822
app.use(morgan(':remote-addr :remote-user :method :url :status :res[content-length] - :response-time ms'));
@@ -21,37 +25,54 @@ app.use((req, res, next) => {
2125
next();
2226
});
2327

28+
29+
+app.get('/updates', (req, res) => {
30+
const format = req.query.format ? req.query.format : '';
31+
if (format.toLowerCase() === 'json') {
32+
return getLiveUpdates(true).then(result => {
33+
return res.json(result);
34+
}).catch(error => errorHandler(error, res));
35+
}
36+
return getLiveUpdates(false).then(result => {
37+
return res.send(result);
38+
}).catch(error => errorHandler(error, res));
39+
});
40+
2441
app.get('/', (req, res) => {
2542
const isCurl = req.headers['user-agent'].match(/\bcurl\b/gmi) !== null;
2643
const format = req.query.format ? req.query.format : '';
44+
const minimal = req.query.minimal === 'true' ? true : false;
45+
const emojis = req.query.emojis === 'true' ? true : false;
2746

2847
if (format.toLowerCase() === 'json') {
2948
return getJSONData().then(result => {
3049
return res.json(result);
3150
}).catch(error => errorHandler(error, res));
3251
}
3352

34-
return getCompleteTable({ isCurl }).then(result => {
35-
return res.send(result);
36-
}).catch(error => errorHandler(error, res));
53+
return getCompleteTable({ isCurl, emojis, minimal })
54+
.then(result => {
55+
return res.send(result);
56+
}).catch(error => errorHandler(error, res));
3757
});
3858

3959
app.get('/:country', (req, res) => {
4060
const { country } = req.params;
4161
const isCurl = req.headers['user-agent'].match(/\bcurl\b/gmi) !== null;
4262
const format = req.query.format ? req.query.format : '';
43-
63+
const minimal = req.query.minimal === 'true' ? true : false;
64+
const emojis = req.query.emojis === 'true' ? true : false;
4465
if (!country || 'ALL' === country.toUpperCase()) {
4566
if (format.toLowerCase() === 'json') {
4667
return getJSONData().then(result => {
4768
return res.json(result);
4869
}).catch(error => errorHandler(error, res));
4970
}
5071

51-
52-
return getCompleteTable({ isCurl }).then(result => {
53-
return res.send(result);
54-
}).catch(error => errorHandler(error, res));
72+
return getCompleteTable({ isCurl, emojis, minimal })
73+
.then(result => {
74+
return res.send(result);
75+
}).catch(error => errorHandler(error, res));
5576
}
5677

5778
let lookupObj = lookupCountry(country);
@@ -63,7 +84,7 @@ app.get('/:country', (req, res) => {
6384
Ex:
6485
- /UK: for United Kingdom
6586
- /US: for United States of America.
66-
- /India: for India.
87+
- /Italy: for Italy.
6788
`);
6889
}
6990

@@ -75,9 +96,10 @@ app.get('/:country', (req, res) => {
7596
}).catch(error => errorHandler(error, res));
7697
}
7798

78-
return getCountryTable({ countryCode: iso2, isCurl }).then(result => {
79-
return res.send(result);
80-
}).catch(error => errorHandler(error, res));
99+
return getCountryTable({ countryCode: iso2, isCurl, emojis, minimal })
100+
.then(result => {
101+
return res.send(result);
102+
}).catch(error => errorHandler(error, res));
81103
});
82104

83105

bin/index.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const { getCountryTable } = require('../lib/byCountry');
77
const { lookupCountry } = require('../lib/helpers');
88

99
const { argv } = yargs
10-
.command('$0 [country]','Tool to track COVID-19 statistics for the world or the given country', yargs =>
10+
.command('$0 [country]','Tool to track COVID-19 statistics from terminal', yargs =>
1111
yargs.positional('country', {
1212
coerce(arg) {
1313
if ('ALL' === arg.toUpperCase()) {
@@ -41,16 +41,22 @@ const { argv } = yargs
4141
alias: 'color',
4242
describe: 'Show colors formatted output',
4343
type: 'boolean'
44+
},
45+
m: {
46+
alias: 'minimal',
47+
describe: 'remove borders and padding from table',
48+
type: 'boolean',
49+
default: false,
4450
}
4551
})
4652
.strict()
4753
.help('help');
4854

49-
const { emojis, country } = argv;
55+
const { emojis, country, minimal } = argv;
5056
(
5157
country === 'ALL'
52-
? getCompleteTable({emojis})
53-
: getCountryTable({ countryCode: country, emojis })
58+
? getCompleteTable({ emojis, minimal })
59+
: getCountryTable({ countryCode: country, emojis, minimal })
5460
)
5561
.then(console.log)
5662
.catch(console.error);

changelog.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Changelog
2+
3+
## Version 0.5.0
4+
5+
* Added minimal / comapct table command. ``corona --minimal``
6+
* Added world total stats at the bottom of the table too.
7+
* Refactor: moved table formatting functions to helpers.
8+
* Added total stats object when using `?format=json`
9+
10+
## Version 0.4.0
11+
12+
* Added country filter. Ex: ``corona Italy``
13+
* Added command to show emojis. Ex: ``corona --emojis``
14+
* Added command to disable colors using. Ex: ``corona --color=false``
15+
16+
## Version 0.2.0
17+
18+
* Added daily and weekly column.
19+
20+
## Version 0.1.0
21+
22+
* Lauched command `corona`

lib/byCountry.js

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@ exports.getJSONData = async () => {
6969
const data = await api.getCoronaData();
7070
const { latest, confirmed, deaths, recovered } = data;
7171
const countryData = getDataByState(confirmed, deaths, recovered);
72-
return countryData;
72+
const totalStats = getTotalStats(countryData);
73+
totalStats.country = 'World';
74+
return countryData.concat(totalStats);
7375
}
7476

7577
exports.getJSONDataForCountry = async (countryCode) => {
@@ -80,27 +82,16 @@ exports.getJSONDataForCountry = async (countryCode) => {
8082
return countryData;
8183
}
8284

83-
exports.getCountryTable = async ({countryCode, emojis = false, isCurl = true}) => {
84-
const head = [
85-
'',
86-
'State',
87-
'Confirmed',
88-
`Recovered${emojis ? ' 😀' : ''}`,
89-
`Deaths${emojis ? ' 😞' : ''}`,
90-
`Active${emojis ? ' 😷' : ''}`,
91-
'Mortality %',
92-
'Recovered %',
93-
'1 Day ▲',
94-
'1 Week ▲',
95-
// 'RoG',
96-
...( emojis ? ['🏳'] : [] ),
97-
];
85+
exports.getCountryTable = async ({
86+
countryCode,
87+
emojis = false,
88+
isCurl = true,
89+
minimal = false,
90+
}) => {
9891
const table = new Table({
99-
head,
100-
chars: { 'top': '═' , 'top-mid': '╤' , 'top-left': '╔' , 'top-right': '╗'
101-
, 'bottom': '═' , 'bottom-mid': '╧' , 'bottom-left': '╚' , 'bottom-right': '╝'
102-
, 'left': '║' , 'left-mid': '╟' , 'mid': '─' , 'mid-mid': '┼'
103-
, 'right': '║' , 'right-mid': '╢' , 'middle': '│' }
92+
head: helpers.getTableHeaders(emojis, 'State'),
93+
chars: helpers.getTableBorders(minimal),
94+
style: helpers.getTableStyles(minimal),
10495
});
10596
const data = await api.getCoronaData();
10697
const { latest, confirmed, deaths, recovered } = data;
@@ -167,9 +158,9 @@ exports.getCountryTable = async ({countryCode, emojis = false, isCurl = true}) =
167158
}
168159
</style>
169160
</head>
170-
<body>
171-
<pre>${table.toString() + footer(lastUpdated)}</pre>
172-
</body>
161+
<body>
162+
<pre>${table.toString() + footer(lastUpdated)}</pre>
163+
</body>
173164
</html>`;
174165
return stripAnsi(template);
175166
}

lib/corona.js

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -80,27 +80,15 @@ function extraStats(dataArr) {
8080
);
8181
}
8282

83-
exports.getCompleteTable = async ({isCurl = true, emojis = false}) => {
84-
const head = [
85-
'',
86-
'Country',
87-
`Confirmed ${emojis ? ' ✅': ''}`,
88-
`Recovered${emojis ? ' 😀' : ''}`,
89-
`Deaths${emojis ? ' 😞' : ''}`,
90-
`Active${emojis ? ' 😷' : ''}`,
91-
'Mortality %',
92-
'Recovered %',
93-
'1 Day ▲',
94-
'1 Week ▲',
95-
// 'RoG',
96-
...( emojis ? ['🏳'] : [] ),
97-
];
83+
exports.getCompleteTable = async ({
84+
isCurl = true,
85+
emojis = false,
86+
minimal = false,
87+
}) => {
9888
const table = new Table({
99-
head,
100-
chars: { 'top': '═' , 'top-mid': '╤' , 'top-left': '╔' , 'top-right': '╗'
101-
, 'bottom': '═' , 'bottom-mid': '╧' , 'bottom-left': '╚' , 'bottom-right': '╝'
102-
, 'left': '║' , 'left-mid': '╟' , 'mid': '─' , 'mid-mid': '┼'
103-
, 'right': '║' , 'right-mid': '╢' , 'middle': '│' }
89+
head: helpers.getTableHeaders(emojis, 'Country'),
90+
chars: helpers.getTableBorders(minimal),
91+
style: helpers.getTableStyles(minimal),
10492
});
10593
const data = await api.getCoronaData();
10694
const { latest, confirmed, deaths, recovered } = data;
@@ -139,6 +127,21 @@ exports.getCompleteTable = async ({isCurl = true, emojis = false}) => {
139127
]
140128
table.push({ [rank++]: values })
141129
});
130+
table.push({
131+
'': [
132+
'World',
133+
getConfirmed(worldStats.confirmed),
134+
getRecovered(worldStats.recovered),
135+
getDeaths(worldStats.deaths),
136+
getActive(worldStats.active),
137+
getMortalityPer(worldStats.mortalityPer),
138+
getRecoveredPer(worldStats.recoveredPer),
139+
getOneDayChange(worldStats),
140+
getOneWeekChange(worldStats),
141+
// '',
142+
...( emojis ? ['🌎'] : [] )
143+
]
144+
})
142145
const lastUpdated = countryData[0].lastUpdated;
143146
if (!isCurl) {
144147
const template = `<!DOCTYPE html>
@@ -159,9 +162,9 @@ exports.getCompleteTable = async ({isCurl = true, emojis = false}) => {
159162
}
160163
</style>
161164
</head>
162-
<body>
163-
<pre>${table.toString() + footer(lastUpdated)}</pre>
164-
</body>
165+
<body>
166+
<pre>${table.toString() + footer(lastUpdated)}</pre>
167+
</body>
165168
</html>`
166169
return stripAnsi(template);
167170
}

lib/helpers.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,3 +174,40 @@ Twitter: https://twitter.com/ekrysis
174174
Last Updated on: ${moment(lastUpdated).utc().format('DD-MMM-YYYY HH:MM')} UTC
175175
176176
`;
177+
178+
e.getTableBorders = minimal => {
179+
if (minimal) {
180+
return { 'top': '' , 'top-mid': '' , 'top-left': '' , 'top-right': ''
181+
, 'bottom': '' , 'bottom-mid': '' , 'bottom-left': '' , 'bottom-right': ''
182+
, 'left': '' , 'left-mid': '' , 'mid': '' , 'mid-mid': ''
183+
, 'right': '' , 'right-mid': '' , 'middle': ' ' }
184+
};
185+
return { 'top': '═' , 'top-mid': '╤' , 'top-left': '╔' , 'top-right': '╗'
186+
,'bottom': '═' , 'bottom-mid': '╧' , 'bottom-left': '╚' , 'bottom-right': '╝'
187+
,'left': '║' , 'left-mid': '╟' , 'mid': '─' , 'mid-mid': '┼'
188+
,'right': '║' , 'right-mid': '╢' , 'middle': '│' };
189+
};
190+
191+
e.getTableStyles = minimal => {
192+
if (minimal) {
193+
return { 'padding-left': 0, 'padding-right': 0 };
194+
}
195+
};
196+
197+
e.getTableHeaders = (emojis, secondColumnName ) => {
198+
const head = [
199+
'Rank',
200+
secondColumnName,
201+
`Confirmed ${emojis ? ' ✅': ''}`,
202+
`Recovered${emojis ? ' 😀' : ''}`,
203+
`Deaths${emojis ? ' 😞' : ''}`,
204+
`Active${emojis ? ' 😷' : ''}`,
205+
'Mortality %',
206+
'Recovered %',
207+
'1 Day ▲',
208+
'1 Week ▲',
209+
// 'RoG',
210+
...( emojis ? ['🏳'] : [] ),
211+
];
212+
return head;
213+
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "coronavirus-tracker-cli",
3-
"version": "0.4.0",
3+
"version": "0.5.0",
44
"description": "track conronavirus cases from cli",
55
"main": "./lib/corona.js",
66
"bin": {

0 commit comments

Comments
 (0)