From 9796cbd7b76e7bff4ac16112cbc8404eb3fbb00e Mon Sep 17 00:00:00 2001 From: Shivathanu GC Date: Sun, 10 May 2020 21:39:32 +0100 Subject: [PATCH 1/2] DEV: Added multiple countries view --- index.js | 2 ++ utils/getMultipleCountries.js | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 utils/getMultipleCountries.js diff --git a/index.js b/index.js index c040557..dbd8cb0 100755 --- a/index.js +++ b/index.js @@ -16,6 +16,7 @@ const theEnd = require('./utils/theEnd.js'); const handleError = require('cli-handle-error'); const getStates = require('./utils/getStates.js'); const getCountry = require('./utils/getCountry.js'); +const getMultipleCountries = require('./utils/getMultipleCountries.js'); const getCountryChart = require('./utils/getCountryChart.js'); const getBar = require('./utils/getBar.js'); const getWorldwide = require('./utils/getWorldwide.js'); @@ -63,6 +64,7 @@ const options = { sortBy, limit, reverse, minimal, chart, log, json, bar }; spinner.start(); const lastUpdated = await getWorldwide(output, states, json); await getCountry(spinner, output, states, country, options); + await getMultipleCountries(spinner, output, states, country, options); await getStates(spinner, output, states, options); await getCountries(spinner, output, states, country, options); await getCountryChart(spinner, country, options); diff --git a/utils/getMultipleCountries.js b/utils/getMultipleCountries.js new file mode 100644 index 0000000..876aa01 --- /dev/null +++ b/utils/getMultipleCountries.js @@ -0,0 +1,35 @@ +const axios = require('axios'); +const numberFormat = require('./numberFormat'); +const exitCountry = require('./exitCountry'); +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) { + const [err, response] = await to( + axios.get(`https://corona.lmao.ninja/v2/countries/${countryName}`) + ); + exitCountry(err, spinner, countryName); + err && spinner.stopAndPersist(); + handleError(`API is down, try again later.`, err, false); + const thisCountry = response.data; + + // Format. + const format = numberFormat(options.json); + + thisCountry.map(data => table.push([ + `—`, + data.country, + format(data.cases), + format(data.todayCases), + format(data.deaths), + format(data.todayDeaths), + format(data.recovered), + format(data.active), + format(data.critical), + format(data.casesPerOneMillion) + ])); + spinner.stopAndPersist(); + console.log(table.toString()); + } +}; From f07a6464135a5bbe3e188ae26883a816c24ff172 Mon Sep 17 00:00:00 2001 From: Shivathanu Date: Wed, 20 May 2020 18:08:40 +0100 Subject: [PATCH 2/2] FIX: Address PR comments --- utils/getMultipleCountries.js | 37 +++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/utils/getMultipleCountries.js b/utils/getMultipleCountries.js index 876aa01..927cd90 100644 --- a/utils/getMultipleCountries.js +++ b/utils/getMultipleCountries.js @@ -4,31 +4,34 @@ const exitCountry = require('./exitCountry'); 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) { +module.exports = async (spinner, table, states, countryList, options) => { + if (countryList && !states && !options.chart) { const [err, response] = await to( - axios.get(`https://corona.lmao.ninja/v2/countries/${countryName}`) + axios.get(`https://corona.lmao.ninja/v2/countries/${countryList}`) ); - exitCountry(err, spinner, countryName); + exitCountry(err, spinner, countryList); err && spinner.stopAndPersist(); handleError(`API is down, try again later.`, err, false); - const thisCountry = response.data; + const countries = response.data; // Format. const format = numberFormat(options.json); - thisCountry.map(data => table.push([ - `—`, - data.country, - format(data.cases), - format(data.todayCases), - format(data.deaths), - format(data.todayDeaths), - format(data.recovered), - format(data.active), - format(data.critical), - format(data.casesPerOneMillion) - ])); + if (Array.isArray(countries)) { + countries.map(data => table.push([ + `—`, + data.country, + format(data.cases), + format(data.todayCases), + format(data.deaths), + format(data.todayDeaths), + format(data.recovered), + format(data.active), + format(data.critical), + format(data.casesPerOneMillion) + ])); + } + spinner.stopAndPersist(); console.log(table.toString()); }