Skip to content

Commit 9696910

Browse files
committed
📦 NEW: Sort in States
1 parent 2f9fe4a commit 9696910

File tree

9 files changed

+136
-93
lines changed

9 files changed

+136
-93
lines changed

.prettierrc.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"prettier.printWidth": 80,
3+
"prettier.singleQuote": true,
4+
"prettier.useTabs": true,
5+
"prettier.tabWidth": 4,
6+
"prettier.semi": true
7+
}

index.js

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,55 +3,53 @@
33
// Makes the script crash on unhandled rejections instead of silently
44
// ignoring them. In the future, promise rejections that are not handled will
55
// terminate the Node.js process with a non-zero exit code.
6-
process.on('unhandledRejection', err => {
6+
process.on("unhandledRejection", err => {
77
handleError(`UNHANDLED ERROR`, err);
88
});
99

10-
const ora = require('ora');
11-
const spinner = ora({ text: '' });
12-
const Table = require('cli-table3');
13-
const cli = require('./utils/cli.js');
14-
const init = require('./utils/init.js');
15-
const getAll = require('./utils/getAll.js');
16-
const theEnd = require('./utils/theEnd.js');
17-
const handleError = require('cli-handle-error');
18-
const getCountry = require('./utils/getCountry.js');
19-
const getStates = require('./utils/getStates.js');
20-
const getWorldwide = require('./utils/getWorldwide.js');
21-
const { single, colored, singleStates, coloredStates, style } = require('./utils/table.js');
10+
const ora = require("ora");
11+
const spinner = ora({ text: "" });
12+
const Table = require("cli-table3");
13+
const cli = require("./utils/cli.js");
14+
const init = require("./utils/init.js");
15+
const getAll = require("./utils/getAll.js");
16+
const showHelp = require("./utils/showHelp.js");
17+
const theEnd = require("./utils/theEnd.js");
18+
const handleError = require("cli-handle-error");
19+
const getCountry = require("./utils/getCountry.js");
20+
const getStates = require("./utils/getStates.js");
21+
const getWorldwide = require("./utils/getWorldwide.js");
22+
const {
23+
single,
24+
colored,
25+
singleStates,
26+
coloredStates,
27+
style
28+
} = require("./utils/table.js");
2229
const xcolor = cli.flags.xcolor;
2330
const sortBy = cli.flags.sort;
24-
let isState = false;
2531

2632
(async () => {
2733
// Init.
2834
init();
29-
const [country] = cli.input;
30-
if (country === 'help') {
31-
cli.showHelp(0);
32-
}
33-
if (country === 'states') {
34-
isState = true;
35-
}
35+
const [input] = cli.input;
36+
await showHelp();
37+
const states = input === "states" ? true : false;
38+
const country = input;
3639

3740
// Table
38-
let head;
39-
if (xcolor) {
40-
head = isState ? singleStates : single;
41-
} else {
42-
head = isState ? coloredStates : colored;
43-
}
44-
const table = new Table({ head, style });
41+
const head = xcolor ? single : colored;
42+
const headStates = xcolor ? singleStates : coloredStates;
43+
const table = !states
44+
? new Table({ head, style })
45+
: new Table({ head: headStates, style });
4546

4647
// Display data.
4748
spinner.start();
48-
if (isState) {
49-
await getStates(spinner, table);
50-
} else {
51-
await getWorldwide(table);
52-
await getCountry(spinner, table, country);
53-
}
54-
await getAll(spinner, table, country, { sort: sortBy });
49+
await getWorldwide(table, states);
50+
await getCountry(spinner, table, states, country);
51+
await getStates(spinner, table, states, { sort: sortBy });
52+
await getAll(spinner, table, states, country, { sort: sortBy });
5553

56-
theEnd(isState);
54+
theEnd(states);
5755
})();

utils/getAll.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
const axios = require('axios');
2-
const chalk = require('chalk');
3-
const comma = require('comma-number');
4-
const sortKeys = require('./table.js').sortKeys;
5-
const sortOrders = require('./table.js').sortOrders;
1+
const axios = require("axios");
2+
const chalk = require("chalk");
3+
const comma = require("comma-number");
4+
const { sortKeys, sortOrders } = require("./table.js");
65

7-
module.exports = async (spinner, table, country, options) => {
8-
if (!country) {
6+
module.exports = async (spinner, table, states, country, options) => {
7+
if (!country && !states) {
98
const api = await axios.get(`https://corona.lmao.ninja/countries`);
109
let all = api.data.map(one => Object.values(one));
1110

utils/getCountry.js

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
1-
const chalk = require('chalk');
2-
const axios = require('axios');
3-
const logSymbols = require('log-symbols');
4-
const comma = require('comma-number');
1+
const chalk = require("chalk");
2+
const axios = require("axios");
3+
const logSymbols = require("log-symbols");
4+
const comma = require("comma-number");
55
const red = chalk.red;
66
const green = chalk.green;
77

8-
module.exports = async (spinner, table, country) => {
9-
if (country) {
10-
const api = await axios.get(`https://corona.lmao.ninja/countries/${country}`);
11-
if (api.data === 'Country not found') {
8+
module.exports = async (spinner, table, states, country) => {
9+
if (country && !states) {
10+
const api = await axios.get(
11+
`https://corona.lmao.ninja/countries/${country}`
12+
);
13+
if (api.data === "Country not found") {
1214
spinner.stopAndPersist();
13-
console.log(`${red(`${logSymbols.error} Nops. A country named "${country}" does not exist…`)}\n`);
15+
console.log(
16+
`${red(
17+
`${logSymbols.error} Nops. A country named "${country}" does not exist…`
18+
)}\n`
19+
);
1420
process.exit(0);
1521
}
1622
let data = Object.values(api.data);

utils/getStates.js

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,27 @@
1-
const axios = require('axios');
2-
const chalk = require('chalk');
3-
const comma = require('comma-number');
1+
const axios = require("axios");
2+
const chalk = require("chalk");
3+
const comma = require("comma-number");
4+
const { sortStateKeys, sortStateOrders } = require("./table.js");
45

5-
module.exports = async (spinner, table) => {
6+
module.exports = async (spinner, table, states, options) => {
7+
if (states) {
8+
const api = await axios.get(`https://corona.lmao.ninja/states`);
9+
let all = api.data.map(one => Object.values(one));
610

7-
const api = await axios.get(`https://corona.lmao.ninja/states`);
8-
let all = api.data.map(one => Object.values(one));
11+
const sortIndex = sortStateKeys.indexOf(options.sort);
912

10-
all.map(one => {
11-
one = one.map(d => comma(d));
12-
return table.push(one);
13-
});
14-
spinner.stopAndPersist();
15-
console.log(table.toString());
13+
if (sortIndex != -1) {
14+
const dir = sortStateOrders[sortIndex];
15+
all = all.sort((a, b) => (a[sortIndex] > b[sortIndex] ? dir : -dir));
16+
}
17+
18+
all.map(one => {
19+
one = one.map(d => comma(d));
20+
return table.push(one);
21+
});
22+
23+
spinner.stopAndPersist();
24+
spinner.info(`${chalk.cyan(`Sorted by:`)} ${options.sort}`);
25+
console.log(table.toString());
26+
}
1627
};

utils/getWorldwide.js

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
1-
const axios = require('axios');
2-
const comma = require('comma-number');
1+
const axios = require("axios");
2+
const comma = require("comma-number");
33

4-
module.exports = async table => {
5-
const all = await axios.get(`https://corona.lmao.ninja/all`);
6-
let data = Object.values(all.data);
7-
data = data.map(d => comma(d));
8-
table.push([`Worldwide`, data[0], `—`, data[1], `—`, data[2], `—`, `—`, `—`]);
4+
module.exports = async (table, states) => {
5+
if (!states) {
6+
const all = await axios.get(`https://corona.lmao.ninja/all`);
7+
let data = Object.values(all.data);
8+
data = data.map(d => comma(d));
9+
table.push([
10+
`Worldwide`,
11+
data[0],
12+
`—`,
13+
data[1],
14+
`—`,
15+
data[2],
16+
`—`,
17+
`—`,
18+
`—`
19+
]);
20+
}
921
};

utils/showHelp.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = async country => {
2+
if (country === "help") {
3+
await cli.showHelp(0);
4+
}
5+
};

utils/table.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const chalk = require('chalk');
1+
const chalk = require("chalk");
22
const green = chalk.green;
33
const red = chalk.red;
44
const dim = chalk.dim;
@@ -55,16 +55,16 @@ module.exports = {
5555
`${green(`Recovered`)}`,
5656
`Active`
5757
],
58-
sortOrders: [
59-
1,
60-
-1,
61-
-1,
62-
-1,
63-
-1,
64-
-1,
65-
-1,
66-
-1,
67-
-1
58+
sortStateKeys: [
59+
`state`,
60+
`cases`,
61+
`cases-today`,
62+
`deaths`,
63+
`deaths-today`,
64+
`recovered`,
65+
`active`
6866
],
69-
style: { head: ['cyan'] }
67+
sortOrders: [1, -1, -1, -1, -1, -1, -1, -1, -1],
68+
sortStateOrders: [1, -1, -1, -1, -1, -1, -1],
69+
style: { head: ["cyan"] }
7070
};

utils/theEnd.js

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
const chalk = require('chalk');
2-
const logSymbols = require('log-symbols');
3-
const dim = chalk.dim;
1+
const logSymbols = require("log-symbols");
2+
const chalk = require("chalk");
43
const cyan = chalk.cyan;
4+
const dim = chalk.dim;
55

6-
module.exports = async (isState) => {
7-
if (isState) {
8-
console.log(`
6+
const infoStates = () =>
7+
console.log(`
98
\n${logSymbols.info} ${cyan(`KEY:`)}
109
${dim(`❯ `)}${cyan(`State:`)} Name of the state
1110
${dim(`❯ `)}${cyan(`Cases:`)} Total number of cases in a country
@@ -15,8 +14,9 @@ ${dim(`❯ `)}${cyan(`Deaths (today):`)} Deaths in 24 hours GMT/UTC
1514
${dim(`❯ `)}${cyan(`Recovered:`)} Total number of recovered people
1615
${dim(`❯ `)}${cyan(`Active:`)} Total number of active patients
1716
`);
18-
} else {
19-
console.log(`
17+
18+
const infoCountries = () =>
19+
console.log(`
2020
\n${logSymbols.info} ${cyan(`KEY:`)}
2121
${dim(`❯ `)}${cyan(`Country:`)} Name of the country
2222
${dim(`❯ `)}${cyan(`Cases:`)} Total number of cases in a country
@@ -28,9 +28,14 @@ ${dim(`❯ `)}${cyan(`Active:`)} Total number of active patients
2828
${dim(`❯ `)}${cyan(`Critical:`)} Total number of critical patients
2929
${dim(`❯ `)}${cyan(`Per Million:`)} Affected patients per million
3030
`);
31-
}
31+
32+
module.exports = async states => {
33+
states && infoStates();
34+
!states && infoCountries();
3235
console.log(
33-
`\n${logSymbols.success} ${dim(`Star the repo for updates → https://git.io/corona-cli`)}\n${logSymbols.info} ${dim(
36+
`\n${logSymbols.success} ${dim(
37+
`Star the repo for updates → https://git.io/corona-cli`
38+
)}\n${logSymbols.info} ${dim(
3439
`Follow for more CLIs → https://twitter.com/MrAhmadAwais\n\n`
3540
)}`
3641
);

0 commit comments

Comments
 (0)