Skip to content

Commit 1f67fcc

Browse files
committed
Fix ESLint issues, make code consistent, remove unused code and dependencies.
1 parent 3f2d607 commit 1f67fcc

File tree

9 files changed

+119
-220
lines changed

9 files changed

+119
-220
lines changed

app.js

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
const express = require('express');
2-
const app = express();
3-
const lookup = require('country-code-lookup');
42
const morgan = require('morgan');
5-
const stripAnsi = require('strip-ansi');
6-
7-
const port = process.env.PORT || 3001;
83

94
const { getCountryTable, getJSONData, getJSONDataForCountry } = require('./lib/byCountry');
105
const { getCompleteTable } = require('./lib/corona');
116
const { lookupCountry } = require('./lib/helpers');
127
const { getLiveUpdates } = require('./lib/reddit.js');
138

9+
const app = express();
10+
const port = process.env.PORT || 3001;
1411

1512
function errorHandler(error, res) {
1613
console.error(error);
@@ -26,13 +23,11 @@ app.use((req, res, next) => {
2623
next();
2724
});
2825

29-
30-
3126
app.get('/', (req, res) => {
32-
const isCurl = req.headers['user-agent'].match(/\bcurl\b/gmi) !== null;
27+
const isCurl = req.headers['user-agent'].match(/\bcurl\b/gim) !== null;
3328
const format = req.query.format ? req.query.format : '';
34-
const minimal = req.query.minimal === 'true' ? true : false;
35-
const emojis = req.query.emojis === 'true' ? true : false;
29+
const minimal = req.query.minimal === 'true';
30+
const emojis = req.query.emojis === 'true';
3631
const top = req.query.top ? Number(req.query.top) : 1000;
3732

3833
if (format.toLowerCase() === 'json') {
@@ -49,24 +44,25 @@ app.get('/', (req, res) => {
4944

5045
app.get('/updates', (req, res) => {
5146
const format = req.query.format ? req.query.format : '';
52-
const isCurl = req.headers['user-agent'].match(/\bcurl\b/gmi) !== null;
47+
const isCurl = req.headers['user-agent'].match(/\bcurl\b/gim) !== null;
5348
if (format.toLowerCase() === 'json') {
5449
return getLiveUpdates({ json: true, isCurl }).then(result => {
5550
return res.json(result);
5651
}).catch(error => errorHandler(error, res));
5752
}
53+
5854
return getLiveUpdates({ json: false, isCurl }).then(result => {
5955
return res.send(result);
6056
}).catch(error => errorHandler(error, res));
6157
});
6258

6359
app.get('/:country', (req, res) => {
6460
const { country } = req.params;
65-
const isCurl = req.headers['user-agent'].match(/\bcurl\b/gmi) !== null;
61+
const isCurl = req.headers['user-agent'].match(/\bcurl\b/gim) !== null;
6662
const format = req.query.format ? req.query.format : '';
67-
const minimal = req.query.minimal === 'true' ? true : false;
68-
const emojis = req.query.emojis === 'true' ? true : false;
69-
if (!country || 'ALL' === country.toUpperCase()) {
63+
const minimal = req.query.minimal === 'true';
64+
const emojis = req.query.emojis === 'true';
65+
if (!country || country.toUpperCase() === 'ALL') {
7066
if (format.toLowerCase() === 'json') {
7167
return getJSONData().then(result => {
7268
return res.json(result);
@@ -79,13 +75,13 @@ app.get('/:country', (req, res) => {
7975
}).catch(error => errorHandler(error, res));
8076
}
8177

82-
let lookupObj = lookupCountry(country);
78+
const lookupObj = lookupCountry(country);
8379

8480
if (!lookupObj) {
8581
return res.send(`
8682
Country not found.
87-
Try full country name or country code.
88-
Ex:
83+
Try the full country name or country code.
84+
Example:
8985
- /UK: for United Kingdom
9086
- /US: for United States of America.
9187
- /Italy: for Italy.
@@ -106,5 +102,4 @@ app.get('/:country', (req, res) => {
106102
}).catch(error => errorHandler(error, res));
107103
});
108104

109-
110-
app.listen(port, () => console.log(`Running on ${port}`));
105+
app.listen(port, () => console.log(`Running on ${port}`));

bin/index.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
#!/usr/bin/env node
2-
const yargonaut = require('yargonaut').style('green');
2+
33
const yargs = require('yargs');
44
const chalk = require('chalk');
55
const { getCompleteTable } = require('../lib/corona');
66
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 from terminal', yargs =>
10+
.command('$0 [country]', 'Tool to track COVID-19 statistics from terminal', yargs =>
1111
yargs.positional('country', {
1212
coerce(arg) {
13-
if ('ALL' === arg.toUpperCase()) {
13+
if (arg.toUpperCase() === 'ALL') {
1414
return 'ALL';
1515
}
16+
1617
const country = lookupCountry(arg);
1718
if (!country) {
1819
let error = `Country '${arg}' not found.\n`;
@@ -23,6 +24,7 @@ const { argv } = yargs
2324
error += '- Italy: for Italy.\n';
2425
throw new Error(chalk.red.bold(error));
2526
}
27+
2628
return country.iso2;
2729
},
2830
describe: 'Filter table by country',
@@ -57,12 +59,11 @@ const { argv } = yargs
5759
.strict()
5860
.help('help');
5961

60-
6162
const { emojis, country, minimal, top } = argv;
6263
(
6364
country === 'ALL'
6465
? getCompleteTable({ emojis, minimal, top })
6566
: getCountryTable({ countryCode: country, emojis, minimal })
6667
)
6768
.then(console.log)
68-
.catch(console.error);
69+
.catch(console.error);

lib/api.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
const NodeCache = require('node-cache');
22
const axios = require('axios');
3-
const myCache = new NodeCache( { stdTTL: 100, checkperiod: 600 } );
3+
4+
const myCache = new NodeCache({ stdTTL: 100, checkperiod: 600 });
45
const CORONA_ALL_KEY = 'coronaAll';
56

67
exports.getCoronaData = async () => {
78
const coronaCache = myCache.get(CORONA_ALL_KEY);
9+
810
if (coronaCache) {
911
return coronaCache;
1012
}
13+
1114
const result = await axios('https://coronavirus-tracker-api.herokuapp.com/all');
15+
1216
if (!result || !result.data) {
13-
throw new Error(`Source API faliure.`);
17+
throw new Error('Source API failure.');
1418
}
19+
1520
myCache.set(CORONA_ALL_KEY, result.data, 60 * 15);
21+
1622
return result.data;
17-
}
23+
};

lib/byCountry.js

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
const Table = require('cli-table3');
2-
const axios = require('axios');
32
const _ = require('lodash');
4-
const chalk = require('chalk');
53
const helpers = require('./helpers');
64
const api = require('./api');
75
const stripAnsi = require('strip-ansi');
86
const {
9-
getCountry,
107
getConfirmed,
118
getActive,
129
getDeaths,
@@ -19,7 +16,6 @@ const {
1916
calRecoveredPer,
2017
getOneDayChange,
2118
getOneWeekChange,
22-
getRateOfGrowth,
2319
getTotalStats,
2420
footer,
2521
} = require('./helpers');
@@ -52,35 +48,35 @@ function getDataByState(confirmed, deaths, recovered) {
5248
const countryArr = extraStats(
5349
Object.keys(countryMap).map(key => countryMap[key])
5450
);
55-
return _.sortBy(countryArr, (o) => -o.confirmed)
51+
return _.sortBy(countryArr, (o) => -o.confirmed);
5652
}
5753

5854
function extraStats(dataArr) {
59-
return dataArr.map(obj => Object.assign({}, obj,
60-
{
61-
active: calActive(obj),
62-
mortalityPer: calMortalityPer(obj),
63-
recoveredPer: calRecoveredPer(obj),
64-
})
55+
return dataArr.map(obj => ({
56+
...obj,
57+
active: calActive(obj),
58+
mortalityPer: calMortalityPer(obj),
59+
recoveredPer: calRecoveredPer(obj),
60+
})
6561
);
6662
}
6763

6864
exports.getJSONData = async () => {
6965
const data = await api.getCoronaData();
70-
const { latest, confirmed, deaths, recovered } = data;
66+
const { confirmed, deaths, recovered } = data;
7167
const countryData = getDataByState(confirmed, deaths, recovered);
7268
const totalStats = getTotalStats(countryData);
7369
totalStats.country = 'World';
7470
return countryData.concat(totalStats);
75-
}
71+
};
7672

7773
exports.getJSONDataForCountry = async (countryCode) => {
7874
const data = await api.getCoronaData();
79-
const { latest, confirmed, deaths, recovered } = data;
80-
const countryData = getDataByState(confirmed, deaths, recovered, countryCode)
75+
const { confirmed, deaths, recovered } = data;
76+
const countryData = getDataByState(confirmed, deaths, recovered)
8177
.filter(obj => obj.countryCode === countryCode);
8278
return countryData;
83-
}
79+
};
8480

8581
exports.getCountryTable = async ({
8682
countryCode,
@@ -94,11 +90,11 @@ exports.getCountryTable = async ({
9490
style: helpers.getTableStyles(minimal),
9591
});
9692
const data = await api.getCoronaData();
97-
const { latest, confirmed, deaths, recovered } = data;
93+
const { confirmed, deaths, recovered } = data;
9894
const countryData = getDataByState(confirmed, deaths, recovered)
9995
.filter(obj => obj.countryCode === countryCode);
10096

101-
if(countryData.length === 0) {
97+
if (countryData.length === 0) {
10298
throw new Error(`Country code ${countryCode} does not match anything in the database.`);
10399
}
104100

@@ -114,10 +110,9 @@ exports.getCountryTable = async ({
114110
getRecoveredPer(totalStats.recoveredPer),
115111
getOneDayChange(totalStats),
116112
getOneWeekChange(totalStats),
117-
// '',
118-
// getEmoji(countryData[0].countryCode),
119113
]
120-
})
114+
});
115+
121116
if (countryData.length > 1) {
122117
let rank = 1;
123118
countryData.forEach(cd => {
@@ -132,13 +127,13 @@ exports.getCountryTable = async ({
132127
getRecoveredPer(cd.recoveredPer),
133128
getOneDayChange(cd),
134129
getOneWeekChange(cd),
135-
// getRateOfGrowth(cd),
136-
... (emojis ? [countryEmoji] : [])
137-
]
138-
table.push({ [rank++]: values })
130+
...(emojis ? [countryEmoji] : [])
131+
];
132+
table.push({ [rank++]: values });
139133
});
140134
}
141-
const lastUpdated = countryData[0].lastUpdated;
135+
136+
const { lastUpdated } = countryData[0];
142137
if (!isCurl) {
143138
const template = `<!DOCTYPE html>
144139
<html lang="en">
@@ -164,5 +159,6 @@ exports.getCountryTable = async ({
164159
</html>`;
165160
return stripAnsi(template);
166161
}
162+
167163
return table.toString() + footer(lastUpdated);
168-
}
164+
};

0 commit comments

Comments
 (0)