From 168ff5b97f95374a1205ed9d8b8705215dd8da2c Mon Sep 17 00:00:00 2001 From: Tomoaki Abe Date: Thu, 14 May 2020 17:57:29 +0900 Subject: [PATCH] Replace indexOf with includes (#1264) * Replace indexOf with includes Changed the relevant if statement to an early return. * Modify to "StartsWith" to correspond to "indexOf === 0" --- index.js | 58 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/index.js b/index.js index 987b79f0d..223d5d36e 100644 --- a/index.js +++ b/index.js @@ -20,10 +20,10 @@ class Option { constructor(flags, description) { this.flags = flags; - this.required = flags.indexOf('<') >= 0; // A value must be supplied when the option is specified. - this.optional = flags.indexOf('[') >= 0; // A value is optional when the option is specified. + this.required = flags.includes('<'); // A value must be supplied when the option is specified. + this.optional = flags.includes('['); // A value is optional when the option is specified. this.mandatory = false; // The option must have a value after parsing, which usually means it must be specified on command line. - this.negate = flags.indexOf('-no-') !== -1; + this.negate = flags.includes('-no-'); const flagParts = flags.split(/[ ,|]+/); if (flagParts.length > 1 && !/^[[<]/.test(flagParts[1])) this.short = flagParts.shift(); this.long = flagParts.shift(); @@ -1722,35 +1722,35 @@ function incrementNodeInspectorPort(args) { // --inspect-brk[=[host:]port] // --inspect-port=[host:]port return args.map((arg) => { - let result = arg; - if (arg.indexOf('--inspect') === 0) { - let debugOption; - let debugHost = '127.0.0.1'; - let debugPort = '9229'; - let match; - if ((match = arg.match(/^(--inspect(-brk)?)$/)) !== null) { - // e.g. --inspect - debugOption = match[1]; - } else if ((match = arg.match(/^(--inspect(-brk|-port)?)=([^:]+)$/)) !== null) { - debugOption = match[1]; - if (/^\d+$/.test(match[3])) { - // e.g. --inspect=1234 - debugPort = match[3]; - } else { - // e.g. --inspect=localhost - debugHost = match[3]; - } - } else if ((match = arg.match(/^(--inspect(-brk|-port)?)=([^:]+):(\d+)$/)) !== null) { - // e.g. --inspect=localhost:1234 - debugOption = match[1]; + if (!arg.startsWith('--inspect')) { + return arg; + } + let debugOption; + let debugHost = '127.0.0.1'; + let debugPort = '9229'; + let match; + if ((match = arg.match(/^(--inspect(-brk)?)$/)) !== null) { + // e.g. --inspect + debugOption = match[1]; + } else if ((match = arg.match(/^(--inspect(-brk|-port)?)=([^:]+)$/)) !== null) { + debugOption = match[1]; + if (/^\d+$/.test(match[3])) { + // e.g. --inspect=1234 + debugPort = match[3]; + } else { + // e.g. --inspect=localhost debugHost = match[3]; - debugPort = match[4]; } + } else if ((match = arg.match(/^(--inspect(-brk|-port)?)=([^:]+):(\d+)$/)) !== null) { + // e.g. --inspect=localhost:1234 + debugOption = match[1]; + debugHost = match[3]; + debugPort = match[4]; + } - if (debugOption && debugPort !== '0') { - result = `${debugOption}=${debugHost}:${parseInt(debugPort) + 1}`; - } + if (debugOption && debugPort !== '0') { + return `${debugOption}=${debugHost}:${parseInt(debugPort) + 1}`; } - return result; + return arg; }); }