diff --git a/index.js b/index.js index 054fa41..6527384 100755 --- a/index.js +++ b/index.js @@ -3,7 +3,7 @@ // Makes the script crash on unhandled rejections instead of silently // ignoring them. In the future, promise rejections that are not handled will // terminate the Node.js process with a non-zero exit code. -process.on('unhandledRejection', (err) => { +process.on('unhandledRejection', err => { handleError(`UNHANDLED ERROR`, err); }); @@ -23,13 +23,12 @@ const { colored, singleStates, coloredStates, - style, + style } = require('./utils/table.js'); const xcolor = cli.flags.xcolor; const sortBy = cli.flags.sort; const reverse = cli.flags.reverse; const limit = Math.abs(cli.flags.limit); - const options = { sortBy, limit, reverse }; (async () => { diff --git a/readme.md b/readme.md index 261b15b..523dff8 100644 --- a/readme.md +++ b/readme.md @@ -113,12 +113,11 @@ corona -s active -r [![📟](./.github/sort.gif)](./../../) -### Limit output - -Only output the top N results (depending on your sort criteria). Defaults to showing all entries. +### Limit the output ````sh -corona --sort cases -n 10 +# Print a limited number of entries to the output. +corona --limit 10 ```` #### CLI Help diff --git a/utils/cli.js b/utils/cli.js index 34e8904..83ac1d4 100644 --- a/utils/cli.js +++ b/utils/cli.js @@ -37,22 +37,22 @@ module.exports = meow( xcolor: { type: 'boolean', default: false, - alias: 'x', + alias: 'x' }, sort: { type: 'string', default: 'cases', - alias: 's', + alias: 's' }, reverse: { type: 'boolean', default: false, - alias: 'r', + alias: 'r' }, limit: { type: 'number', default: Number.MAX_SAFE_INTEGER, - alias: 'n' + alias: 'l' } } } diff --git a/utils/getCountries.js b/utils/getCountries.js index 5359c50..31cf9e5 100644 --- a/utils/getCountries.js +++ b/utils/getCountries.js @@ -8,7 +8,13 @@ const to = require('await-to-js').default; const handleError = require('cli-handle-error'); const orderBy = require('lodash.orderby'); -module.exports = async (spinner, table, states, countryName, { sortBy, limit, reverse }) => { +module.exports = async ( + spinner, + table, + states, + countryName, + { sortBy, limit, reverse } +) => { if (!countryName && !states) { const [err, response] = await to( axios.get(`https://corona.lmao.ninja/countries`) @@ -16,6 +22,9 @@ module.exports = async (spinner, table, states, countryName, { sortBy, limit, re handleError(`API is down, try again later.`, err, false); let allCountries = response.data; + // Limit. + allCountries = allCountries.slice(0, limit); + // Sort & reverse. const direction = reverse ? 'asc' : 'desc'; allCountries = orderBy( @@ -24,9 +33,6 @@ module.exports = async (spinner, table, states, countryName, { sortBy, limit, re [direction] ); - // Limit - allCountries = allCountries.slice(0, limit); - // Push selected data. allCountries.map((oneCountry, count) => { table.push([ diff --git a/utils/getStates.js b/utils/getStates.js index d66a644..4eaee68 100644 --- a/utils/getStates.js +++ b/utils/getStates.js @@ -16,13 +16,13 @@ module.exports = async (spinner, table, states, { sortBy, limit, reverse }) => { handleError(`API is down, try again later.`, err, false); let allStates = response.data; + // Limit. + allStates = allStates.slice(0, limit); + // Sort & reverse. const direction = reverse ? 'asc' : 'desc'; allStates = orderBy(allStates, [sortingStateKeys[sortBy]], [direction]); - // Limit - allStates = allStates.slice(0, limit); - // Push selected data. allStates.map((oneState, count) => { table.push([