Skip to content

Commit

Permalink
📦 NEW: limit functionality to cli (#17)
Browse files Browse the repository at this point in the history
Co-authored-by: Firas Dib <firas@consid.se>
  • Loading branch information
firasdib and Firas Dib authored Mar 25, 2020
1 parent 2ba122a commit 5b97523
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 12 deletions.
7 changes: 5 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);
})();
8 changes: 8 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion utils/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module.exports = meow(
${yellow(`--xcolor`)}, ${yellow(`-x`)} Single colored output
${yellow(`--sort`)}, ${yellow(`-s`)} Sort data by type
${yellow(`--reverse`)}, ${yellow(`-r`)} Reverse print order
${yellow(`--limit`)}, ${yellow(`-n`)} Limit output to N entries
Examples
${green(`corona`)} ${cyan(`china`)}
Expand Down Expand Up @@ -48,6 +49,11 @@ module.exports = meow(
default: false,
alias: 'r',
},
},
limit: {
type: 'number',
default: Number.MAX_SAFE_INTEGER,
alias: 'n'
}
}
}
);
12 changes: 4 additions & 8 deletions utils/getCountries.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
Expand All @@ -31,6 +24,9 @@ module.exports = async (
[direction]
);

// Limit
allCountries = allCountries.slice(0, limit);

// Push selected data.
allCountries.map((oneCountry, count) => {
table.push([
Expand Down
5 changes: 4 additions & 1 deletion utils/getStates.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
Expand All @@ -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([
Expand Down

0 comments on commit 5b97523

Please sign in to comment.