Skip to content

Commit

Permalink
馃憣 IMPROVE: Limit
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmadawais committed Mar 25, 2020
1 parent 5b97523 commit 4bc3f58
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 18 deletions.
5 changes: 2 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// Makes the script crash on unhandled rejections instead of silently
// ignoring them. In the future, promise rejections that are not handled will
// terminate the Node.js process with a non-zero exit code.
process.on('unhandledRejection', (err) => {
process.on('unhandledRejection', err => {
handleError(`UNHANDLED ERROR`, err);
});

Expand All @@ -23,13 +23,12 @@ const {
colored,
singleStates,
coloredStates,
style,
style
} = require('./utils/table.js');
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 () => {
Expand Down
7 changes: 3 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,11 @@ 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.
### Limit the output

````sh
corona --sort cases -n 10
# Print a limited number of entries to the output.
corona --limit 10
````

#### CLI Help
Expand Down
8 changes: 4 additions & 4 deletions utils/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,22 @@ module.exports = meow(
xcolor: {
type: 'boolean',
default: false,
alias: 'x',
alias: 'x'
},
sort: {
type: 'string',
default: 'cases',
alias: 's',
alias: 's'
},
reverse: {
type: 'boolean',
default: false,
alias: 'r',
alias: 'r'
},
limit: {
type: 'number',
default: Number.MAX_SAFE_INTEGER,
alias: 'n'
alias: 'l'
}
}
}
Expand Down
14 changes: 10 additions & 4 deletions utils/getCountries.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,23 @@ 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, limit, 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`)
);
handleError(`API is down, try again later.`, err, false);
let allCountries = response.data;

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

// Sort & reverse.
const direction = reverse ? 'asc' : 'desc';
allCountries = orderBy(
Expand All @@ -24,9 +33,6 @@ module.exports = async (spinner, table, states, countryName, { sortBy, limit, re
[direction]
);

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

// Push selected data.
allCountries.map((oneCountry, count) => {
table.push([
Expand Down
6 changes: 3 additions & 3 deletions utils/getStates.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ module.exports = async (spinner, table, states, { sortBy, limit, reverse }) => {
handleError(`API is down, try again later.`, err, false);
let allStates = response.data;

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

// Sort & 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 4bc3f58

Please sign in to comment.