Skip to content

Commit 2f9fe4a

Browse files
Bernardo AndersonBernardo Anderson
andauthored
📦 NEW: Add US States #6 via @bernardodsanderson
* Add states as a view option * Fix naming Co-authored-by: Bernardo Anderson <banderson@fbsdata.com>
1 parent f863853 commit 2f9fe4a

File tree

5 files changed

+73
-10
lines changed

5 files changed

+73
-10
lines changed

index.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ const getAll = require('./utils/getAll.js');
1616
const theEnd = require('./utils/theEnd.js');
1717
const handleError = require('cli-handle-error');
1818
const getCountry = require('./utils/getCountry.js');
19+
const getStates = require('./utils/getStates.js');
1920
const getWorldwide = require('./utils/getWorldwide.js');
20-
const { single, colored, style } = require('./utils/table.js');
21+
const { single, colored, singleStates, coloredStates, style } = require('./utils/table.js');
2122
const xcolor = cli.flags.xcolor;
2223
const sortBy = cli.flags.sort;
24+
let isState = false;
2325

2426
(async () => {
2527
// Init.
@@ -28,16 +30,28 @@ const sortBy = cli.flags.sort;
2830
if (country === 'help') {
2931
cli.showHelp(0);
3032
}
33+
if (country === 'states') {
34+
isState = true;
35+
}
3136

3237
// Table
33-
const head = xcolor ? single : colored;
38+
let head;
39+
if (xcolor) {
40+
head = isState ? singleStates : single;
41+
} else {
42+
head = isState ? coloredStates : colored;
43+
}
3444
const table = new Table({ head, style });
3545

3646
// Display data.
3747
spinner.start();
38-
await getWorldwide(table);
39-
await getCountry(spinner, table, country);
48+
if (isState) {
49+
await getStates(spinner, table);
50+
} else {
51+
await getWorldwide(table);
52+
await getCountry(spinner, table, country);
53+
}
4054
await getAll(spinner, table, country, { sort: sortBy });
4155

42-
theEnd();
56+
theEnd(isState);
4357
})();

utils/cli.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@ module.exports = meow(
1111
1212
Commands
1313
${cyan(`country-name`)} Get data for a given country
14+
${cyan(`states`)} Get data for all USA states
1415
1516
Options
1617
${yellow(`--xcolor`)}, ${yellow(`-x`)} Single colored output
1718
${yellow(`--sort`)}, ${yellow(`-s`)} Sort data by type
1819
1920
Examples
20-
${green(`corona`)} ${cyan(`china`)}
21+
${green(`corona`)} ${cyan(`china`)}
22+
${green(`corona`)} ${cyan(`states`)}
2123
${green(`corona`)} ${yellow(`-x`)}
2224
${green(`corona`)} ${yellow(`--sort`)} ${cyan(`cases-today`)}
2325
${green(`corona`)} ${yellow(`-s`)} ${cyan(`critical`)}

utils/getStates.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const axios = require('axios');
2+
const chalk = require('chalk');
3+
const comma = require('comma-number');
4+
5+
module.exports = async (spinner, table) => {
6+
7+
const api = await axios.get(`https://corona.lmao.ninja/states`);
8+
let all = api.data.map(one => Object.values(one));
9+
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());
16+
};

utils/table.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,24 @@ module.exports = {
3737
`critical`,
3838
`per-million`
3939
],
40+
singleStates: [
41+
`State`,
42+
`Cases`,
43+
`Cases ${dim(`(today)`)}`,
44+
`Deaths`,
45+
`Deaths ${dim(`(today)`)}`,
46+
`Recovered`,
47+
`Active`
48+
],
49+
coloredStates: [
50+
`State`,
51+
`Cases`,
52+
`Cases ${dim(`(today)`)}`,
53+
`${red(`Deaths`)}`,
54+
`${red(`Deaths (today)`)}`,
55+
`${green(`Recovered`)}`,
56+
`Active`
57+
],
4058
sortOrders: [
4159
1,
4260
-1,

utils/theEnd.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,21 @@ const logSymbols = require('log-symbols');
33
const dim = chalk.dim;
44
const cyan = chalk.cyan;
55

6-
module.exports = async () => {
7-
console.log(`
8-
\n${logSymbols.info} ${cyan(`KEY:`)}
6+
module.exports = async (isState) => {
7+
if (isState) {
8+
console.log(`
9+
\n${logSymbols.info} ${cyan(`KEY:`)}
10+
${dim(`❯ `)}${cyan(`State:`)} Name of the state
11+
${dim(`❯ `)}${cyan(`Cases:`)} Total number of cases in a country
12+
${dim(`❯ `)}${cyan(`Cases (today):`)} Cases in 24 hours GMT/UTC
13+
${dim(`❯ `)}${cyan(`Deaths:`)} Total number of deaths in a state
14+
${dim(`❯ `)}${cyan(`Deaths (today):`)} Deaths in 24 hours GMT/UTC
15+
${dim(`❯ `)}${cyan(`Recovered:`)} Total number of recovered people
16+
${dim(`❯ `)}${cyan(`Active:`)} Total number of active patients
17+
`);
18+
} else {
19+
console.log(`
20+
\n${logSymbols.info} ${cyan(`KEY:`)}
921
${dim(`❯ `)}${cyan(`Country:`)} Name of the country
1022
${dim(`❯ `)}${cyan(`Cases:`)} Total number of cases in a country
1123
${dim(`❯ `)}${cyan(`Cases (today):`)} Cases in 24 hours GMT/UTC
@@ -15,7 +27,8 @@ ${dim(`❯ `)}${cyan(`Recovered:`)} Total number of recovered people
1527
${dim(`❯ `)}${cyan(`Active:`)} Total number of active patients
1628
${dim(`❯ `)}${cyan(`Critical:`)} Total number of critical patients
1729
${dim(`❯ `)}${cyan(`Per Million:`)} Affected patients per million
18-
`);
30+
`);
31+
}
1932
console.log(
2033
`\n${logSymbols.success} ${dim(`Star the repo for updates → https://git.io/corona-cli`)}\n${logSymbols.info} ${dim(
2134
`Follow for more CLIs → https://twitter.com/MrAhmadAwais\n\n`

0 commit comments

Comments
 (0)