Skip to content

Commit

Permalink
🐛 FIX: Sort + API Outcome
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmadawais committed Mar 23, 2020
1 parent 5e54736 commit f35cf99
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 14 deletions.
13 changes: 8 additions & 5 deletions utils/getAll.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,21 @@ const { sortKeys, sortOrders } = require("./table.js");
module.exports = async (spinner, table, states, country, options) => {
if (!country && !states) {
const api = await axios.get(`https://corona.lmao.ninja/countries`);
let all = api.data.map(one => Object.values(one));
let allCountries = api.data.map(country => Object.values(country));

const sortIndex = sortKeys.indexOf(options.sort);

if (sortIndex != -1) {
const dir = sortOrders[sortIndex];
all = all.sort((a, b) => (a[sortIndex] > b[sortIndex] ? dir : -dir));
allCountries = allCountries.sort((a, b) =>
a[sortIndex] > b[sortIndex] ? dir : -dir
);
}

all.map(one => {
one = one.map(d => comma(d));
return table.push(one);
allCountries.map((oneCountry, count) => {
oneCountry = [oneCountry[0], ...oneCountry.slice(2, oneCountry.length)];
oneCountry = oneCountry.map(stat => comma(stat));
return table.push([count + 1, ...oneCountry]);
});
spinner.stopAndPersist();
spinner.info(`${chalk.cyan(`Sorted by:`)} ${options.sort}`);
Expand Down
10 changes: 7 additions & 3 deletions utils/getCountry.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@ module.exports = async (spinner, table, states, country) => {
);
process.exit(0);
}
let data = Object.values(api.data);
data = data.map(d => comma(d));
table.push(data);
let dataPerCountry = Object.values(api.data);
dataPerCountry = [
dataPerCountry[0],
...dataPerCountry.slice(2, dataPerCountry.length)
];
dataPerCountry = dataPerCountry.map(stat => comma(stat));
table.push([`—`, ...dataPerCountry]);
spinner.stopAndPersist();
console.log(table.toString());
}
Expand Down
12 changes: 7 additions & 5 deletions utils/getStates.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,20 @@ const { sortStateKeys, sortStateOrders } = require("./table.js");
module.exports = async (spinner, table, states, options) => {
if (states) {
const api = await axios.get(`https://corona.lmao.ninja/states`);
let all = api.data.map(one => Object.values(one));
let allStates = api.data.map(one => Object.values(one));

const sortIndex = sortStateKeys.indexOf(options.sort);

if (sortIndex != -1) {
const dir = sortStateOrders[sortIndex];
all = all.sort((a, b) => (a[sortIndex] > b[sortIndex] ? dir : -dir));
allStates = allStates.sort((a, b) =>
a[sortIndex] > b[sortIndex] ? dir : -dir
);
}

all.map(one => {
one = one.map(d => comma(d));
return table.push(one);
allStates.map((oneState, count) => {
oneState = oneState.map(d => comma(d));
return table.push([count + 1, ...oneState]);
});

spinner.stopAndPersist();
Expand Down
1 change: 1 addition & 0 deletions utils/getWorldwide.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module.exports = async (table, states) => {
let data = Object.values(all.data);
data = data.map(d => comma(d));
table.push([
`—`,
`Worldwide`,
data[0],
`—`,
Expand Down
7 changes: 6 additions & 1 deletion utils/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const dim = chalk.dim;

module.exports = {
single: [
`#`,
`Country`,
`Cases`,
`Cases ${dim(`(today)`)}`,
Expand All @@ -16,6 +17,7 @@ module.exports = {
`Per Million`
],
colored: [
`#`,
`Country`,
`Cases`,
`Cases ${dim(`(today)`)}`,
Expand All @@ -28,6 +30,7 @@ module.exports = {
],
sortKeys: [
`country`,
`country-info`,
`cases`,
`cases-today`,
`deaths`,
Expand All @@ -38,6 +41,7 @@ module.exports = {
`per-million`
],
singleStates: [
`#`,
`State`,
`Cases`,
`Cases ${dim(`(today)`)}`,
Expand All @@ -47,6 +51,7 @@ module.exports = {
`Active`
],
coloredStates: [
`#`,
`State`,
`Cases`,
`Cases ${dim(`(today)`)}`,
Expand All @@ -64,7 +69,7 @@ module.exports = {
`recovered`,
`active`
],
sortOrders: [1, -1, -1, -1, -1, -1, -1, -1, -1],
sortOrders: [1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
sortStateOrders: [1, -1, -1, -1, -1, -1, -1],
style: { head: ["cyan"] }
};

0 comments on commit f35cf99

Please sign in to comment.