Skip to content

Commit d51b1c2

Browse files
vsemozhetbytaddaleax
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. PR-URL: #13536 Reviewed-By: Anna Henningsen <[email protected]>
1 parent c4cbd99 commit d51b1c2

File tree

5 files changed

+18
-13
lines changed

5 files changed

+18
-13
lines changed

lib/_tls_wrap.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -963,7 +963,7 @@ function SNICallback(servername, callback) {
963963
var ctx;
964964

965965
this.server._contexts.some(function(elem) {
966-
if (servername.match(elem[0]) !== null) {
966+
if (elem[0].test(servername)) {
967967
ctx = elem[1];
968968
return true;
969969
}

lib/dns.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -310,21 +310,23 @@ function setServers(servers) {
310310
// servers cares won't have any servers available for resolution
311311
const orig = cares.getServers();
312312
const newSet = [];
313+
const IPv6RE = /\[(.*)\]/;
314+
const addrSplitRE = /:\d+$/;
313315

314316
servers.forEach((serv) => {
315317
var ipVersion = isIP(serv);
316318
if (ipVersion !== 0)
317319
return newSet.push([ipVersion, serv]);
318320

319-
const match = serv.match(/\[(.*)\](?::\d+)?/);
321+
const match = serv.match(IPv6RE);
320322
// we have an IPv6 in brackets
321323
if (match) {
322324
ipVersion = isIP(match[1]);
323325
if (ipVersion !== 0)
324326
return newSet.push([ipVersion, match[1]]);
325327
}
326328

327-
const s = serv.split(/:\d+$/)[0];
329+
const s = serv.split(addrSplitRE)[0];
328330
ipVersion = isIP(s);
329331

330332
if (ipVersion !== 0)

lib/internal/cluster/master.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,14 @@ function createWorkerProcess(id, env) {
9999
var workerEnv = util._extend({}, process.env);
100100
var execArgv = cluster.settings.execArgv.slice();
101101
var debugPort = 0;
102+
var debugArgvRE =
103+
/^(--inspect|--inspect-(brk|port)|--debug|--debug-(brk|port))(=\d+)?$/;
102104

103105
util._extend(workerEnv, env);
104106
workerEnv.NODE_UNIQUE_ID = '' + id;
105107

106108
for (var i = 0; i < execArgv.length; i++) {
107-
const match = execArgv[i].match(
108-
/^(--inspect|--inspect-(brk|port)|--debug|--debug-(brk|port))(=\d+)?$/
109-
);
109+
const match = execArgv[i].match(debugArgvRE);
110110

111111
if (match) {
112112
if (debugPort === 0) {

lib/repl.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,7 @@ function complete(line, callback) {
754754
const exts = Object.keys(this.context.require.extensions);
755755
var indexRe = new RegExp('^index(' + exts.map(regexpEscape).join('|') +
756756
')$');
757+
var versionedFileNamesRe = /-\d+\.\d+/;
757758

758759
completeOn = match[1];
759760
var subdir = match[2] || '';
@@ -772,7 +773,7 @@ function complete(line, callback) {
772773
name = files[f];
773774
ext = path.extname(name);
774775
base = name.slice(0, -ext.length);
775-
if (base.match(/-\d+\.\d+(\.\d+)?/) || name === '.npm') {
776+
if (versionedFileNamesRe.test(base) || name === '.npm') {
776777
// Exclude versioned names that 'npm' installs.
777778
continue;
778779
}
@@ -816,7 +817,7 @@ function complete(line, callback) {
816817
// spam.eggs.<|> # completions for 'spam.eggs' with filter ''
817818
// foo<|> # all scope vars with filter 'foo'
818819
// foo.<|> # completions for 'foo' with filter ''
819-
} else if (line.length === 0 || line[line.length - 1].match(/\w|\.|\$/)) {
820+
} else if (line.length === 0 || /\w|\.|\$/.test(line[line.length - 1])) {
820821
match = simpleExpressionRE.exec(line);
821822
if (line.length === 0 || match) {
822823
var expr;
@@ -1067,7 +1068,7 @@ REPLServer.prototype.memory = function memory(cmd) {
10671068
self.lines.level.push({
10681069
line: self.lines.length - 1,
10691070
depth: depth,
1070-
isFunction: /\s*function\s*/.test(cmd)
1071+
isFunction: /\bfunction\b/.test(cmd)
10711072
});
10721073
} else if (depth < 0) {
10731074
// going... up.

lib/util.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ const inspectDefaultOptions = Object.seal({
3838
breakLength: 60
3939
});
4040

41+
const numbersOnlyRE = /^\d+$/;
42+
4143
var CIRCULAR_ERROR_MESSAGE;
4244
var Debug;
4345

@@ -663,7 +665,7 @@ function formatArray(ctx, value, recurseTimes, visibleKeys, keys) {
663665
}
664666
for (var n = 0; n < keys.length; n++) {
665667
var key = keys[n];
666-
if (typeof key === 'symbol' || !key.match(/^\d+$/)) {
668+
if (typeof key === 'symbol' || !numbersOnlyRE.test(key)) {
667669
output.push(formatProperty(ctx, value, recurseTimes, visibleKeys,
668670
key, true));
669671
}
@@ -682,7 +684,7 @@ function formatTypedArray(ctx, value, recurseTimes, visibleKeys, keys) {
682684
output.push(`... ${remaining} more item${remaining > 1 ? 's' : ''}`);
683685
}
684686
for (const key of keys) {
685-
if (typeof key === 'symbol' || !key.match(/^\d+$/)) {
687+
if (typeof key === 'symbol' || !numbersOnlyRE.test(key)) {
686688
output.push(
687689
formatProperty(ctx, value, recurseTimes, visibleKeys, key, true));
688690
}
@@ -797,11 +799,11 @@ function formatProperty(ctx, value, recurseTimes, visibleKeys, key, array) {
797799
}
798800
}
799801
if (name === undefined) {
800-
if (array && key.match(/^\d+$/)) {
802+
if (array && numbersOnlyRE.test(key)) {
801803
return str;
802804
}
803805
name = JSON.stringify('' + key);
804-
if (name.match(/^"([a-zA-Z_][a-zA-Z_0-9]*)"$/)) {
806+
if (/^"([a-zA-Z_][a-zA-Z_0-9]*)"$/.test(name)) {
805807
name = name.substr(1, name.length - 2);
806808
name = ctx.stylize(name, 'name');
807809
} else {

0 commit comments

Comments
 (0)