Skip to content

Commit ddc8050

Browse files
vsemozhetbytMylesBorins
authored andcommitted
cluster, dns, repl, tls, util: fix RegExp nits
* Take RegExp creation out of cycles. * Use test(), not match() in boolean context. * Remove redundant RegExp parts. Backport-PR-URL: #14348 PR-URL: #13536 Reviewed-By: Anna Henningsen <[email protected]>
1 parent 3b2c791 commit ddc8050

File tree

5 files changed

+17
-13
lines changed

5 files changed

+17
-13
lines changed

lib/_tls_wrap.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -954,7 +954,7 @@ function SNICallback(servername, callback) {
954954
var ctx;
955955

956956
this.server._contexts.some(function(elem) {
957-
if (servername.match(elem[0]) !== null) {
957+
if (elem[0].test(servername)) {
958958
ctx = elem[1];
959959
return true;
960960
}

lib/cluster.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -298,14 +298,13 @@ function masterInit() {
298298
var workerEnv = util._extend({}, process.env);
299299
var execArgv = cluster.settings.execArgv.slice();
300300
var debugPort = 0;
301+
var debugArgvRE = /^(--inspect|--debug|--debug-(brk|port))(=\d+)?$/;
301302

302303
workerEnv = util._extend(workerEnv, env);
303304
workerEnv.NODE_UNIQUE_ID = '' + id;
304305

305306
for (var i = 0; i < execArgv.length; i++) {
306-
var match = execArgv[i].match(
307-
/^(--inspect|--debug|--debug-(brk|port))(=\d+)?$/
308-
);
307+
var match = execArgv[i].match(debugArgvRE);
309308

310309
if (match) {
311310
if (debugPort === 0) {

lib/dns.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -297,21 +297,23 @@ exports.setServers = function(servers) {
297297
// servers cares won't have any servers available for resolution
298298
const orig = cares.getServers();
299299
const newSet = [];
300+
const IPv6RE = /\[(.*)\]/;
301+
const addrSplitRE = /:\d+$/;
300302

301303
servers.forEach((serv) => {
302304
var ipVersion = isIP(serv);
303305
if (ipVersion !== 0)
304306
return newSet.push([ipVersion, serv]);
305307

306-
const match = serv.match(/\[(.*)\](?::\d+)?/);
308+
const match = serv.match(IPv6RE);
307309
// we have an IPv6 in brackets
308310
if (match) {
309311
ipVersion = isIP(match[1]);
310312
if (ipVersion !== 0)
311313
return newSet.push([ipVersion, match[1]]);
312314
}
313315

314-
const s = serv.split(/:\d+$/)[0];
316+
const s = serv.split(addrSplitRE)[0];
315317
ipVersion = isIP(s);
316318

317319
if (ipVersion !== 0)

lib/repl.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -861,6 +861,7 @@ function complete(line, callback) {
861861
const exts = Object.keys(this.context.require.extensions);
862862
var indexRe = new RegExp('^index(' + exts.map(regexpEscape).join('|') +
863863
')$');
864+
var versionedFileNamesRe = /-\d+\.\d+/;
864865

865866
completeOn = match[1];
866867
var subdir = match[2] || '';
@@ -879,7 +880,7 @@ function complete(line, callback) {
879880
name = files[f];
880881
ext = path.extname(name);
881882
base = name.slice(0, -ext.length);
882-
if (base.match(/-\d+\.\d+(\.\d+)?/) || name === '.npm') {
883+
if (versionedFileNamesRe.test(base) || name === '.npm') {
883884
// Exclude versioned names that 'npm' installs.
884885
continue;
885886
}
@@ -923,7 +924,7 @@ function complete(line, callback) {
923924
// spam.eggs.<|> # completions for 'spam.eggs' with filter ''
924925
// foo<|> # all scope vars with filter 'foo'
925926
// foo.<|> # completions for 'foo' with filter ''
926-
} else if (line.length === 0 || line[line.length - 1].match(/\w|\.|\$/)) {
927+
} else if (line.length === 0 || /\w|\.|\$/.test(line[line.length - 1])) {
927928
match = simpleExpressionRE.exec(line);
928929
if (line.length === 0 || match) {
929930
var expr;
@@ -1175,7 +1176,7 @@ REPLServer.prototype.memory = function memory(cmd) {
11751176
self.lines.level.push({
11761177
line: self.lines.length - 1,
11771178
depth: depth,
1178-
isFunction: /\s*function\s*/.test(cmd)
1179+
isFunction: /\bfunction\b/.test(cmd)
11791180
});
11801181
} else if (depth < 0) {
11811182
// going... up.

lib/util.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ const inspectDefaultOptions = Object.seal({
1717
breakLength: 60
1818
});
1919

20+
const numbersOnlyRE = /^\d+$/;
21+
2022
var Debug;
2123
var simdFormatters;
2224

@@ -668,7 +670,7 @@ function formatArray(ctx, value, recurseTimes, visibleKeys, keys) {
668670
output.push(`... ${remaining} more item${remaining > 1 ? 's' : ''}`);
669671
}
670672
keys.forEach(function(key) {
671-
if (typeof key === 'symbol' || !key.match(/^\d+$/)) {
673+
if (typeof key === 'symbol' || !numbersOnlyRE.test(key)) {
672674
output.push(formatProperty(ctx, value, recurseTimes, visibleKeys,
673675
key, true));
674676
}
@@ -687,7 +689,7 @@ function formatTypedArray(ctx, value, recurseTimes, visibleKeys, keys) {
687689
output.push(`... ${remaining} more item${remaining > 1 ? 's' : ''}`);
688690
}
689691
for (const key of keys) {
690-
if (typeof key === 'symbol' || !key.match(/^\d+$/)) {
692+
if (typeof key === 'symbol' || !numbersOnlyRE.test(key)) {
691693
output.push(
692694
formatProperty(ctx, value, recurseTimes, visibleKeys, key, true));
693695
}
@@ -801,11 +803,11 @@ function formatProperty(ctx, value, recurseTimes, visibleKeys, key, array) {
801803
}
802804
}
803805
if (name === undefined) {
804-
if (array && key.match(/^\d+$/)) {
806+
if (array && numbersOnlyRE.test(key)) {
805807
return str;
806808
}
807809
name = JSON.stringify('' + key);
808-
if (name.match(/^"([a-zA-Z_][a-zA-Z_0-9]*)"$/)) {
810+
if (/^"([a-zA-Z_][a-zA-Z_0-9]*)"$/.test(name)) {
809811
name = name.substr(1, name.length - 2);
810812
name = ctx.stylize(name, 'name');
811813
} else {

0 commit comments

Comments
 (0)