From babcec69484a9d8df73d3b9dcf8fc511e80ba200 Mon Sep 17 00:00:00 2001 From: Landon Yarrington Date: Sun, 2 May 2021 15:03:33 -0600 Subject: [PATCH] refactor: small simplifications --- lib/cjs.ts | 7 +++---- lib/command.ts | 6 +----- lib/middleware.ts | 2 +- lib/platform-shims/deno.ts | 2 +- lib/typings/common-types.ts | 2 +- lib/utils/maybe-async-result.ts | 10 +++------- lib/validation.ts | 13 ++++--------- 7 files changed, 14 insertions(+), 28 deletions(-) diff --git a/lib/cjs.ts b/lib/cjs.ts index 82dae13e6..cbfbea675 100644 --- a/lib/cjs.ts +++ b/lib/cjs.ts @@ -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) - : 10; +const minNodeVersion = process?.env?.YARGS_MIN_NODE_VERSION + ? Number(process.env.YARGS_MIN_NODE_VERSION) + : 10; if (process && process.version) { const major = Number(process.version.match(/v([^.]+)/)![1]); if (major < minNodeVersion) { diff --git a/lib/command.ts b/lib/command.ts index b1477d24a..bb95561b4 100644 --- a/lib/command.ts +++ b/lib/command.ts @@ -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( diff --git a/lib/middleware.ts b/lib/middleware.ts index 35d35463d..7cf22039d 100644 --- a/lib/middleware.ts +++ b/lib/middleware.ts @@ -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); }); diff --git a/lib/platform-shims/deno.ts b/lib/platform-shims/deno.ts index c2a8dbb1e..0c4436bfe 100644 --- a/lib/platform-shims/deno.ts +++ b/lib/platform-shims/deno.ts @@ -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; diff --git a/lib/typings/common-types.ts b/lib/typings/common-types.ts index 890187831..9b9bcfcda 100644 --- a/lib/typings/common-types.ts +++ b/lib/typings/common-types.ts @@ -54,7 +54,7 @@ export function assertSingleKey( } /** - * Typing wrappefr around Object.keys() + * Typing wrapper around Object.keys() */ export function objectKeys(object: T) { return Object.keys(object) as (keyof T)[]; diff --git a/lib/utils/maybe-async-result.ts b/lib/utils/maybe-async-result.ts index 1e288cb11..9c9efc8df 100644 --- a/lib/utils/maybe-async-result.ts +++ b/lib/utils/maybe-async-result.ts @@ -15,13 +15,9 @@ export function maybeAsyncResult( ): T | Promise { 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); } diff --git a/lib/validation.ts b/lib/validation.ts index b550a2957..4a4e89f8f 100644 --- a/lib/validation.ts +++ b/lib/validation.ts @@ -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