Skip to content

Commit 44a124a

Browse files
committed
Deduplicate code.
1 parent 4d99201 commit 44a124a

File tree

3 files changed

+48
-78
lines changed

3 files changed

+48
-78
lines changed

lib/byCountry.js

Lines changed: 4 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,20 @@ const Table = require('cli-table3');
22
const _ = require('lodash');
33
const helpers = require('./helpers');
44
const api = require('./api');
5-
const stripAnsi = require('strip-ansi');
65
const {
6+
extraStats,
77
getConfirmed,
88
getActive,
99
getDeaths,
1010
getRecovered,
1111
getMortalityPer,
1212
getRecoveredPer,
1313
getEmoji,
14-
calActive,
15-
calMortalityPer,
16-
calRecoveredPer,
1714
getOneDayChange,
1815
getOneWeekChange,
1916
getTotalStats,
2017
footer,
18+
htmlTemplate,
2119
} = require('./helpers');
2220

2321
function getDataByState(confirmed, deaths, recovered) {
@@ -51,16 +49,6 @@ function getDataByState(confirmed, deaths, recovered) {
5149
return _.sortBy(countryArr, (o) => -o.confirmed);
5250
}
5351

54-
function extraStats(dataArr) {
55-
return dataArr.map(obj => ({
56-
...obj,
57-
active: calActive(obj),
58-
mortalityPer: calMortalityPer(obj),
59-
recoveredPer: calRecoveredPer(obj),
60-
})
61-
);
62-
}
63-
6452
exports.getJSONData = async () => {
6553
const data = await api.getCoronaData();
6654
const { confirmed, deaths, recovered } = data;
@@ -134,30 +122,7 @@ exports.getCountryTable = async ({
134122
}
135123

136124
const { lastUpdated } = countryData[0];
137-
if (!isCurl) {
138-
const template = `<!doctype html>
139-
<html lang="en">
140-
<head>
141-
<meta charset="utf-8">
142-
<meta name="viewport" content="width=device-width, initial-scale=1">
143-
<title>Coronavirus Tracker</title>
144-
<style>
145-
body {
146-
background-color: #0d0208;
147-
color: #00ff41;
148-
}
149-
pre {
150-
font-family: SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
151-
white-space: pre;
152-
}
153-
</style>
154-
</head>
155-
<body>
156-
<pre>${table.toString() + footer(lastUpdated)}</pre>
157-
</body>
158-
</html>`;
159-
return stripAnsi(template);
160-
}
125+
const ret = table.toString() + footer(lastUpdated);
161126

162-
return table.toString() + footer(lastUpdated);
127+
return isCurl ? ret : htmlTemplate(ret);
163128
};

lib/corona.js

Lines changed: 5 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ const Table = require('cli-table3');
22
const _ = require('lodash');
33
const helpers = require('./helpers');
44
const api = require('./api');
5-
const stripAnsi = require('strip-ansi');
5+
66
const {
7+
extraStats,
78
getCountry,
89
getConfirmed,
910
getActive,
@@ -12,13 +13,11 @@ const {
1213
getMortalityPer,
1314
getRecoveredPer,
1415
getEmoji,
15-
calActive,
16-
calMortalityPer,
17-
calRecoveredPer,
1816
getOneDayChange,
1917
getOneWeekChange,
2018
getTotalStats,
2119
footer,
20+
htmlTemplate,
2221
} = require('./helpers');
2322

2423
function getDataByCountry(confirmed, deaths, recovered) {
@@ -67,16 +66,6 @@ function getDataByCountry(confirmed, deaths, recovered) {
6766
return _.sortBy(countryArr, (o) => -o.confirmed);
6867
}
6968

70-
function extraStats(dataArr) {
71-
return dataArr.map(obj => ({
72-
...obj,
73-
active: calActive(obj),
74-
mortalityPer: calMortalityPer(obj),
75-
recoveredPer: calRecoveredPer(obj),
76-
})
77-
);
78-
}
79-
8069
exports.getCompleteTable = async ({
8170
isCurl = true,
8271
emojis = false,
@@ -139,30 +128,7 @@ exports.getCompleteTable = async ({
139128
]
140129
});
141130
const { lastUpdated } = countryData[0];
142-
if (!isCurl) {
143-
const template = `<!doctype html>
144-
<html lang="en">
145-
<head>
146-
<meta charset="utf-8">
147-
<meta name="viewport" content="width=device-width, initial-scale=1">
148-
<title>Coronavirus Tracker</title>
149-
<style>
150-
body {
151-
background-color: #0d0208;
152-
color: #00ff41;
153-
}
154-
pre {
155-
font-family: SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
156-
white-space: pre;
157-
}
158-
</style>
159-
</head>
160-
<body>
161-
<pre>${table.toString() + footer(lastUpdated)}</pre>
162-
</body>
163-
</html>`;
164-
return stripAnsi(template);
165-
}
131+
const ret = table.toString() + footer(lastUpdated);
166132

167-
return table.toString() + footer(lastUpdated);
133+
return isCurl ? ret : htmlTemplate(ret);
168134
};

lib/helpers.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const emojiFlags = require('emoji-flags');
44
const _ = require('lodash');
55
const moment = require('moment');
66
const lookup = require('country-code-lookup');
7+
const stripAnsi = require('strip-ansi');
78
const e = exports;
89

910
e.getCountry = (country) => {
@@ -222,3 +223,41 @@ e.getTableHeaders = (emojis, secondColumnName) => {
222223
];
223224
return head;
224225
};
226+
227+
e.extraStats = (dataArr) => {
228+
return dataArr.map(obj => {
229+
return {
230+
...obj,
231+
active: e.calActive(obj),
232+
mortalityPer: e.calMortalityPer(obj),
233+
recoveredPer: e.calRecoveredPer(obj)
234+
};
235+
});
236+
};
237+
238+
e.htmlTemplate = (body) => {
239+
const template = `<!doctype html>
240+
<html lang="en">
241+
<head>
242+
<meta charset="utf-8">
243+
<meta name="viewport" content="width=device-width, initial-scale=1">
244+
<title>Coronavirus Tracker</title>
245+
<style>
246+
body {
247+
background-color: #0d0208;
248+
color: #00ff41;
249+
}
250+
pre {
251+
font-family: SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
252+
white-space: pre;
253+
}
254+
</style>
255+
</head>
256+
<body>
257+
<pre>${body}</pre>
258+
</body>
259+
</html>
260+
`;
261+
262+
return stripAnsi(template);
263+
};

0 commit comments

Comments
 (0)