Skip to content

Commit

Permalink
cluster, dns, repl, tls, util: fix RegExp nits
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
vsemozhetbyt authored and addaleax committed Jun 10, 2017
1 parent c4cbd99 commit d51b1c2
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 13 deletions.
2 changes: 1 addition & 1 deletion lib/_tls_wrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -963,7 +963,7 @@ function SNICallback(servername, callback) {
var ctx;

this.server._contexts.some(function(elem) {
if (servername.match(elem[0]) !== null) {
if (elem[0].test(servername)) {
ctx = elem[1];
return true;
}
Expand Down
6 changes: 4 additions & 2 deletions lib/dns.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,21 +310,23 @@ function setServers(servers) {
// servers cares won't have any servers available for resolution
const orig = cares.getServers();
const newSet = [];
const IPv6RE = /\[(.*)\]/;
const addrSplitRE = /:\d+$/;

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

const match = serv.match(/\[(.*)\](?::\d+)?/);
const match = serv.match(IPv6RE);
// we have an IPv6 in brackets
if (match) {
ipVersion = isIP(match[1]);
if (ipVersion !== 0)
return newSet.push([ipVersion, match[1]]);
}

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

if (ipVersion !== 0)
Expand Down
6 changes: 3 additions & 3 deletions lib/internal/cluster/master.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,14 @@ function createWorkerProcess(id, env) {
var workerEnv = util._extend({}, process.env);
var execArgv = cluster.settings.execArgv.slice();
var debugPort = 0;
var debugArgvRE =
/^(--inspect|--inspect-(brk|port)|--debug|--debug-(brk|port))(=\d+)?$/;

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

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

if (match) {
if (debugPort === 0) {
Expand Down
7 changes: 4 additions & 3 deletions lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,7 @@ function complete(line, callback) {
const exts = Object.keys(this.context.require.extensions);
var indexRe = new RegExp('^index(' + exts.map(regexpEscape).join('|') +
')$');
var versionedFileNamesRe = /-\d+\.\d+/;

completeOn = match[1];
var subdir = match[2] || '';
Expand All @@ -772,7 +773,7 @@ function complete(line, callback) {
name = files[f];
ext = path.extname(name);
base = name.slice(0, -ext.length);
if (base.match(/-\d+\.\d+(\.\d+)?/) || name === '.npm') {
if (versionedFileNamesRe.test(base) || name === '.npm') {
// Exclude versioned names that 'npm' installs.
continue;
}
Expand Down Expand Up @@ -816,7 +817,7 @@ function complete(line, callback) {
// spam.eggs.<|> # completions for 'spam.eggs' with filter ''
// foo<|> # all scope vars with filter 'foo'
// foo.<|> # completions for 'foo' with filter ''
} else if (line.length === 0 || line[line.length - 1].match(/\w|\.|\$/)) {
} else if (line.length === 0 || /\w|\.|\$/.test(line[line.length - 1])) {
match = simpleExpressionRE.exec(line);
if (line.length === 0 || match) {
var expr;
Expand Down Expand Up @@ -1067,7 +1068,7 @@ REPLServer.prototype.memory = function memory(cmd) {
self.lines.level.push({
line: self.lines.length - 1,
depth: depth,
isFunction: /\s*function\s*/.test(cmd)
isFunction: /\bfunction\b/.test(cmd)
});
} else if (depth < 0) {
// going... up.
Expand Down
10 changes: 6 additions & 4 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ const inspectDefaultOptions = Object.seal({
breakLength: 60
});

const numbersOnlyRE = /^\d+$/;

var CIRCULAR_ERROR_MESSAGE;
var Debug;

Expand Down Expand Up @@ -663,7 +665,7 @@ function formatArray(ctx, value, recurseTimes, visibleKeys, keys) {
}
for (var n = 0; n < keys.length; n++) {
var key = keys[n];
if (typeof key === 'symbol' || !key.match(/^\d+$/)) {
if (typeof key === 'symbol' || !numbersOnlyRE.test(key)) {
output.push(formatProperty(ctx, value, recurseTimes, visibleKeys,
key, true));
}
Expand All @@ -682,7 +684,7 @@ function formatTypedArray(ctx, value, recurseTimes, visibleKeys, keys) {
output.push(`... ${remaining} more item${remaining > 1 ? 's' : ''}`);
}
for (const key of keys) {
if (typeof key === 'symbol' || !key.match(/^\d+$/)) {
if (typeof key === 'symbol' || !numbersOnlyRE.test(key)) {
output.push(
formatProperty(ctx, value, recurseTimes, visibleKeys, key, true));
}
Expand Down Expand Up @@ -797,11 +799,11 @@ function formatProperty(ctx, value, recurseTimes, visibleKeys, key, array) {
}
}
if (name === undefined) {
if (array && key.match(/^\d+$/)) {
if (array && numbersOnlyRE.test(key)) {
return str;
}
name = JSON.stringify('' + key);
if (name.match(/^"([a-zA-Z_][a-zA-Z_0-9]*)"$/)) {
if (/^"([a-zA-Z_][a-zA-Z_0-9]*)"$/.test(name)) {
name = name.substr(1, name.length - 2);
name = ctx.stylize(name, 'name');
} else {
Expand Down

0 comments on commit d51b1c2

Please sign in to comment.