Skip to content

Commit 7a6e824

Browse files
committed
👌 IMPROVE: Order + Flexibility
1 parent c8f2f6a commit 7a6e824

File tree

8 files changed

+88
-89
lines changed

8 files changed

+88
-89
lines changed

index.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ const handleError = require("cli-handle-error");
1919
const getCountry = require("./utils/getCountry.js");
2020
const getStates = require("./utils/getStates.js");
2121
const getWorldwide = require("./utils/getWorldwide.js");
22-
const getUpdated = require("./utils/getupdated.js");
2322
const {
2423
single,
2524
colored,
@@ -49,8 +48,8 @@ const sortBy = cli.flags.sort;
4948
spinner.start();
5049
const lastUpdated = await getWorldwide(table, states);
5150
await getCountry(spinner, table, states, country);
52-
await getStates(spinner, table, states, { sort: sortBy });
53-
await getAll(spinner, table, states, country, { sort: sortBy });
51+
await getStates(spinner, table, states, sortBy);
52+
await getAll(spinner, table, states, country, sortBy);
5453

5554
theEnd(lastUpdated, states);
5655
})();

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
"cli-table3": "^0.5.1",
3535
"cli-welcome": "^1.4.0",
3636
"comma-number": "^2.0.1",
37+
"lodash.orderby": "^4.6.0",
38+
"lodash.sortby": "^4.7.0",
3739
"meow": "^6.1.0",
3840
"ora": "^4.0.3",
3941
"update-notifier": "^4.1.0"

utils/cli.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ module.exports = meow(
3939
},
4040
sort: {
4141
type: "string",
42-
default: "cases-today",
42+
default: "cases",
4343
alias: "s"
4444
}
4545
}

utils/getAll.js

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,40 @@
11
const axios = require("axios");
22
const chalk = require("chalk");
33
const comma = require("comma-number");
4-
const { sortKeys, sortOrders } = require("./table.js");
4+
const { sortingKeys } = require("./table.js");
55
const to = require("await-to-js").default;
66
const handleError = require("cli-handle-error");
7+
const orderBy = require("lodash.orderby");
78

8-
module.exports = async (spinner, table, states, country, options) => {
9-
if (!country && !states) {
10-
const [err, api] = await to(
9+
module.exports = async (spinner, table, states, countryName, sortBy) => {
10+
if (!countryName && !states) {
11+
const [err, response] = await to(
1112
axios.get(`https://corona.lmao.ninja/countries`)
1213
);
1314
handleError(`API is down, try again later.`, err, false);
15+
let allCountries = response.data;
1416

15-
let allCountries = api.data.map(country => Object.values(country));
16-
17-
const sortIndex = sortKeys.indexOf(options.sort);
18-
19-
if (sortIndex != -1) {
20-
const dir = sortOrders[sortIndex];
21-
allCountries = allCountries.sort((a, b) =>
22-
a[sortIndex] > b[sortIndex] ? dir : -dir
23-
);
24-
}
17+
// Sort.
18+
allCountries = orderBy(allCountries, [sortingKeys[sortBy]], ["desc"]);
2519

20+
// Push selected data.
2621
allCountries.map((oneCountry, count) => {
27-
oneCountry = oneCountry
28-
.filter(stat => typeof stat !== "object")
29-
.map(stat => comma(stat));
30-
return table.push([count + 1, ...oneCountry]);
22+
table.push([
23+
count + 1,
24+
oneCountry.country,
25+
comma(oneCountry.cases),
26+
comma(oneCountry.todayCases),
27+
comma(oneCountry.deaths),
28+
comma(oneCountry.todayDeaths),
29+
comma(oneCountry.recovered),
30+
comma(oneCountry.active),
31+
comma(oneCountry.critical),
32+
comma(oneCountry.casesPerOneMillion)
33+
]);
3134
});
35+
3236
spinner.stopAndPersist();
33-
spinner.info(`${chalk.cyan(`Sorted by:`)} ${options.sort}`);
37+
spinner.info(`${chalk.cyan(`Sorted by:`)} ${sortBy}`);
3438
console.log(table.toString());
3539
}
3640
};

utils/getCountry.js

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,36 @@ const red = chalk.red;
66
const to = require("await-to-js").default;
77
const handleError = require("cli-handle-error");
88

9-
module.exports = async (spinner, table, states, country) => {
10-
if (country && !states) {
11-
const [err, api] = await to(
12-
axios.get(`https://corona.lmao.ninja/countries/${country}`)
9+
module.exports = async (spinner, table, states, countryName) => {
10+
if (countryName && !states) {
11+
const [err, response] = await to(
12+
axios.get(`https://corona.lmao.ninja/countries/${countryName}`)
1313
);
1414
handleError(`API is down, try again later.`, err, false);
15+
const thisCountry = response.data;
1516

16-
if (api.data === "Country not found") {
17+
if (response.data === "Country not found") {
1718
spinner.stopAndPersist();
1819
console.log(
1920
`${red(
20-
`${sym.error} Nops. A country named "${country}" does not exist…`
21+
`${sym.error} Nops. A country named "${countryName}" does not exist…`
2122
)}\n`
2223
);
2324
process.exit(0);
2425
}
25-
let dataPerCountry = Object.values(api.data);
26-
dataPerCountry = dataPerCountry
27-
.filter(stat => typeof stat !== "object")
28-
.map(stat => comma(stat));
29-
table.push([`—`, ...dataPerCountry]);
26+
27+
table.push([
28+
`—`,
29+
thisCountry.country,
30+
comma(thisCountry.cases),
31+
comma(thisCountry.todayCases),
32+
comma(thisCountry.deaths),
33+
comma(thisCountry.todayDeaths),
34+
comma(thisCountry.recovered),
35+
comma(thisCountry.active),
36+
comma(thisCountry.critical),
37+
comma(thisCountry.casesPerOneMillion)
38+
]);
3039
spinner.stopAndPersist();
3140
console.log(table.toString());
3241
}

utils/getStates.js

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,37 @@
11
const axios = require("axios");
22
const chalk = require("chalk");
33
const comma = require("comma-number");
4-
const { sortStateKeys, sortStateOrders } = require("./table.js");
4+
const { sortingStateKeys } = require("./table.js");
55
const to = require("await-to-js").default;
66
const handleError = require("cli-handle-error");
7+
const orderBy = require("lodash.orderby");
78

8-
module.exports = async (spinner, table, states, options) => {
9+
module.exports = async (spinner, table, states, sortBy) => {
910
if (states) {
10-
const [err, api] = await to(axios.get(`https://corona.lmao.ninja/states`));
11+
const [err, response] = await to(
12+
axios.get(`https://corona.lmao.ninja/states`)
13+
);
1114
handleError(`API is down, try again later.`, err, false);
15+
let allStates = response.data;
1216

13-
let allStates = api.data.map(one => Object.values(one));
14-
const sortIndex = sortStateKeys.indexOf(options.sort);
15-
16-
if (sortIndex != -1) {
17-
const dir = sortStateOrders[sortIndex];
18-
allStates = allStates.sort((a, b) =>
19-
a[sortIndex] > b[sortIndex] ? dir : -dir
20-
);
21-
}
17+
// Sort.
18+
allStates = orderBy(allStates, [sortingStateKeys[sortBy]], ["desc"]);
2219

20+
// Push selected data.
2321
allStates.map((oneState, count) => {
24-
oneState = oneState.map(d => comma(d));
25-
return table.push([count + 1, ...oneState]);
22+
table.push([
23+
count + 1,
24+
oneState.state,
25+
comma(oneState.cases),
26+
comma(oneState.todayCases),
27+
comma(oneState.deaths),
28+
comma(oneState.todayDeaths),
29+
comma(oneState.active)
30+
]);
2631
});
2732

2833
spinner.stopAndPersist();
29-
spinner.info(`${chalk.cyan(`Sorted by:`)} ${options.sort}`);
34+
spinner.info(`${chalk.cyan(`Sorted by:`)} ${sortBy}`);
3035
console.log(table.toString());
3136
}
3237
};

utils/getUpdated.js

Lines changed: 0 additions & 14 deletions
This file was deleted.

utils/table.js

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -28,26 +28,13 @@ module.exports = {
2828
`${red(`Critical`)}`,
2929
`Per Million`
3030
],
31-
sortKeys: [
32-
`country`,
33-
`country-info`,
34-
`cases`,
35-
`cases-today`,
36-
`deaths`,
37-
`deaths-today`,
38-
`recovered`,
39-
`active`,
40-
`critical`,
41-
`per-million`
42-
],
4331
singleStates: [
4432
`#`,
4533
`State`,
4634
`Cases`,
4735
`Cases ${dim(`(today)`)}`,
4836
`Deaths`,
4937
`Deaths ${dim(`(today)`)}`,
50-
`Recovered`,
5138
`Active`
5239
],
5340
coloredStates: [
@@ -57,19 +44,26 @@ module.exports = {
5744
`Cases ${dim(`(today)`)}`,
5845
`${red(`Deaths`)}`,
5946
`${red(`Deaths (today)`)}`,
60-
`${green(`Recovered`)}`,
6147
`Active`
6248
],
63-
sortStateKeys: [
64-
`state`,
65-
`cases`,
66-
`cases-today`,
67-
`deaths`,
68-
`deaths-today`,
69-
`recovered`,
70-
`active`
71-
],
72-
sortOrders: [1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
73-
sortStateOrders: [1, -1, -1, -1, -1, -1, -1],
74-
style: { head: ["cyan"] }
49+
style: { head: ["cyan"] },
50+
sortingKeys: {
51+
country: "country",
52+
cases: "cases",
53+
"cases-today": "todayCases",
54+
deaths: "deaths",
55+
"deaths-today": "todayDeaths",
56+
recovered: "recovered",
57+
active: "active",
58+
critical: "critical",
59+
"per-million": "casesPerOneMillion"
60+
},
61+
sortingStateKeys: {
62+
state: "state",
63+
cases: "cases",
64+
"cases-today": "todayCases",
65+
deaths: "deaths",
66+
"deaths-today": "todayDeaths",
67+
active: "active"
68+
}
7569
};

0 commit comments

Comments
 (0)