Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃摝 NEW: limit functionality to cli #17

Merged
merged 1 commit into from
Mar 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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