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 <benjamingr@gmail.com> Reviewed-By: Brian White <mscdex@mscdex.net>
This commit is contained in:
parent
fb51c396ff
commit
91466b855f
@ -119,10 +119,10 @@ Client.prototype._transform = function _transform(data, enc, cb) {
|
|||||||
while (true) {
|
while (true) {
|
||||||
if (this.state === 'headers') {
|
if (this.state === 'headers') {
|
||||||
// Not enough data
|
// Not enough data
|
||||||
if (!/\r\n/.test(this.buffer))
|
if (!this.buffer.includes('\r\n'))
|
||||||
break;
|
break;
|
||||||
|
|
||||||
if (/^\r\n/.test(this.buffer)) {
|
if (this.buffer.startsWith('\r\n')) {
|
||||||
this.buffer = this.buffer.slice(2);
|
this.buffer = this.buffer.slice(2);
|
||||||
this.state = 'body';
|
this.state = 'body';
|
||||||
continue;
|
continue;
|
||||||
|
@ -1350,7 +1350,7 @@ Interface.prototype.setBreakpoint = function(script, line,
|
|||||||
}
|
}
|
||||||
|
|
||||||
let req;
|
let req;
|
||||||
if (/\(\)$/.test(script)) {
|
if (script.endsWith('()')) {
|
||||||
// setBreakpoint('functionname()');
|
// setBreakpoint('functionname()');
|
||||||
req = {
|
req = {
|
||||||
type: 'function',
|
type: 'function',
|
||||||
|
@ -237,9 +237,8 @@ function masterInit() {
|
|||||||
// Without --logfile=v8-%p.log, everything ends up in a single, unusable
|
// Without --logfile=v8-%p.log, everything ends up in a single, unusable
|
||||||
// file. (Unusable because what V8 logs are memory addresses and each
|
// file. (Unusable because what V8 logs are memory addresses and each
|
||||||
// process has its own memory mappings.)
|
// process has its own memory mappings.)
|
||||||
if (settings.execArgv.some(function(s) { return /^--prof/.test(s); }) &&
|
if (settings.execArgv.some((s) => s.startsWith('--prof')) &&
|
||||||
!settings.execArgv.some(function(s) { return /^--logfile=/.test(s); }))
|
!settings.execArgv.some((s) => s.startsWith('--logfile='))) {
|
||||||
{
|
|
||||||
settings.execArgv = settings.execArgv.concat(['--logfile=v8-%p.log']);
|
settings.execArgv = settings.execArgv.concat(['--logfile=v8-%p.log']);
|
||||||
}
|
}
|
||||||
cluster.settings = settings;
|
cluster.settings = settings;
|
||||||
|
10
lib/os.js
10
lib/os.js
@ -24,23 +24,23 @@ exports.platform = function() {
|
|||||||
return process.platform;
|
return process.platform;
|
||||||
};
|
};
|
||||||
|
|
||||||
const trailingSlashRe = isWindows ? /[^:]\\$/
|
|
||||||
: /.\/$/;
|
|
||||||
|
|
||||||
exports.tmpdir = function() {
|
exports.tmpdir = function() {
|
||||||
var path;
|
var path;
|
||||||
if (isWindows) {
|
if (isWindows) {
|
||||||
path = process.env.TEMP ||
|
path = process.env.TEMP ||
|
||||||
process.env.TMP ||
|
process.env.TMP ||
|
||||||
(process.env.SystemRoot || process.env.windir) + '\\temp';
|
(process.env.SystemRoot || process.env.windir) + '\\temp';
|
||||||
|
if (path.length > 1 && path.endsWith('\\') && !path.endsWith(':\\'))
|
||||||
|
path = path.slice(0, -1);
|
||||||
} else {
|
} else {
|
||||||
path = process.env.TMPDIR ||
|
path = process.env.TMPDIR ||
|
||||||
process.env.TMP ||
|
process.env.TMP ||
|
||||||
process.env.TEMP ||
|
process.env.TEMP ||
|
||||||
'/tmp';
|
'/tmp';
|
||||||
}
|
if (path.length > 1 && path.endsWith('/'))
|
||||||
if (trailingSlashRe.test(path))
|
|
||||||
path = path.slice(0, -1);
|
path = path.slice(0, -1);
|
||||||
|
}
|
||||||
|
|
||||||
return path;
|
return path;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -335,7 +335,7 @@ Interface.prototype._normalWrite = function(b) {
|
|||||||
this._line_buffer = null;
|
this._line_buffer = null;
|
||||||
}
|
}
|
||||||
if (newPartContainsEnding) {
|
if (newPartContainsEnding) {
|
||||||
this._sawReturn = /\r$/.test(string);
|
this._sawReturn = string.endsWith('\r');
|
||||||
|
|
||||||
// got one or more newlines; process into "line" events
|
// got one or more newlines; process into "line" events
|
||||||
var lines = string.split(lineEnding);
|
var lines = string.split(lineEnding);
|
||||||
|
@ -448,7 +448,7 @@ function REPLServer(prompt,
|
|||||||
self.memory(cmd);
|
self.memory(cmd);
|
||||||
|
|
||||||
self.wrappedCmd = false;
|
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 ' +
|
self.outputStream.write('npm should be run outside of the ' +
|
||||||
'node repl, in your normal shell.\n' +
|
'node repl, in your normal shell.\n' +
|
||||||
'(Press Control-D to exit.)\n');
|
'(Press Control-D to exit.)\n');
|
||||||
|
@ -75,7 +75,7 @@ exports.checkServerIdentity = function checkServerIdentity(host, cert) {
|
|||||||
// Create regexp to much hostnames
|
// Create regexp to much hostnames
|
||||||
function regexpify(host, wildcards) {
|
function regexpify(host, wildcards) {
|
||||||
// Add trailing dot (make hostnames uniform)
|
// 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,
|
// The same applies to hostname with more than one wildcard,
|
||||||
// if hostname has wildcard when wildcards are not allowed,
|
// if hostname has wildcard when wildcards are not allowed,
|
||||||
@ -144,7 +144,7 @@ exports.checkServerIdentity = function checkServerIdentity(host, cert) {
|
|||||||
}
|
}
|
||||||
} else if (cert.subject) {
|
} else if (cert.subject) {
|
||||||
// Transform hostname to canonical form
|
// Transform hostname to canonical form
|
||||||
if (!/\.$/.test(host)) host += '.';
|
if (!host || !host.endsWith('.')) host += '.';
|
||||||
|
|
||||||
// Otherwise check all DNS/URI records from certificate
|
// Otherwise check all DNS/URI records from certificate
|
||||||
// (with allowed wildcards)
|
// (with allowed wildcards)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user