Skip to content

Commit e644eb3

Browse files
JacksonTianevanlucas
authored andcommitted
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 <benjamingr@gmail.com> Reviewed-By: Brian White <mscdex@mscdex.net>
1 parent e1a012f commit e644eb3

File tree

7 files changed

+14
-15
lines changed

7 files changed

+14
-15
lines changed

lib/_debug_agent.js

Lines changed: 2 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1349,7 +1349,7 @@ Interface.prototype.setBreakpoint = function(script, line,
13491349
}
13501350

13511351
let req;
1352-
if (/\(\)$/.test(script)) {
1352+
if (script.endsWith('()')) {
13531353
// setBreakpoint('functionname()');
13541354
req = {
13551355
type: 'function',

lib/cluster.js

Lines changed: 2 additions & 3 deletions
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

Lines changed: 5 additions & 5 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ function REPLServer(prompt,
446446
self.memory(cmd);
447447

448448
self.wrappedCmd = false;
449-
if (e && !self.bufferedCommand && cmd.trim().match(/^npm /)) {
449+
if (e && !self.bufferedCommand && cmd.trim().startsWith('npm ')) {
450450
self.outputStream.write('npm should be run outside of the ' +
451451
'node repl, in your normal shell.\n' +
452452
'(Press Control-D to exit.)\n');

lib/tls.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ exports.checkServerIdentity = function checkServerIdentity(host, cert) {
7575
// Create regexp to much hostnames
7676
function regexpify(host, wildcards) {
7777
// Add trailing dot (make hostnames uniform)
78-
if (!/\.$/.test(host)) host += '.';
78+
if (!host || !host.endsWith('.')) host += '.';
7979

8080
// The same applies to hostname with more than one wildcard,
8181
// if hostname has wildcard when wildcards are not allowed,
@@ -144,7 +144,7 @@ exports.checkServerIdentity = function checkServerIdentity(host, cert) {
144144
}
145145
} else if (cert.subject) {
146146
// Transform hostname to canonical form
147-
if (!/\.$/.test(host)) host += '.';
147+
if (!host || !host.endsWith('.')) host += '.';
148148

149149
// Otherwise check all DNS/URI records from certificate
150150
// (with allowed wildcards)

0 commit comments

Comments
 (0)