From a7710b0092e9f5d2a6303516759acbaea336fc62 Mon Sep 17 00:00:00 2001 From: Jackson Tian Date: Thu, 17 Mar 2016 12:23:52 +0800 Subject: [PATCH] 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: https://github.com/nodejs/node/pull/5753 Reviewed-By: Benjamin Gruenbaum Reviewed-By: Brian White --- lib/_debug_agent.js | 4 ++-- lib/_debugger.js | 2 +- lib/cluster.js | 5 ++--- lib/os.js | 10 +++++----- lib/readline.js | 2 +- lib/repl.js | 2 +- lib/tls.js | 4 ++-- 7 files changed, 14 insertions(+), 15 deletions(-) diff --git a/lib/_debug_agent.js b/lib/_debug_agent.js index 152a7c65a63575..cb4684f0eddfac 100644 --- a/lib/_debug_agent.js +++ b/lib/_debug_agent.js @@ -119,10 +119,10 @@ Client.prototype._transform = function _transform(data, enc, cb) { while (true) { if (this.state === 'headers') { // Not enough data - if (!/\r\n/.test(this.buffer)) + if (!this.buffer.includes('\r\n')) break; - if (/^\r\n/.test(this.buffer)) { + if (this.buffer.startsWith('\r\n')) { this.buffer = this.buffer.slice(2); this.state = 'body'; continue; diff --git a/lib/_debugger.js b/lib/_debugger.js index fbdcf3be2df7e0..8a72121f60176a 100644 --- a/lib/_debugger.js +++ b/lib/_debugger.js @@ -1349,7 +1349,7 @@ Interface.prototype.setBreakpoint = function(script, line, } let req; - if (/\(\)$/.test(script)) { + if (script.endsWith('()')) { // setBreakpoint('functionname()'); req = { type: 'function', diff --git a/lib/cluster.js b/lib/cluster.js index 4f7ca58170468f..e4bbd5f3ff7838 100644 --- a/lib/cluster.js +++ b/lib/cluster.js @@ -237,9 +237,8 @@ function masterInit() { // Without --logfile=v8-%p.log, everything ends up in a single, unusable // file. (Unusable because what V8 logs are memory addresses and each // process has its own memory mappings.) - if (settings.execArgv.some(function(s) { return /^--prof/.test(s); }) && - !settings.execArgv.some(function(s) { return /^--logfile=/.test(s); })) - { + if (settings.execArgv.some((s) => s.startsWith('--prof')) && + !settings.execArgv.some((s) => s.startsWith('--logfile='))) { settings.execArgv = settings.execArgv.concat(['--logfile=v8-%p.log']); } cluster.settings = settings; diff --git a/lib/os.js b/lib/os.js index ddf7cee9d48791..42ece99a7d266c 100644 --- a/lib/os.js +++ b/lib/os.js @@ -24,23 +24,23 @@ exports.platform = function() { return process.platform; }; -const trailingSlashRe = isWindows ? /[^:]\\$/ - : /.\/$/; - exports.tmpdir = function() { var path; if (isWindows) { path = process.env.TEMP || process.env.TMP || (process.env.SystemRoot || process.env.windir) + '\\temp'; + if (path.length > 1 && path.endsWith('\\') && !path.endsWith(':\\')) + path = path.slice(0, -1); } else { path = process.env.TMPDIR || process.env.TMP || process.env.TEMP || '/tmp'; + if (path.length > 1 && path.endsWith('/')) + path = path.slice(0, -1); } - if (trailingSlashRe.test(path)) - path = path.slice(0, -1); + return path; }; diff --git a/lib/readline.js b/lib/readline.js index 8ee10850e54f89..74656896314531 100644 --- a/lib/readline.js +++ b/lib/readline.js @@ -330,7 +330,7 @@ Interface.prototype._normalWrite = function(b) { this._line_buffer = null; } if (newPartContainsEnding) { - this._sawReturn = /\r$/.test(string); + this._sawReturn = string.endsWith('\r'); // got one or more newlines; process into "line" events var lines = string.split(lineEnding); diff --git a/lib/repl.js b/lib/repl.js index 7b3b7d11739add..120ebcc34d0fc8 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -446,7 +446,7 @@ function REPLServer(prompt, self.memory(cmd); self.wrappedCmd = false; - if (e && !self.bufferedCommand && cmd.trim().match(/^npm /)) { + if (e && !self.bufferedCommand && cmd.trim().startsWith('npm ')) { self.outputStream.write('npm should be run outside of the ' + 'node repl, in your normal shell.\n' + '(Press Control-D to exit.)\n'); diff --git a/lib/tls.js b/lib/tls.js index 245afb6bdc7561..b7309a62deff09 100644 --- a/lib/tls.js +++ b/lib/tls.js @@ -75,7 +75,7 @@ exports.checkServerIdentity = function checkServerIdentity(host, cert) { // Create regexp to much hostnames function regexpify(host, wildcards) { // Add trailing dot (make hostnames uniform) - if (!/\.$/.test(host)) host += '.'; + if (!host || !host.endsWith('.')) host += '.'; // The same applies to hostname with more than one wildcard, // if hostname has wildcard when wildcards are not allowed, @@ -144,7 +144,7 @@ exports.checkServerIdentity = function checkServerIdentity(host, cert) { } } else if (cert.subject) { // Transform hostname to canonical form - if (!/\.$/.test(host)) host += '.'; + if (!host || !host.endsWith('.')) host += '.'; // Otherwise check all DNS/URI records from certificate // (with allowed wildcards)