Skip to content

Commit c4103b1

Browse files
JacksonTianMyles Borins
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 6d3822c commit c4103b1

7 files changed

Lines changed: 14 additions & 15 deletions

File tree

‎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
@@ -1342,7 +1342,7 @@ Interface.prototype.setBreakpoint = function(script, line,
13421342
}
13431343

13441344
letreq;
1345-
if(/\(\)$/.test(script)){
1345+
if(script.endsWith('()')){
13461346
// setBreakpoint('functionname()');
13471347
req={
13481348
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
returnprocess.platform;
2525
};
2626

27-
consttrailingSlashRe=isWindows ? /[^:]\\$/
28-
: /.\/$/;
29-
3027
exports.tmpdir=function(){
3128
varpath;
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
returnpath;
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
varlines=string.split(lineEnding);

‎lib/repl.js‎

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

Lines changed: 2 additions & 2 deletions
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
functionregexpify(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
}elseif(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)