Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NEW: Implementation: Compare Feature #53

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const getCountry = require('./utils/getCountry.js');
const getCountryChart = require('./utils/getCountryChart.js');
const getWorldwide = require('./utils/getWorldwide.js');
const getCountries = require('./utils/getCountries.js');
const getCountriesCompare = require('./utils/getCountriesCompare.js');
const {
style,
single,
Expand All @@ -37,14 +38,17 @@ const limit = Math.abs(cli.flags.limit);
const chart = cli.flags.chart;
const log = cli.flags.log;
const minimal = cli.flags.minimal;
const options = { sortBy, limit, reverse, minimal, chart, log };
const compare = cli.flags.compare.length > 1;
const options = { sortBy, limit, reverse, minimal, chart, log, compare };

(async () => {
// Init.
init(minimal);
input === 'help' && (await cli.showHelp(0));
const states = input === 'states' ? true : false;
const country = input;
const [...compareCountries] =
cli.input.length > 0 ? cli.input : cli.flags.compare;

// Table
const head = xcolor ? single : colored;
Expand All @@ -58,6 +62,7 @@ const options = { sortBy, limit, reverse, minimal, chart, log };
spinner.start();
const lastUpdated = await getWorldwide(table, states);
await getCountry(spinner, table, states, country, options);
await getCountriesCompare(spinner, table, compareCountries, options);
await getStates(spinner, table, states, options);
await getCountries(spinner, table, states, country, options);
await getCountryChart(spinner, country, options);
Expand Down
7 changes: 7 additions & 0 deletions utils/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ module.exports = meow(
${cyan(`states`)} Get data for all USA states

Options
${yellow(`--xcolor`)}, ${yellow(`-x`)} Single colored output
${yellow(`--compare`)}, ${yellow(`-c`)} Compare countries
${yellow(`--sort`)}, ${yellow(`-s`)} Sort data by type
${yellow(`--reverse`)}, ${yellow(`-r`)} Reverse print order
${yellow(`--limit`)}, ${yellow(`-l`)} Print only N entries
Expand Down Expand Up @@ -72,6 +74,11 @@ module.exports = meow(
type: 'boolean',
default: false,
alias: 'm'
},
compare: {
type: 'array',
default: [],
alias: 'c'
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions utils/getCountries.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ module.exports = async (
table,
states,
countryName,
{ sortBy, limit, reverse }
{ sortBy, limit, reverse, compare }
) => {
if (!countryName && !states) {
if (!countryName && !states && !compare) {
const [err, response] = await to(
axios.get(`https://corona.lmao.ninja/countries`)
);
Expand Down
75 changes: 75 additions & 0 deletions utils/getCountriesCompare.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
const chalk = require('chalk');
const axios = require('axios');
const sym = require('log-symbols');
const comma = require('comma-number');
const red = chalk.red;
const to = require('await-to-js').default;
const handleError = require('cli-handle-error');

module.exports = async (spinner, table, compareCountries, { compare }) => {
if (compareCountries && compare) {
const tableArr = [];
for (let index = 0; index < compareCountries.length; index++) {
const [err, response] = await to(
axios.get(
`https://corona.lmao.ninja/countries/${compareCountries[index]}`
)
);
const thisCountry = response.data;
handleError(`API is down, try again later.`, err, false);
if (response.data === 'Country not found') {
spinner.stopAndPersist();
console.log(
`${red(
`${sym.error} Nops. A country named "${compareCountries[index]}" does not exist… Could not compare`
)}\n`
);
process.exit(0);
}

tableArr.push([
`—`,
thisCountry.country,
thisCountry.cases,
thisCountry.todayCases,
thisCountry.deaths,
thisCountry.todayDeaths,
thisCountry.recovered,
thisCountry.active,
thisCountry.critical,
thisCountry.casesPerOneMillion
]);
}

// Comparing the country values to highlight the highest count
for (let index = 2; index < 10; index++) {
let highestValueIndex = 0;
for (
let innerIndex = 1;
innerIndex < tableArr.length;
innerIndex++
) {
if (
tableArr[highestValueIndex][index] <
tableArr[innerIndex][index]
) {
highestValueIndex = innerIndex;
}
tableArr[innerIndex][index] = tableArr[innerIndex][index];
}
if (tableArr[highestValueIndex][index]) {
tableArr[highestValueIndex][index] = red(
comma(tableArr[highestValueIndex][index])
);
}

tableArr.forEach((row) => {
row[index] = comma(row[index]);
});
}

table.push(...tableArr);
spinner.stopAndPersist();
console.log(table.toString());
}
};
2 changes: 1 addition & 1 deletion utils/getCountry.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const to = require('await-to-js').default;
const handleError = require('cli-handle-error');

module.exports = async (spinner, table, states, countryName, options) => {
if (countryName && !states && !options.chart) {
if (countryName && !states && !options.chart && !options.compare) {
const [err, response] = await to(
axios.get(`https://corona.lmao.ninja/countries/${countryName}`)
);
Expand Down