Skip to content

Commit

Permalink
refactor: small simplifications (#1933)
Browse files Browse the repository at this point in the history
Co-authored-by: Benjamin E. Coe <bencoe@google.com>
  • Loading branch information
jly36963 and bcoe committed May 3, 2021
1 parent 0924566 commit 96516e7
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 28 deletions.
7 changes: 3 additions & 4 deletions lib/cjs.ts
Expand Up @@ -13,10 +13,9 @@ import cjsPlatformShim from './platform-shims/cjs.js';

// See https://github.com/yargs/yargs#supported-nodejs-versions for our
// version support policy. The YARGS_MIN_NODE_VERSION is used for testing only.
const minNodeVersion =
process && process.env && process.env.YARGS_MIN_NODE_VERSION
? Number(process.env.YARGS_MIN_NODE_VERSION)
: 12;
const minNodeVersion = process?.env?.YARGS_MIN_NODE_VERSION
? Number(process.env.YARGS_MIN_NODE_VERSION)
: 12;
if (process && process.version) {
const major = Number(process.version.match(/v([^.]+)/)![1]);
if (major < minNodeVersion) {
Expand Down
6 changes: 1 addition & 5 deletions lib/command.ts
Expand Up @@ -750,11 +750,7 @@ interface CommandBuilderCallback {
function isCommandAndAliases(
cmd: DefinitionOrCommandName[]
): cmd is [CommandHandlerDefinition, ...string[]] {
if (cmd.every(c => typeof c === 'string')) {
return true;
} else {
return false;
}
return cmd.every(c => typeof c === 'string');
}

export function isCommandBuilderCallback(
Expand Down
2 changes: 1 addition & 1 deletion lib/middleware.ts
Expand Up @@ -48,7 +48,7 @@ export class GlobalMiddleware {
): YargsInstance {
const aliases = this.yargs.getAliases();
this.globalMiddleware = this.globalMiddleware.filter(m => {
const toCheck = [...(aliases[option] ? aliases[option] : []), option];
const toCheck = [...(aliases[option] || []), option];
if (!m.option) return true;
else return !toCheck.includes(m.option);
});
Expand Down
2 changes: 1 addition & 1 deletion lib/platform-shims/deno.ts
Expand Up @@ -21,7 +21,7 @@ const REQUIRE_ERROR = 'require is not supported by ESM';
const REQUIRE_DIRECTORY_ERROR =
'loading a directory of commands is not supported yet for ESM';

// Deno removes argv[0] and argv[1 from Deno.args:
// Deno removes argv[0] and argv[1] from Deno.args:
const argv = ['deno run', ...Deno.args];
const __dirname = new URL('.', import.meta.url).pathname;

Expand Down
2 changes: 1 addition & 1 deletion lib/typings/common-types.ts
Expand Up @@ -54,7 +54,7 @@ export function assertSingleKey(
}

/**
* Typing wrappefr around Object.keys()
* Typing wrapper around Object.keys()
*/
export function objectKeys<T>(object: T) {
return Object.keys(object) as (keyof T)[];
Expand Down
10 changes: 3 additions & 7 deletions lib/utils/maybe-async-result.ts
Expand Up @@ -15,13 +15,9 @@ export function maybeAsyncResult<T>(
): T | Promise<T> {
try {
const result = isFunction(getResult) ? getResult() : getResult;
if (isPromise(result)) {
return result.then((result: T) => {
return resultHandler(result);
});
} else {
return resultHandler(result);
}
return isPromise(result)
? result.then((result: T) => resultHandler(result))
: resultHandler(result);
} catch (err) {
return errorHandler(err);
}
Expand Down
13 changes: 4 additions & 9 deletions lib/validation.ts
Expand Up @@ -232,15 +232,10 @@ export function validation(
return false;
}
const newAliases = (yargs.parsed as DetailedArguments).newAliases;
for (const a of [key, ...aliases[key]]) {
if (
!Object.prototype.hasOwnProperty.call(newAliases, a) ||
!newAliases[key]
) {
return true;
}
}
return false;
return [key, ...aliases[key]].some(
a =>
!Object.prototype.hasOwnProperty.call(newAliases, a) || !newAliases[key]
);
};

// validate arguments limited to enumerated choices
Expand Down

0 comments on commit 96516e7

Please sign in to comment.