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

NEW: Add omit column feature #46

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 21 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
14 changes: 7 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,23 @@ const options = { sortBy, limit, reverse, minimal };
init(minimal);
const [input] = cli.input;
input === 'help' && (await cli.showHelp(0));
const states = input === 'states' ? true : false;
const states = input === 'states';
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's the same code written simpler.

const country = input;

// Table
const head = xcolor ? single : colored;
const headStates = xcolor ? singleStates : coloredStates;

const border = minimal ? borderless : {};
const table = !states
? new Table({ head, style, chars: border })
: new Table({ head: headStates, style, chars: border });
const tableHead = states ? headStates : head;
const table = new Table({ head: tableHead, style, chars: border });
Comment on lines -48 to +50
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic for table was complex - it started with negation even though there was no real reason for it. As I needed this piece of code I refactored it a little bit for easier maintenance.


// Display data.
spinner.start();
const lastUpdated = await getWorldwide(table, states);
await getCountry(spinner, table, states, country);
await getStates(spinner, table, states, options);
await getCountries(spinner, table, states, country, options);
await getCountry({ spinner, table, states, country, tableHead });
await getStates({ spinner, table, states, options, tableHead });
await getCountries({ spinner, table, states, country, options, tableHead });
Comment on lines -55 to +57
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Due to multiple parameters I refactored getCountry, getStates and getCountries to accept object.


theEnd(lastUpdated, states, minimal);
})();
7 changes: 7 additions & 0 deletions utils/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ module.exports = meow(
${green(`corona`)} ${yellow(`-x`)}
${green(`corona`)} ${yellow(`--sort`)} ${cyan(`cases-today`)}
${green(`corona`)} ${yellow(`-s`)} ${cyan(`critical`)}
${green(`corona`)} ${yellow(`--omit`)} ${cyan(`deaths-today_cases_critical`)}
${green(`corona`)} ${yellow(`--o`)} ${cyan(`country`)}

❯ You can also run command + option at once:
${green(`corona`)} ${cyan(`china`)} ${yellow(`-x`)} ${yellow(`-s cases`)}
Expand Down Expand Up @@ -58,6 +60,11 @@ module.exports = meow(
type: 'boolean',
defualt: false,
alias: 'm'
},
omit: {
type: 'string',
default: '',
alias: 'o'
}
}
}
Expand Down
44 changes: 44 additions & 0 deletions utils/columnsMapper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
module.exports = {
single: {
'#': 0,
country: 1,
cases: 2,
'cases-today': 3,
deaths: 4,
'deaths-today': 5,
recovered: 6,
active: 7,
critical: 8,
'per-milion': 9
},
colored: {
'#': 0,
country: 1,
cases: 2,
'cases-today': 3,
deaths: 4,
'deaths-today': 5,
recovered: 6,
active: 7,
critical: 8,
'per-milion': 9
},
singleStates: {
'#': 0,
state: 1,
cases: 2,
'cases-today': 3,
deaths: 4,
'deaths-today': 5,
active: 6
},
coloredStates: {
'#': 0,
state: 1,
cases: 2,
'cases-today': 3,
deaths: 4,
'deaths-today': 5,
active: 6
}
};
35 changes: 35 additions & 0 deletions utils/deleteColumns.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const mapper = require('./columnsMapper');
const cli = require('./cli');

// returns indices of table rows which user wants to omit form output in descending order
const parseCli = (states, separator = '_') => {
const input = cli.flags.omit;
if (!input || typeof input !== 'string' || input === '') return [];
return input
.split(separator)
.reduce((accumulator, current) => {
if (states) {
return mapper.singleStates[current]
? [...accumulator, mapper.singleStates[current]]
: accumulator;
}
return mapper.single[current]
? [...accumulator, mapper.single[current]]
: accumulator;
}, [])
.sort((a, b) => (a > b ? -1 : 1));
};

const deleteColumns = (table, tableHead, input) => {
input.forEach((key) => {
tableHead.splice(key, 1);
table.forEach((row) => {
row.splice(key, 1);
});
});
};

module.exports = {
parseCli,
deleteColumns
};
11 changes: 8 additions & 3 deletions utils/getCountries.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ const { sortingKeys } = require('./table.js');
const to = require('await-to-js').default;
const handleError = require('cli-handle-error');
const orderBy = require('lodash.orderby');
const { parseCli, deleteColumns } = require('./deleteColumns');

module.exports = async (
module.exports = async ({
spinner,
table,
states,
countryName,
{ sortBy, limit, reverse }
) => {
options: { sortBy, limit, reverse },
tableHead
}) => {
if (!countryName && !states) {
const [err, response] = await to(
axios.get(`https://corona.lmao.ninja/countries`)
Expand Down Expand Up @@ -49,6 +51,9 @@ module.exports = async (
]);
});

const input = parseCli(states);
deleteColumns(table, tableHead, input);

spinner.stopAndPersist();
const isRev = reverse ? `${dim(` & `)}${cyan(`Order`)}: reversed` : ``;
spinner.info(`${cyan(`Sorted by:`)} ${sortBy}${isRev}`);
Expand Down
6 changes: 5 additions & 1 deletion utils/getCountry.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ const comma = require('comma-number');
const red = chalk.red;
const to = require('await-to-js').default;
const handleError = require('cli-handle-error');
const { deleteColumns } = require('./deleteColumns');

module.exports = async (spinner, table, states, countryName) => {
module.exports = async ({ spinner, table, states, countryName, tableHead }) => {
if (countryName && !states) {
const [err, response] = await to(
axios.get(`https://corona.lmao.ninja/countries/${countryName}`)
Expand Down Expand Up @@ -36,6 +37,9 @@ module.exports = async (spinner, table, states, countryName) => {
comma(thisCountry.critical),
comma(thisCountry.casesPerOneMillion)
]);
const input = parseCli(states);
deleteColumns(table, tableHead, input);

spinner.stopAndPersist();
console.log(table.toString());
}
Expand Down
12 changes: 11 additions & 1 deletion utils/getStates.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,15 @@ const { sortingStateKeys } = require('./table.js');
const to = require('await-to-js').default;
const handleError = require('cli-handle-error');
const orderBy = require('lodash.orderby');
const { deleteColumns, parseCli } = require('./deleteColumns');

module.exports = async (spinner, table, states, { sortBy, limit, reverse }) => {
module.exports = async ({
spinner,
table,
states,
options: { sortBy, limit, reverse },
tableHead
}) => {
if (states) {
const [err, response] = await to(
axios.get(`https://corona.lmao.ninja/states`)
Expand Down Expand Up @@ -36,6 +43,9 @@ module.exports = async (spinner, table, states, { sortBy, limit, reverse }) => {
]);
});

const input = parseCli(states);
deleteColumns(table, tableHead, input);

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