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 22, 2020
1 parent 5e54736 commit 3665af6
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 3 deletions.
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const {
} = require("./utils/table.js");
const xcolor = cli.flags.xcolor;
const sortBy = cli.flags.sort;
const limit = cli.flags.limit;

(async () => {
// Init.
Expand All @@ -48,8 +49,8 @@ const sortBy = cli.flags.sort;
spinner.start();
await getWorldwide(table, states);
await getCountry(spinner, table, states, country);
await getStates(spinner, table, states, { sort: sortBy });
await getAll(spinner, table, states, country, { sort: sortBy });
await getStates(spinner, table, states, { sort: sortBy, limit });
await getAll(spinner, table, states, country, { sort: sortBy, limit });

theEnd(states);
})();
8 changes: 8 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,14 @@ corona -s per-million

[![📟](./.github/sort.gif)](./../../)

### Limit output

Only output the top N results (depending on your sort criteria)

````sh
corona --sort cases -n 10
````

#### CLI Help

```sh
Expand Down
6 changes: 6 additions & 0 deletions utils/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module.exports = meow(
Options
${yellow(`--xcolor`)}, ${yellow(`-x`)} Single colored output
${yellow(`--sort`)}, ${yellow(`-s`)} Sort data by type
${yellow(`--limit`)}, ${yellow(`-n`)} Limit output to N entries
Examples
${green(`corona`)} ${cyan(`china`)}
Expand All @@ -41,6 +42,11 @@ module.exports = meow(
type: 'string',
default: 'cases-today',
alias: 's'
},
limit: {
type: 'number',
default: Number.MAX_SAFE_INTEGER,
alias: 'n'
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion utils/getAll.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const { sortKeys, sortOrders } = require("./table.js");
module.exports = async (spinner, table, states, country, options) => {
if (!country && !states) {
const api = await axios.get(`https://corona.lmao.ninja/countries`);
let all = api.data.map(one => Object.values(one));
let all = api.data.map(one => Object.values(one)).slice(0, options.limit);

const sortIndex = sortKeys.indexOf(options.sort);

Expand All @@ -15,6 +15,8 @@ module.exports = async (spinner, table, states, country, options) => {
all = all.sort((a, b) => (a[sortIndex] > b[sortIndex] ? dir : -dir));
}

all = all.slice(0, options.limit);

all.map(one => {
one = one.map(d => comma(d));
return table.push(one);
Expand Down
2 changes: 2 additions & 0 deletions utils/getStates.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ module.exports = async (spinner, table, states, options) => {
all = all.sort((a, b) => (a[sortIndex] > b[sortIndex] ? dir : -dir));
}

all = all.slice(0, options.limit);

all.map(one => {
one = one.map(d => comma(d));
return table.push(one);
Expand Down

0 comments on commit 3665af6

Please sign in to comment.