Skip to content

Commit

Permalink
馃憣 IMPROVE: Categories
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmadawais committed Apr 23, 2020
1 parent 76d2d70 commit 47ba82f
Showing 1 changed file with 31 additions and 21 deletions.
52 changes: 31 additions & 21 deletions utils/getBar.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,49 @@
const comma = require('comma-number');
const handleError = require('cli-handle-error');
const axios = require('axios');
const to = require('await-to-js').default;
const moment = require('moment');
const blessed = require('blessed');
const contrib = require('blessed-contrib');
const { sortingKeys } = require('./table.js');
const orderBy = require('lodash.orderby');
const { cyan, dim } = require('chalk');
const sortValidation = require('./sortValidation.js');

module.exports = async (spinner, countryName, states, { bar, log, sortBy, limit, reverse }) => {
module.exports = async (
spinner,
countryName,
states,
{ bar, log, sortBy, limit, reverse }
) => {
if (!countryName && !states && bar) {
// Handle custom sorting and validate it.
const customSort = sortValidation(sortBy, spinner);

const [err, response] = await to(
axios.get(`https://corona.lmao.ninja/v2/countries`)
);
handleError(`API is down, try again later.`, err, false);
let allCountries = response.data;
let allCountries = response.data;

// Sort & reverse.
// Sort & reverse.
const direction = reverse ? 'asc' : 'desc';
allCountries = orderBy(
allCountries,
[sortingKeys[sortBy]],
[direction]
);
// Limit.
limit = limit>12 ? 12: limit;
allCountries = allCountries.slice(0, limit);

// Limit.
limit = limit > 12 ? 12 : limit;
allCountries = allCountries.slice(0, limit);

let logScale = x => x;
let logScale = x => x;
if (log) {
logScale = x => (x === 0 ? undefined : Math.log(x));
}
// Format Stack Data
barCountries = {};

// Format Stack Data.
barCountries = {};

allCountries.map(country => {
if (customSort) {
barCountries[country.country] = [
Expand All @@ -52,8 +58,9 @@ module.exports = async (spinner, countryName, states, { bar, log, sortBy, limit,
}
});

const names = Object.keys(barCountries);
const data = Object.values(barCountries);
const names = Object.keys(barCountries);
const data = Object.values(barCountries);
const screen = blessed.screen();

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

screen.append(stack);
spinner.stop();
stack.setData(
{ barCategory: names
, stackedCategory: ['CASES', 'DEATHS', 'RECOVERED']
, data:
data
});
screen.append(stack);
spinner.stop();
stack.setData({
barCategory: names,
stackedCategory:
sortBy !== 'cases'
? [`${sortBy.toUpperCase()}`]
: ['CASES', 'DEATHS', 'RECOVERED'],
data: data
});
screen.render();

await new Promise((resolve, _) => {
screen.key(['escape', 'q', 'C-c', 'enter', 'space'], (ch, key) => {
return process.exit(0);
Expand Down

0 comments on commit 47ba82f

Please sign in to comment.