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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional stateName parameter to filter states results #81

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,14 @@ const {
// Cli.
const [input] = cli.input;
const xcolor = cli.flags.xcolor;
const stateName = cli.flags.stateName;
const sortBy = cli.flags.sort;
const reverse = cli.flags.reverse;
const limit = Math.abs(cli.flags.limit);
const chart = cli.flags.chart;
const log = cli.flags.log;
const minimal = cli.flags.minimal;
const options = { sortBy, limit, reverse, minimal, chart, log };
const options = { stateName, sortBy, limit, reverse, minimal, chart, log };

(async () => {
// Init.
Expand Down
5 changes: 5 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ corona usa
# Display data for all the US states.
corona states

# Display data for single US state.
corona states --stateName Tennessee
or
corona states --stateName "New York"

# Display states data sorted by active cases.
corona states --sort active

Expand Down
37 changes: 26 additions & 11 deletions 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, limit, reverse }) => {
module.exports = async (spinner, table, states, { stateName, sortBy, limit, reverse }) => {
if (states) {
const [err, response] = await to(
axios.get(`https://corona.lmao.ninja/states`)
Expand All @@ -24,17 +24,32 @@ module.exports = async (spinner, table, states, { sortBy, limit, reverse }) => {
allStates = orderBy(allStates, [sortingStateKeys[sortBy]], [direction]);

// Push selected data.
allStates.map((oneState, count) => {
table.push([
count + 1,
oneState.state,
comma(oneState.cases),
comma(oneState.todayCases),
comma(oneState.deaths),
comma(oneState.todayDeaths),
comma(oneState.active)
]);
if (stateName) {
allStates.map((oneState, count) => {
if (oneState.state === stateName)
table.push([
'-',
oneState.state,
comma(oneState.cases),
comma(oneState.todayCases),
comma(oneState.deaths),
comma(oneState.todayDeaths),
comma(oneState.active)
]);
});
}
else { allStates.map((oneState, count) => {
table.push([
count + 1,
oneState.state,
comma(oneState.cases),
comma(oneState.todayCases),
comma(oneState.deaths),
comma(oneState.todayDeaths),
comma(oneState.active)
]);
});
};

spinner.stopAndPersist();
const isRev = reverse ? `${dim(` & `)}${cyan(`Order`)}: reversed` : ``;
Expand Down