Skip to content

Commit f843313

Browse files
authored
Merge branch 'master' into master
2 parents bcd2fd4 + 5bcd9ec commit f843313

File tree

19 files changed

+1906
-397
lines changed

19 files changed

+1906
-397
lines changed

.editorconfig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# editorconfig.org
2+
3+
root = true
4+
5+
[*]
6+
charset = utf-8
7+
end_of_line = lf
8+
indent_size = 2
9+
indent_style = space
10+
insert_final_newline = true
11+
trim_trailing_whitespace = true

.eslintrc.json

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
{
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-
}
2+
"env": {
3+
"node": true,
4+
"es6": true
5+
},
6+
"extends": "eslint:recommended",
7+
"parserOptions": {
8+
"ecmaVersion": 2019,
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+
}
2929
}

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Enforce Unix newlines
2+
* text=auto eol=lf

.github/workflows/test.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: Tests
2+
on: [push, pull_request]
3+
env:
4+
CI: true
5+
6+
jobs:
7+
run:
8+
name: Node ${{ matrix.node }}
9+
runs-on: ubuntu-latest
10+
11+
strategy:
12+
fail-fast: false
13+
14+
steps:
15+
- name: Clone repository
16+
uses: actions/checkout@v2
17+
18+
- name: Set Node.js version
19+
uses: actions/setup-node@v1
20+
with:
21+
node-version: "12.x"
22+
23+
- run: node --version
24+
- run: npm --version
25+
26+
- name: Install npm dependencies
27+
run: npm ci
28+
29+
- name: Run tests
30+
run: npm test

app.js

Lines changed: 71 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,128 @@
11
const express = require('express');
2-
const app = express();
3-
const lookup = require('country-code-lookup');
42
const morgan = require('morgan');
53

6-
const port = process.env.PORT || 3001;
7-
84
const { getCountryTable, getJSONData, getJSONDataForCountry } = require('./lib/byCountry');
95
const { getCompleteTable } = require('./lib/corona');
10-
const { countryUpperCase, lookupCountry } = require('./lib/helpers');
6+
const { lookupCountry } = require('./lib/helpers');
7+
const { getLiveUpdates } = require('./lib/reddit.js');
8+
const { getWorldoMetersTable } = require('./lib/worldoMeters.js');
119

10+
const app = express();
11+
const port = process.env.PORT || 3001;
12+
const IS_CURL_RE = /\bcurl\b/im;
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

22+
app.set('json escape', true);
23+
1824
app.use(morgan(':remote-addr :remote-user :method :url :status :res[content-length] - :response-time ms'));
25+
app.use((req, res, next) => {
26+
res.setHeader('Cache-Control', 'no-cache');
27+
next();
28+
});
1929

2030
app.get('/', (req, res) => {
31+
const isCurl = IS_CURL_RE.test(req.headers['user-agent']);
2132
const format = req.query.format ? req.query.format : '';
33+
const minimal = req.query.minimal === 'true';
34+
const emojis = req.query.emojis === 'true';
35+
const top = req.query.top ? Number(req.query.top) : 1000;
36+
const source = req.query.source ? Number(req.query.source) : 1;
37+
38+
if (source === 2) {
39+
return getWorldoMetersTable({ isCurl, emojis, minimal, top, format})
40+
.then(result => {
41+
return res.send(result);
42+
}).catch(error => errorHandler(error, res));
43+
}
2244

2345
if (format.toLowerCase() === 'json') {
2446
return getJSONData().then(result => {
25-
res.setHeader('Cache-Control', 's-maxage=900');
2647
return res.json(result);
2748
}).catch(error => errorHandler(error, res));
2849
}
2950

30-
return getCompleteTable().then(result => {
31-
res.setHeader('Cache-Control', 's-maxage=900');
51+
return getCompleteTable({ isCurl, emojis, minimal, top })
52+
.then(result => {
53+
return res.send(result);
54+
}).catch(error => errorHandler(error, res));
55+
});
56+
57+
app.get('/updates', (req, res) => {
58+
const isCurl = IS_CURL_RE.test(req.headers['user-agent']);
59+
const format = req.query.format ? req.query.format : '';
60+
61+
if (format.toLowerCase() === 'json') {
62+
return getLiveUpdates({ json: true, isCurl }).then(result => {
63+
return res.json(result);
64+
}).catch(error => errorHandler(error, res));
65+
}
66+
67+
return getLiveUpdates({ json: false, isCurl }).then(result => {
3268
return res.send(result);
3369
}).catch(error => errorHandler(error, res));
3470
});
3571

3672
app.get('/:country', (req, res) => {
3773
const { country } = req.params;
74+
const isCurl = IS_CURL_RE.test(req.headers['user-agent']);
3875
const format = req.query.format ? req.query.format : '';
76+
const minimal = req.query.minimal === 'true';
77+
const emojis = req.query.emojis === 'true';
78+
const source = req.query.source ? Number(req.query.source) : 1;
3979

40-
if (!country || 'ALL' === country.toUpperCase()) {
80+
if (!country || country.toUpperCase() === 'ALL') {
4181
if (format.toLowerCase() === 'json') {
4282
return getJSONData().then(result => {
43-
res.setHeader('Cache-Control', 's-maxage=900');
4483
return res.json(result);
4584
}).catch(error => errorHandler(error, res));
4685
}
4786

48-
49-
return getCompleteTable().then(result => {
50-
res.setHeader('Cache-Control', 's-maxage=900');
51-
return res.send(result);
52-
}).catch(error => errorHandler(error, res));
87+
return getCompleteTable({ isCurl, emojis, minimal })
88+
.then(result => {
89+
return res.send(result);
90+
}).catch(error => errorHandler(error, res));
5391
}
5492

55-
let lookupObj = lookupCountry(country);
93+
const lookupObj = lookupCountry(country);
5694

5795
if (!lookupObj) {
5896
return res.send(`
5997
Country not found.
60-
Try full country name or country code.
61-
Ex:
98+
Try the full country name or country code.
99+
Example:
62100
- /UK: for United Kingdom
63101
- /US: for United States of America.
64-
- /India: for India.
102+
- /Italy: for Italy.
65103
`);
66104
}
67105

106+
68107
const { iso2 } = lookupObj;
69108

109+
if (source === 2) {
110+
return getWorldoMetersTable({ countryCode: iso2, isCurl, emojis, minimal, format })
111+
.then(result => {
112+
return res.send(result);
113+
}).catch(error => errorHandler(error, res));
114+
}
115+
70116
if (format.toLowerCase() === 'json') {
71117
return getJSONDataForCountry(iso2).then(result => {
72-
res.setHeader('Cache-Control', 's-maxage=900');
73118
return res.json(result);
74119
}).catch(error => errorHandler(error, res));
75120
}
76121

77-
return getCountryTable(iso2).then(result => {
78-
res.setHeader('Cache-Control', 's-maxage=900');
79-
return res.send(result);
80-
}).catch(error => errorHandler(error, res));
122+
return getCountryTable({ countryCode: iso2, isCurl, emojis, minimal })
123+
.then(result => {
124+
return res.send(result);
125+
}).catch(error => errorHandler(error, res));
81126
});
82127

83-
84-
app.listen(port, () => console.log(`Running on ${port}`));
128+
app.listen(port, () => console.log(`Running on ${port}`));

bin/index.js

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
#!/usr/bin/env node
2-
const yargonaut = require('yargonaut')
3-
.style('green');
2+
3+
require('yargonaut').style('green');
44
const yargs = require('yargs');
55
const chalk = require('chalk');
66
const { getCompleteTable } = require('../lib/corona');
77
const { getCountryTable } = require('../lib/byCountry');
8+
const { getWorldoMetersTable } = require('../lib/worldoMeters');
89
const { lookupCountry } = require('../lib/helpers');
910

1011
const { argv } = yargs
11-
.command('$0 [country]','Tool to COVID-19 statistics for the world or the given country', yargs =>
12+
.command('$0 [country]', 'Tool to track COVID-19 statistics from terminal', yargs =>
1213
yargs.positional('country', {
1314
coerce(arg) {
14-
if ('ALL' === arg.toUpperCase()) {
15+
if (arg.toUpperCase() === 'ALL') {
1516
return 'ALL';
1617
}
18+
1719
const country = lookupCountry(arg);
1820
if (!country) {
1921
let error = `Country '${arg}' not found.\n`;
@@ -24,6 +26,7 @@ const { argv } = yargs
2426
error += '- Italy: for Italy.\n';
2527
throw new Error(chalk.red.bold(error));
2628
}
29+
2730
return country.iso2;
2831
},
2932
describe: 'Filter table by country',
@@ -32,6 +35,12 @@ const { argv } = yargs
3235
})
3336
)
3437
.options({
38+
s: {
39+
alias: 'source',
40+
describe: 'fetch data from other source',
41+
default: 1,
42+
type: 'int'
43+
},
3544
e: {
3645
alias: 'emojis',
3746
describe: 'Show emojis in table',
@@ -42,16 +51,33 @@ const { argv } = yargs
4251
alias: 'color',
4352
describe: 'Show colors formatted output',
4453
type: 'boolean'
54+
},
55+
m: {
56+
alias: 'minimal',
57+
describe: 'Remove borders & padding from table',
58+
type: 'boolean',
59+
default: false,
60+
},
61+
t: {
62+
alias: 'top',
63+
describe: 'Filter table by rank',
64+
type: 'int'
4565
}
4666
})
4767
.strict()
4868
.help('help');
4969

50-
const { emojis, country } = argv;
51-
(
52-
country === 'ALL'
53-
? getCompleteTable(emojis)
54-
: getCountryTable(country, emojis)
55-
)
56-
.then(console.log)
57-
.catch(console.error);
70+
argv.countryCode = argv.country;
71+
if (argv.source === 2) {
72+
getWorldoMetersTable(argv)
73+
.then(console.log)
74+
.catch(console.error);
75+
} else {
76+
(
77+
argv.country === 'ALL'
78+
? getCompleteTable(argv)
79+
: getCountryTable(argv)
80+
)
81+
.then(console.log)
82+
.catch(console.error);
83+
}

changelog.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Changelog
2+
3+
## Version 0.7.0
4+
5+
* Added new source to fetch realtime data. ``corona --source=2``
6+
* Code refactored and some bug fixes.
7+
8+
## Version 0.6.0
9+
10+
* Added filter to show top N countries. ``corona --top=20``
11+
12+
## Version 0.5.0
13+
14+
* Added minimal / comapct table command. ``corona --minimal``
15+
* Added world total stats at the bottom of the table too.
16+
* Refactor: moved table formatting functions to helpers.
17+
* Added total stats object when using `?format=json`
18+
19+
## Version 0.4.0
20+
21+
* Added country filter. Ex: ``corona Italy``
22+
* Added command to show emojis. Ex: ``corona --emojis``
23+
* Added command to disable colors using. Ex: ``corona --color=false``
24+
25+
## Version 0.2.0
26+
27+
* Added daily and weekly column.
28+
29+
## Version 0.1.0
30+
31+
* Lauched command `corona`

0 commit comments

Comments
 (0)