Skip to content

Commit 4beedfe

Browse files
author
Waren Gonzaga
committed
Code refactor
1 parent 9ae8088 commit 4beedfe

File tree

2 files changed

+71
-70
lines changed

2 files changed

+71
-70
lines changed

app.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
// node modules
2-
const express = require('express'),
3-
app = express(),
4-
util = require('./bin/util'),
5-
fs = require('fs'),
6-
axios = require('axios'),
7-
covid19 = require('./lib/cli');
2+
const express = require('express'),
3+
app = express(),
4+
util = require('./bin/util'),
5+
fs = require('fs'),
6+
axios = require('axios'),
7+
covid19 = require('./lib/cli');
88

99
// set port
1010
const port = process.env.port || 7070;

lib/cli/index.js

Lines changed: 65 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,32 @@
11
/* eslint-disable no-await-in-loop */
22

3-
const style = require('ansi-styles'),
4-
fs = require('fs'),
5-
table3 = require('cli-table3');
3+
const style = require('ansi-styles'),
4+
fs = require('fs'),
5+
table3 = require('cli-table3');
6+
7+
// ansi style and colors
8+
9+
const
10+
// table color
11+
tblclr = (border) => {return cyan(border);},
12+
// normal ansi colors
13+
white = (txt) => {return style.white.open+txt+style.white.close;},
14+
black = (txt) => {return style.black.open+txt+style.black.close;},
15+
green = (txt) => {return style.green.open+txt+style.green.close;},
16+
cyan = (txt) => {return style.cyan.open+txt+style.cyan.close;},
17+
magenta = (txt) => {return style.magenta.open+txt+style.magenta.close;},
18+
yellow = (txt) => {return style.yellow.open+txt+style.yellow.close;},
19+
red = (txt) => {return style.red.open+txt+style.red.close;},
20+
// bright ansi colors
21+
cyanBright = (txt) => {return style.cyanBright.open+txt+style.cyanBright.close;},
22+
magentaBright = (txt) => {return style.magentaBright.open+txt+style.magentaBright.close;},
23+
redBright = (txt) => {return style.redBright.open+txt+style.redBright.close;},
24+
// background ansi color
25+
cyanBG = (txt) => {return style.bgCyan.open+txt+style.bgCyan.close;};
626

7-
const axios = require('axios');
8-
9-
const tblopn = style.cyan.open,
10-
tblcls = style.cyan.close;
11-
12-
const apiBaseURL = "https://corona.lmao.ninja";
1327

1428
// package.json information
15-
const pkg = JSON.parse(fs.readFileSync('package.json'));
29+
const pkg = JSON.parse(fs.readFileSync('package.json'));
1630

1731
// format data
1832
const formatNumber = (num) => {
@@ -27,89 +41,76 @@ let ts = Date.now(),
2741
year = date_ob.getFullYear(),
2842
currentdate = month + "/" + date + "/" + year;
2943

44+
// table configuration
45+
const borders = { 'top': tblclr('═'),
46+
'top-mid': tblclr('╤'),
47+
'top-left': tblclr('╔'),
48+
'top-right': tblclr('╗'),
49+
'bottom': tblclr('═'),
50+
'bottom-mid': tblclr('╧'),
51+
'bottom-left': tblclr('╚'),
52+
'bottom-right': tblclr('╝'),
53+
'left': tblclr('║'),
54+
'left-mid': tblclr('╟'),
55+
'mid': tblclr('─'),
56+
'mid-mid': tblclr('┼'),
57+
'right': tblclr('║'),
58+
'right-mid': tblclr('╢'),
59+
'middle': tblclr('│')};
60+
61+
// additional informaton
62+
const source = 'Source: https://www.worldometers.info/coronavirus/',
63+
repo = 'Code: https://github.com/warengonzaga/covid19-tracker-cli',
64+
sourceGlobal = {colSpan:3,content:source},
65+
repoGlobal = {colSpan:3,content:repo},
66+
sourceCountry = {colSpan:4,content:source},
67+
repoCountry = {colSpan:4,content:repo};
68+
3069
// covid19 global tracker
3170
exports.covid19globaltracker = (c, d, r, u) => {
3271
const cases = c, deaths = d, recovered = r, asof = new Date(u),
3372
table = new table3({
34-
head: [{colSpan:3,content:`${style.white.open}COVID-19 Tracker CLI v`+pkg.version+` - Global Update${style.white.close}`}],
35-
chars: { 'top': `${tblopn}${tblcls}`,
36-
'top-mid': `${tblopn}${tblcls}`,
37-
'top-left': `${tblopn}${tblcls}`,
38-
'top-right': `${tblopn}${tblcls}`,
39-
'bottom': `${tblopn}${tblcls}`,
40-
'bottom-mid': `${tblopn}${tblcls}`,
41-
'bottom-left': `${tblopn}${tblcls}`,
42-
'bottom-right': `${tblopn}${tblcls}`,
43-
'left': `${tblopn}${tblcls}` ,
44-
'left-mid': `${tblopn}${tblcls}` ,
45-
'mid': `${tblopn}${tblcls}` ,
46-
'mid-mid': `${tblopn}${tblcls}`,
47-
'right': `${tblopn}${tblcls}` ,
48-
'right-mid': `${tblopn}${tblcls}` ,
49-
'middle': `${tblopn}${tblcls}` }
73+
head: [{colSpan:3,content:white('COVID-19 Tracker CLI v'+pkg.version+' - Global Update')}],
74+
chars: borders
5075
});
51-
5276
table.push(
53-
[{colSpan:3,content:style.gray.open+'As of '+asof.toLocaleString()+' [Date:'+currentdate+']'+style.gray.close}],
54-
[`${style.magenta.open}Cases${style.magenta.close}`, `${style.red.open}Deaths${style.red.close}`, `${style.green.open}Recovered${style.green.close}`],
77+
[{colSpan:3,content:yellow('As of '+asof.toLocaleString()+' [Date:'+currentdate+']')}],
78+
[magenta('Cases'), red('Deaths'), green('Recovered')],
5579
[formatNumber(cases), formatNumber(deaths), formatNumber(recovered)],
56-
[{colSpan:3,content:style.gray.open+`Source: https://www.worldometers.info/coronavirus/`+style.gray.close}],
57-
[{colSpan:3,content:style.gray.open+`Code: https://github.com/warengonzaga/covid19-tracker-cli`+style.gray.close}]
80+
[sourceGlobal],[repoGlobal]
5881
);
5982
return table.toString()+'\n'+footer;
6083
};
6184

62-
const lastupdate = async () => {
63-
return await axios.get(`${apiBaseURL}/all`).then(function(res){
64-
const update = res.data.updated;
65-
return update.toLocaleString();
66-
});
67-
};
68-
85+
// covid19 country tracker
6986
exports.covid19countrytracker = (n, c, tC, d, tD, r, a, cl, cPOM, u) => {
7087
const name = n, cases = c, todayCases = tC,
7188
deaths = d, todayDeaths = tD, recovered = r,
7289
active = a, critical = cl, casesPerOneMillion = cPOM,
7390
asof = new Date(u);
7491
table = new table3({
75-
head: [{colSpan:4,content:`${style.white.open}COVID-19 Tracker CLI v`+pkg.version+` - ${name} Update${style.white.close}`}],
76-
chars: { 'top': `${tblopn}${tblcls}`,
77-
'top-mid': `${tblopn}${tblcls}`,
78-
'top-left': `${tblopn}${tblcls}`,
79-
'top-right': `${tblopn}${tblcls}`,
80-
'bottom': `${tblopn}${tblcls}`,
81-
'bottom-mid': `${tblopn}${tblcls}`,
82-
'bottom-left': `${tblopn}${tblcls}`,
83-
'bottom-right': `${tblopn}${tblcls}`,
84-
'left': `${tblopn}${tblcls}` ,
85-
'left-mid': `${tblopn}${tblcls}` ,
86-
'mid': `${tblopn}${tblcls}` ,
87-
'mid-mid': `${tblopn}${tblcls}`,
88-
'right': `${tblopn}${tblcls}` ,
89-
'right-mid': `${tblopn}${tblcls}` ,
90-
'middle': `${tblopn}${tblcls}` }
92+
head: [{colSpan:4,content:white('COVID-19 Tracker CLI v'+pkg.version+' - '+name+' Update')}],
93+
chars: borders
9194
});
92-
9395
table.push(
94-
[{colSpan:4,content:style.gray.open+'As of '+asof.toLocaleString()+' [Date:'+currentdate+']'+style.gray.close}],
95-
[`${style.magenta.open}Cases${style.magenta.close}`, `${style.red.open}Deaths${style.red.close}`, `${style.green.open}Recovered${style.green.close}`,`${style.cyan.open}Active${style.cyan.close}`],
96+
[{colSpan:4,content:yellow('As of '+asof.toLocaleString()+' [Date:'+currentdate+']')}],
97+
[magenta('Cases'), red('Deaths'), green('Recovered'), cyan('Active')],
9698
[formatNumber(cases), formatNumber(deaths), formatNumber(recovered), formatNumber(active)],
97-
[`${style.magentaBright.open}Today Cases${style.magentaBright.close}`, `${style.redBright.open}Today Deaths${style.red.close}`, `${style.redBright.open}Critical${style.redBright.close}`,`${style.cyanBright.open}Cases Per Million${style.cyanBright.close}`],
99+
[magentaBright('Today Cases'), redBright('Today Deaths'), redBright('Critical'), cyanBright('Cases Per Million')],
98100
[formatNumber(todayCases), formatNumber(todayDeaths), formatNumber(critical), formatNumber(casesPerOneMillion)],
99-
[{colSpan:4,content:style.gray.open+`Source: https://www.worldometers.info/coronavirus/`+style.gray.close}],
100-
[{colSpan:4,content:style.gray.open+`Code: https://github.com/warengonzaga/covid19-tracker-cli`+style.gray.close}]
101+
[sourceCountry],[repoCountry]
101102
);
102-
103103
return table.toString()+'\n'+footer;
104104
};
105105

106+
// cli footer
106107
const footer = `
107-
Always wash your hands, stay safe...
108+
Always wash your hands, stay safe...
108109
109110
---
110111
Love this project? Please consider buying me a cup of coffee!
111-
${style.yellow.open}warengonzaga.com/buymeacoffee${style.yellow.close}
112+
${yellow('warengonzaga.com/buymeacoffee')}
112113
113114
---
114-
Follow ${style.bgCyan.open}${style.black.open}@warengonzaga${style.black.close}${style.bgCyan.close} for more updates!
115+
Follow ${cyanBG(black('@warengonzaga'))} for more updates!
115116
\n`;

0 commit comments

Comments
 (0)