Skip to content

Commit 47ba82f

Browse files
committed
👌 IMPROVE: Categories
1 parent 76d2d70 commit 47ba82f

File tree

1 file changed

+31
-21
lines changed

1 file changed

+31
-21
lines changed

utils/getBar.js

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,49 @@
1-
const comma = require('comma-number');
21
const handleError = require('cli-handle-error');
32
const axios = require('axios');
43
const to = require('await-to-js').default;
5-
const moment = require('moment');
64
const blessed = require('blessed');
75
const contrib = require('blessed-contrib');
86
const { sortingKeys } = require('./table.js');
97
const orderBy = require('lodash.orderby');
108
const { cyan, dim } = require('chalk');
119
const sortValidation = require('./sortValidation.js');
1210

13-
module.exports = async (spinner, countryName, states, { bar, log, sortBy, limit, reverse }) => {
11+
module.exports = async (
12+
spinner,
13+
countryName,
14+
states,
15+
{ bar, log, sortBy, limit, reverse }
16+
) => {
1417
if (!countryName && !states && bar) {
1518
// Handle custom sorting and validate it.
1619
const customSort = sortValidation(sortBy, spinner);
20+
1721
const [err, response] = await to(
1822
axios.get(`https://corona.lmao.ninja/v2/countries`)
1923
);
2024
handleError(`API is down, try again later.`, err, false);
21-
let allCountries = response.data;
25+
let allCountries = response.data;
2226

23-
// Sort & reverse.
27+
// Sort & reverse.
2428
const direction = reverse ? 'asc' : 'desc';
2529
allCountries = orderBy(
2630
allCountries,
2731
[sortingKeys[sortBy]],
2832
[direction]
2933
);
30-
// Limit.
31-
limit = limit>12 ? 12: limit;
32-
allCountries = allCountries.slice(0, limit);
3334

35+
// Limit.
36+
limit = limit > 12 ? 12 : limit;
37+
allCountries = allCountries.slice(0, limit);
3438

35-
let logScale = x => x;
39+
let logScale = x => x;
3640
if (log) {
3741
logScale = x => (x === 0 ? undefined : Math.log(x));
3842
}
39-
// Format Stack Data
40-
barCountries = {};
43+
44+
// Format Stack Data.
45+
barCountries = {};
46+
4147
allCountries.map(country => {
4248
if (customSort) {
4349
barCountries[country.country] = [
@@ -52,8 +58,9 @@ module.exports = async (spinner, countryName, states, { bar, log, sortBy, limit,
5258
}
5359
});
5460

55-
const names = Object.keys(barCountries);
56-
const data = Object.values(barCountries);
61+
const names = Object.keys(barCountries);
62+
const data = Object.values(barCountries);
63+
const screen = blessed.screen();
5764

5865
// Better colors.
5966
const getColors = {
@@ -83,15 +90,18 @@ module.exports = async (spinner, countryName, states, { bar, log, sortBy, limit,
8390
legend: { width: 20, padding: 5 }
8491
});
8592

86-
screen.append(stack);
87-
spinner.stop();
88-
stack.setData(
89-
{ barCategory: names
90-
, stackedCategory: ['CASES', 'DEATHS', 'RECOVERED']
91-
, data:
92-
data
93-
});
93+
screen.append(stack);
94+
spinner.stop();
95+
stack.setData({
96+
barCategory: names,
97+
stackedCategory:
98+
sortBy !== 'cases'
99+
? [`${sortBy.toUpperCase()}`]
100+
: ['CASES', 'DEATHS', 'RECOVERED'],
101+
data: data
102+
});
94103
screen.render();
104+
95105
await new Promise((resolve, _) => {
96106
screen.key(['escape', 'q', 'C-c', 'enter', 'space'], (ch, key) => {
97107
return process.exit(0);

0 commit comments

Comments
 (0)