diff --git a/index.js b/index.js index 3c4b4db..054fa41 100755 --- a/index.js +++ b/index.js @@ -28,6 +28,9 @@ const { 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 () => { // Init. @@ -48,8 +51,8 @@ const reverse = cli.flags.reverse; spinner.start(); const lastUpdated = await getWorldwide(table, states); await getCountry(spinner, table, states, country); - await getStates(spinner, table, states, sortBy, reverse); - await getCountries(spinner, table, states, country, sortBy, reverse); + await getStates(spinner, table, states, options); + await getCountries(spinner, table, states, country, options); theEnd(lastUpdated, states); })(); diff --git a/readme.md b/readme.md index 1d0f193..261b15b 100644 --- a/readme.md +++ b/readme.md @@ -113,6 +113,14 @@ 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. + +````sh +corona --sort cases -n 10 +```` + #### CLI Help ```sh diff --git a/utils/cli.js b/utils/cli.js index 289f520..ac3ed8b 100644 --- a/utils/cli.js +++ b/utils/cli.js @@ -16,7 +16,11 @@ module.exports = meow( Options ${yellow(`--xcolor`)}, ${yellow(`-x`)} Single colored output ${yellow(`--sort`)}, ${yellow(`-s`)} Sort data by type +<<<<<<< HEAD ${yellow(`--reverse`)}, ${yellow(`-r`)} Reverse print order +======= + ${yellow(`--limit`)}, ${yellow(`-n`)} Limit output to N entries +>>>>>>> Adds limit functionality to cli Examples ${green(`corona`)} ${cyan(`china`)} @@ -48,6 +52,11 @@ module.exports = meow( default: false, alias: 'r', }, - }, + limit: { + type: 'number', + default: Number.MAX_SAFE_INTEGER, + alias: 'n' + } + } } ); diff --git a/utils/getCountries.js b/utils/getCountries.js index d4bcf81..5359c50 100644 --- a/utils/getCountries.js +++ b/utils/getCountries.js @@ -8,14 +8,7 @@ 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, - 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`) @@ -31,6 +24,9 @@ module.exports = async ( [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 e79a209..d66a644 100644 --- a/utils/getStates.js +++ b/utils/getStates.js @@ -8,7 +8,7 @@ const to = require('await-to-js').default; const handleError = require('cli-handle-error'); const orderBy = require('lodash.orderby'); -module.exports = async (spinner, table, states, sortBy, reverse) => { +module.exports = async (spinner, table, states, { sortBy, limit, reverse }) => { if (states) { const [err, response] = await to( axios.get(`https://corona.lmao.ninja/states`) @@ -20,6 +20,9 @@ module.exports = async (spinner, table, states, sortBy, 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([