diff --git a/index.js b/index.js index 053222e..14eceac 100755 --- a/index.js +++ b/index.js @@ -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, @@ -37,7 +38,8 @@ 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. @@ -45,6 +47,8 @@ const options = { sortBy, limit, reverse, minimal, chart, log }; 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; @@ -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); diff --git a/utils/cli.js b/utils/cli.js index 93e1916..39f2f24 100644 --- a/utils/cli.js +++ b/utils/cli.js @@ -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 @@ -72,6 +74,11 @@ module.exports = meow( type: 'boolean', default: false, alias: 'm' + }, + compare: { + type: 'array', + default: [], + alias: 'c' } } } diff --git a/utils/getCountries.js b/utils/getCountries.js index 8c63676..0a3c946 100644 --- a/utils/getCountries.js +++ b/utils/getCountries.js @@ -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`) ); diff --git a/utils/getCountriesCompare.js b/utils/getCountriesCompare.js new file mode 100644 index 0000000..9590d4b --- /dev/null +++ b/utils/getCountriesCompare.js @@ -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()); + } +}; diff --git a/utils/getCountry.js b/utils/getCountry.js index 74aa818..6b9b915 100644 --- a/utils/getCountry.js +++ b/utils/getCountry.js @@ -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}`) );