Skip to content

Commit c4103b1

Browse files
JacksonTianMyles Borins
authored and
Myles Borins
committed
lib: refactor code with startsWith/endsWith
reduce using RegExp for string test. This pull reuqest replaces various usages of regular expressions in favor of the ES2015 startsWith and endsWith methods. PR-URL: #5753 Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Brian White <[email protected]>
1 parent 6d3822c commit c4103b1

File tree

7 files changed

+14
-15
lines changed

7 files changed

+14
-15
lines changed

lib/_debug_agent.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,10 @@ Client.prototype._transform = function _transform(data, enc, cb) {
119119
while (true) {
120120
if (this.state === 'headers') {
121121
// Not enough data
122-
if (!/\r\n/.test(this.buffer))
122+
if (!this.buffer.includes('\r\n'))
123123
break;
124124

125-
if (/^\r\n/.test(this.buffer)) {
125+
if (this.buffer.startsWith('\r\n')) {
126126
this.buffer = this.buffer.slice(2);
127127
this.state = 'body';
128128
continue;

lib/_debugger.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1342,7 +1342,7 @@ Interface.prototype.setBreakpoint = function(script, line,
13421342
}
13431343

13441344
let req;
1345-
if (/\(\)$/.test(script)) {
1345+
if (script.endsWith('()')) {
13461346
// setBreakpoint('functionname()');
13471347
req = {
13481348
type: 'function',

lib/cluster.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -237,9 +237,8 @@ function masterInit() {
237237
// Without --logfile=v8-%p.log, everything ends up in a single, unusable
238238
// file. (Unusable because what V8 logs are memory addresses and each
239239
// process has its own memory mappings.)
240-
if (settings.execArgv.some(function(s) { return /^--prof/.test(s); }) &&
241-
!settings.execArgv.some(function(s) { return /^--logfile=/.test(s); }))
242-
{
240+
if (settings.execArgv.some((s) => s.startsWith('--prof')) &&
241+
!settings.execArgv.some((s) => s.startsWith('--logfile='))) {
243242
settings.execArgv = settings.execArgv.concat(['--logfile=v8-%p.log']);
244243
}
245244
cluster.settings = settings;

lib/os.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,23 @@ exports.platform = function() {
2424
return process.platform;
2525
};
2626

27-
const trailingSlashRe = isWindows ? /[^:]\\$/
28-
: /.\/$/;
29-
3027
exports.tmpdir = function() {
3128
var path;
3229
if (isWindows) {
3330
path = process.env.TEMP ||
3431
process.env.TMP ||
3532
(process.env.SystemRoot || process.env.windir) + '\\temp';
33+
if (path.length > 1 && path.endsWith('\\') && !path.endsWith(':\\'))
34+
path = path.slice(0, -1);
3635
} else {
3736
path = process.env.TMPDIR ||
3837
process.env.TMP ||
3938
process.env.TEMP ||
4039
'/tmp';
40+
if (path.length > 1 && path.endsWith('/'))
41+
path = path.slice(0, -1);
4142
}
42-
if (trailingSlashRe.test(path))
43-
path = path.slice(0, -1);
43+
4444
return path;
4545
};
4646

lib/readline.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ Interface.prototype._normalWrite = function(b) {
330330
this._line_buffer = null;
331331
}
332332
if (newPartContainsEnding) {
333-
this._sawReturn = /\r$/.test(string);
333+
this._sawReturn = string.endsWith('\r');
334334

335335
// got one or more newlines; process into "line" events
336336
var lines = string.split(lineEnding);

lib/repl.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ function REPLServer(prompt,
437437
debug('finish', e, ret);
438438
self.memory(cmd);
439439

440-
if (e && !self.bufferedCommand && cmd.trim().match(/^npm /)) {
440+
if (e && !self.bufferedCommand && cmd.trim().startsWith('npm ')) {
441441
self.outputStream.write('npm should be run outside of the ' +
442442
'node repl, in your normal shell.\n' +
443443
'(Press Control-D to exit.)\n');

lib/tls.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ exports.checkServerIdentity = function checkServerIdentity(host, cert) {
6060
// Create regexp to much hostnames
6161
function regexpify(host, wildcards) {
6262
// Add trailing dot (make hostnames uniform)
63-
if (!/\.$/.test(host)) host += '.';
63+
if (!host || !host.endsWith('.')) host += '.';
6464

6565
// The same applies to hostname with more than one wildcard,
6666
// if hostname has wildcard when wildcards are not allowed,
@@ -129,7 +129,7 @@ exports.checkServerIdentity = function checkServerIdentity(host, cert) {
129129
}
130130
} else if (cert.subject) {
131131
// Transform hostname to canonical form
132-
if (!/\.$/.test(host)) host += '.';
132+
if (!host || !host.endsWith('.')) host += '.';
133133

134134
// Otherwise check all DNS/URI records from certificate
135135
// (with allowed wildcards)

0 commit comments

Comments
 (0)