Skip to content

Commit 73432df

Browse files
committed
📦 NEW: Sort Validation
1 parent 1a09647 commit 73432df

File tree

5 files changed

+55
-0
lines changed

5 files changed

+55
-0
lines changed

utils/getCountries.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const { sortingKeys } = require('./table.js');
77
const to = require('await-to-js').default;
88
const handleError = require('cli-handle-error');
99
const orderBy = require('lodash.orderby');
10+
const sortValidation = require('./sortValidation.js');
1011

1112
module.exports = async (
1213
spinner,
@@ -17,6 +18,8 @@ module.exports = async (
1718
{ sortBy, limit, reverse }
1819
) => {
1920
if (!countryName && !states && !bar) {
21+
sortValidation(sortBy, spinner);
22+
2023
const [err, response] = await to(
2124
axios.get(`https://corona.lmao.ninja/v2/countries`)
2225
);

utils/getCountryChart.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ module.exports = async (spinner, countryName, { chart, log }) => {
7575
spinner.stop();
7676
line.setData([casesSeries, deathsSeries, recoveredSeries]);
7777
screen.render();
78+
7879
await new Promise((resolve, _) => {
7980
screen.key(['escape', 'q', 'C-c', 'enter', 'space'], (ch, key) => {
8081
return process.exit(0);

utils/getStates.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ const { sortingStateKeys } = require('./table.js');
77
const to = require('await-to-js').default;
88
const handleError = require('cli-handle-error');
99
const orderBy = require('lodash.orderby');
10+
const sortStateValidation = require('./sortStateValidation.js');
1011

1112
module.exports = async (spinner, table, states, { sortBy, limit, reverse }) => {
1213
if (states) {
14+
sortStateValidation(sortBy, spinner);
1315
const [err, response] = await to(
1416
axios.get(`https://corona.lmao.ninja/v2/states`)
1517
);

utils/sortStateValidation.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const sym = require('log-symbols');
2+
const { sortingStateKeys } = require('./table.js');
3+
const { red, green, dim } = require('chalk');
4+
5+
module.exports = (sortBy, spinner) => {
6+
if (sortBy !== 'cases') {
7+
if (Object.keys(sortingStateKeys).indexOf(sortBy) === -1) {
8+
spinner.stop();
9+
console.log(`${sym.error} ${red(`Wrong sorting key!`)}`);
10+
console.log(`${sym.info} You can only sort by:
11+
${dim(`-`)} ${green(`cases`)}
12+
${dim(`-`)} ${green(`cases-today`)}
13+
${dim(`-`)} ${green(`deaths`)}
14+
${dim(`-`)} ${green(`deaths-today`)}
15+
${dim(`-`)} ${green(`active`)}\n`);
16+
process.exit(0);
17+
}
18+
// It is a custom sort.
19+
return true;
20+
}
21+
// Not a custom sort.
22+
return false;
23+
};

utils/sortValidation.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const sym = require('log-symbols');
2+
const { sortingKeys } = require('./table.js');
3+
const { red, green, dim } = require('chalk');
4+
5+
module.exports = (sortBy, spinner) => {
6+
if (sortBy !== 'cases') {
7+
if (Object.keys(sortingKeys).indexOf(sortBy) === -1) {
8+
spinner.stop();
9+
console.log(`${sym.error} ${red(`Wrong sorting key!`)}`);
10+
console.log(`${sym.info} You can only sort by:
11+
${dim(`-`)} ${green(`cases`)}
12+
${dim(`-`)} ${green(`cases-today`)}
13+
${dim(`-`)} ${green(`deaths`)}
14+
${dim(`-`)} ${green(`deaths-today`)}
15+
${dim(`-`)} ${green(`recovered`)}
16+
${dim(`-`)} ${green(`active`)}
17+
${dim(`-`)} ${green(`critical`)}
18+
${dim(`-`)} ${green(`per-million`)}\n`);
19+
process.exit(0);
20+
}
21+
// It is a custom sort.
22+
return true;
23+
}
24+
// Not a custom sort.
25+
return false;
26+
};

0 commit comments

Comments
 (0)