Skip to content

Commit

Permalink
Adds limit functionality to cli
Browse files Browse the repository at this point in the history
  • Loading branch information
Firas Dib committed Mar 25, 2020
1 parent 2ba122a commit a91faca
Show file tree
Hide file tree
Showing 5 changed files with 31 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
11 changes: 10 additions & 1 deletion utils/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`)}
Expand Down Expand Up @@ -48,6 +52,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 a91faca

Please sign in to comment.