Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: reduce util.is*() usage #647

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions lib/_debugger.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@ exports.Client = Client;


Client.prototype._addHandle = function(desc) {
if (!util.isObject(desc) || !util.isNumber(desc.handle)) {
if (desc === null || typeof desc !== 'object' ||
typeof desc.handle !== 'number') {
return;
}

Expand Down Expand Up @@ -282,7 +283,7 @@ Client.prototype.reqLookup = function(refs, cb) {
this.req(req, function(err, res) {
if (err) return cb(err);
for (var ref in res) {
if (util.isObject(res[ref])) {
if (res[ref] !== null && typeof res[ref] === 'object') {
self._addHandle(res[ref]);
}
}
Expand Down Expand Up @@ -544,8 +545,7 @@ Client.prototype.mirrorObject = function(handle, depth, cb) {
mirrorValue = '[?]';
}


if (util.isArray(mirror) && !util.isNumber(prop.name)) {
if (Array.isArray(mirror) && typeof prop.name !== 'number') {
// Skip the 'length' property.
return;
}
Expand Down Expand Up @@ -578,7 +578,7 @@ Client.prototype.mirrorObject = function(handle, depth, cb) {
val = function() {};
} else if (handle.type === 'null') {
val = null;
} else if (!util.isUndefined(handle.value)) {
} else if (handle.value !== undefined) {
val = handle.value;
} else if (handle.type === 'undefined') {
val = undefined;
Expand Down Expand Up @@ -879,7 +879,7 @@ Interface.prototype.print = function(text, oneline) {
if (this.killed) return;
this.clearline();

this.stdout.write(util.isString(text) ? text : util.inspect(text));
this.stdout.write(typeof text === 'string' ? text : util.inspect(text));

if (oneline !== true) {
this.stdout.write('\n');
Expand Down Expand Up @@ -1194,7 +1194,7 @@ Interface.prototype.scripts = function() {
this.pause();
for (var id in client.scripts) {
var script = client.scripts[id];
if (util.isObject(script) && script.name) {
if (script !== null && typeof script === 'object' && script.name) {
if (displayNatives ||
script.name == client.currentScript ||
!script.isNative) {
Expand Down Expand Up @@ -1332,13 +1332,13 @@ Interface.prototype.setBreakpoint = function(script, line,
ambiguous;

// setBreakpoint() should insert breakpoint on current line
if (util.isUndefined(script)) {
if (script === undefined) {
script = this.client.currentScript;
line = this.client.currentSourceLine + 1;
}

// setBreakpoint(line-number) should insert breakpoint in current script
if (util.isUndefined(line) && util.isNumber(script)) {
if (line === undefined && typeof script === 'number') {
line = script;
script = this.client.currentScript;
}
Expand Down Expand Up @@ -1442,7 +1442,7 @@ Interface.prototype.clearBreakpoint = function(script, line) {
if (bp.scriptId === script ||
bp.scriptReq === script ||
(bp.script && bp.script.indexOf(script) !== -1)) {
if (!util.isUndefined(index)) {
if (index !== undefined) {
ambiguous = true;
}
scriptId = script;
Expand Down Expand Up @@ -1470,11 +1470,11 @@ Interface.prototype.clearBreakpoint = function(script, line) {

if (ambiguous) return this.error('Script name is ambiguous');

if (util.isUndefined(scriptId)) {
if (scriptId === undefined) {
return this.error('Script ' + script + ' not found');
}

if (util.isUndefined(breakpoint)) {
if (breakpoint === undefined) {
return this.error('Breakpoint not found on line ' + line);
}

Expand Down
13 changes: 7 additions & 6 deletions lib/_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,16 @@ function ClientRequest(options, cb) {
var self = this;
OutgoingMessage.call(self);

if (util.isString(options)) {
if (typeof options === 'string') {
options = url.parse(options);
}

var agent = options.agent;
var defaultAgent = options._defaultAgent || Agent.globalAgent;
if (agent === false) {
agent = new defaultAgent.constructor();
} else if (util.isNullOrUndefined(agent) && !options.createConnection) {
} else if ((agent === null || agent === undefined) &&
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be shortened to agent == null.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. I'll update these. I was trying to avoid using ==.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On second thought, I'll leave it to your good judgment. Doing the explicit checks is a little more efficient than == because they don't generate an IC.

!options.createConnection) {
agent = defaultAgent;
}
self.agent = agent;
Expand Down Expand Up @@ -56,7 +57,7 @@ function ClientRequest(options, cb) {
var port = options.port = options.port || defaultPort || 80;
var host = options.host = options.hostname || options.host || 'localhost';

if (util.isUndefined(options.setHost)) {
if (options.setHost === undefined) {
var setHost = true;
}

Expand All @@ -68,7 +69,7 @@ function ClientRequest(options, cb) {
self.once('response', cb);
}

if (!util.isArray(options.headers)) {
if (!Array.isArray(options.headers)) {
if (options.headers) {
var keys = Object.keys(options.headers);
for (var i = 0, l = keys.length; i < l; i++) {
Expand Down Expand Up @@ -101,7 +102,7 @@ function ClientRequest(options, cb) {
self.useChunkedEncodingByDefault = true;
}

if (util.isArray(options.headers)) {
if (Array.isArray(options.headers)) {
self._storeHeader(self.method + ' ' + self.path + ' HTTP/1.1\r\n',
options.headers);
} else if (self.getHeader('expect')) {
Expand Down Expand Up @@ -446,7 +447,7 @@ function tickOnSocket(req, socket) {
httpSocketSetup(socket);

// Propagate headers limit from request object to parser
if (util.isNumber(req.maxHeadersCount)) {
if (typeof req.maxHeadersCount === 'number') {
parser.maxHeaderPairs = req.maxHeadersCount << 1;
} else {
// Set default value because parser may be reused from FreeList
Expand Down
3 changes: 1 addition & 2 deletions lib/_http_common.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ const IncomingMessage = incoming.IncomingMessage;
const readStart = incoming.readStart;
const readStop = incoming.readStop;

const isNumber = require('util').isNumber;
const debug = require('util').debuglog('http');
exports.debug = debug;

Expand Down Expand Up @@ -69,7 +68,7 @@ function parserOnHeadersComplete(versionMajor, versionMinor, headers, method,

parser.incoming._addHeaderLines(headers, n);

if (isNumber(method)) {
if (typeof method === 'number') {
// server only
parser.incoming.method = HTTPParser.methods[method];
} else {
Expand Down
6 changes: 3 additions & 3 deletions lib/_http_incoming.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ IncomingMessage.prototype._addHeaderLine = function(field, value, dest) {
switch (field) {
// Array headers:
case 'set-cookie':
if (!util.isUndefined(dest[field])) {
if (dest[field] !== undefined) {
dest[field].push(value);
} else {
dest[field] = [value];
Expand All @@ -152,13 +152,13 @@ IncomingMessage.prototype._addHeaderLine = function(field, value, dest) {
case 'location':
case 'max-forwards':
// drop duplicates
if (util.isUndefined(dest[field]))
if (dest[field] === undefined)
dest[field] = value;
break;

default:
// make comma-separated list
if (!util.isUndefined(dest[field]))
if (dest[field] !== undefined)
dest[field] += ', ' + value;
else {
dest[field] = value;
Expand Down
27 changes: 13 additions & 14 deletions lib/_http_outgoing.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ OutgoingMessage.prototype._send = function(data, encoding, callback) {
// the same packet. Future versions of Node are going to take care of
// this at a lower level and in a more general way.
if (!this._headerSent) {
if (util.isString(data) &&
if (typeof data === 'string' &&
encoding !== 'hex' &&
encoding !== 'base64') {
data = this._header + data;
Expand All @@ -122,13 +122,13 @@ OutgoingMessage.prototype._send = function(data, encoding, callback) {


OutgoingMessage.prototype._writeRaw = function(data, encoding, callback) {
if (util.isFunction(encoding)) {
if (typeof encoding === 'function') {
callback = encoding;
encoding = null;
}

if (data.length === 0) {
if (util.isFunction(callback))
if (typeof callback === 'function')
process.nextTick(callback);
return true;
}
Expand Down Expand Up @@ -187,7 +187,7 @@ OutgoingMessage.prototype._storeHeader = function(firstLine, headers) {

if (headers) {
var keys = Object.keys(headers);
var isArray = util.isArray(headers);
var isArray = Array.isArray(headers);
var field, value;

for (var i = 0, l = keys.length; i < l; i++) {
Expand All @@ -200,7 +200,7 @@ OutgoingMessage.prototype._storeHeader = function(firstLine, headers) {
value = headers[key];
}

if (util.isArray(value)) {
if (Array.isArray(value)) {
for (var j = 0; j < value.length; j++) {
storeHeader(this, state, field, value[j]);
}
Expand Down Expand Up @@ -408,7 +408,7 @@ OutgoingMessage.prototype.write = function(chunk, encoding, callback) {
return true;
}

if (!util.isString(chunk) && !util.isBuffer(chunk)) {
if (typeof chunk !== 'string' && !(chunk instanceof Buffer)) {
throw new TypeError('first argument must be a string or Buffer');
}

Expand All @@ -419,7 +419,7 @@ OutgoingMessage.prototype.write = function(chunk, encoding, callback) {

var len, ret;
if (this.chunkedEncoding) {
if (util.isString(chunk) &&
if (typeof chunk === 'string' &&
encoding !== 'hex' &&
encoding !== 'base64' &&
encoding !== 'binary') {
Expand All @@ -428,7 +428,7 @@ OutgoingMessage.prototype.write = function(chunk, encoding, callback) {
ret = this._send(chunk, encoding, callback);
} else {
// buffer, or a non-toString-friendly encoding
if (util.isString(chunk))
if (typeof chunk === 'string')
len = Buffer.byteLength(chunk, encoding);
else
len = chunk.length;
Expand Down Expand Up @@ -458,7 +458,7 @@ OutgoingMessage.prototype.write = function(chunk, encoding, callback) {
OutgoingMessage.prototype.addTrailers = function(headers) {
this._trailer = '';
var keys = Object.keys(headers);
var isArray = util.isArray(headers);
var isArray = Array.isArray(headers);
var field, value;
for (var i = 0, l = keys.length; i < l; i++) {
var key = keys[i];
Expand All @@ -479,15 +479,15 @@ const crlf_buf = new Buffer('\r\n');


OutgoingMessage.prototype.end = function(data, encoding, callback) {
if (util.isFunction(data)) {
if (typeof data === 'function') {
callback = data;
data = null;
} else if (util.isFunction(encoding)) {
} else if (typeof encoding === 'function') {
callback = encoding;
encoding = null;
}

if (data && !util.isString(data) && !util.isBuffer(data)) {
if (data && typeof data !== 'string' && !(data instanceof Buffer)) {
throw new TypeError('first argument must be a string or Buffer');
}

Expand All @@ -500,10 +500,9 @@ OutgoingMessage.prototype.end = function(data, encoding, callback) {
self.emit('finish');
}

if (util.isFunction(callback))
if (typeof callback === 'function')
this.once('finish', callback);


if (!this._header) {
this._implicitHeader();
}
Expand Down
6 changes: 3 additions & 3 deletions lib/_http_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ ServerResponse.prototype._implicitHeader = function() {
ServerResponse.prototype.writeHead = function(statusCode, reason, obj) {
var headers;

if (util.isString(reason)) {
if (typeof reason === 'string') {
// writeHead(statusCode, reasonPhrase[, headers])
this.statusMessage = reason;
} else {
Expand Down Expand Up @@ -297,7 +297,7 @@ function connectionListener(socket) {
parser.incoming = null;

// Propagate headers limit from server instance to parser
if (util.isNumber(this.maxHeadersCount)) {
if (typeof this.maxHeadersCount === 'number') {
parser.maxHeaderPairs = this.maxHeadersCount << 1;
} else {
// Set default value because parser may be reused from FreeList
Expand Down Expand Up @@ -455,7 +455,7 @@ function connectionListener(socket) {
}
}

if (!util.isUndefined(req.headers.expect) &&
if (req.headers.expect !== undefined &&
(req.httpVersionMajor == 1 && req.httpVersionMinor == 1) &&
continueExpression.test(req.headers['expect'])) {
res._expect_continue = true;
Expand Down
Loading