Skip to content

Commit 6ac8bdc

Browse files
committed
lib: reduce util.is*() usage
Many of the util.is*() methods used to check data types simply compare against a single value or the result of typeof. This commit replaces calls to these methods with equivalent checks. This commit does not touch calls to the more complex methods (isRegExp(), isDate(), etc.). Fixes: #607 PR-URL: #647 Reviewed-By: Ben Noordhuis <[email protected]>
1 parent bce7a26 commit 6ac8bdc

37 files changed

+451
-445
lines changed

lib/_debugger.js

+12-12
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,8 @@ exports.Client = Client;
164164

165165

166166
Client.prototype._addHandle = function(desc) {
167-
if (!util.isObject(desc) || !util.isNumber(desc.handle)) {
167+
if (desc === null || typeof desc !== 'object' ||
168+
typeof desc.handle !== 'number') {
168169
return;
169170
}
170171

@@ -282,7 +283,7 @@ Client.prototype.reqLookup = function(refs, cb) {
282283
this.req(req, function(err, res) {
283284
if (err) return cb(err);
284285
for (var ref in res) {
285-
if (util.isObject(res[ref])) {
286+
if (res[ref] !== null && typeof res[ref] === 'object') {
286287
self._addHandle(res[ref]);
287288
}
288289
}
@@ -544,8 +545,7 @@ Client.prototype.mirrorObject = function(handle, depth, cb) {
544545
mirrorValue = '[?]';
545546
}
546547

547-
548-
if (util.isArray(mirror) && !util.isNumber(prop.name)) {
548+
if (Array.isArray(mirror) && typeof prop.name !== 'number') {
549549
// Skip the 'length' property.
550550
return;
551551
}
@@ -578,7 +578,7 @@ Client.prototype.mirrorObject = function(handle, depth, cb) {
578578
val = function() {};
579579
} else if (handle.type === 'null') {
580580
val = null;
581-
} else if (!util.isUndefined(handle.value)) {
581+
} else if (handle.value !== undefined) {
582582
val = handle.value;
583583
} else if (handle.type === 'undefined') {
584584
val = undefined;
@@ -879,7 +879,7 @@ Interface.prototype.print = function(text, oneline) {
879879
if (this.killed) return;
880880
this.clearline();
881881

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

884884
if (oneline !== true) {
885885
this.stdout.write('\n');
@@ -1194,7 +1194,7 @@ Interface.prototype.scripts = function() {
11941194
this.pause();
11951195
for (var id in client.scripts) {
11961196
var script = client.scripts[id];
1197-
if (util.isObject(script) && script.name) {
1197+
if (script !== null && typeof script === 'object' && script.name) {
11981198
if (displayNatives ||
11991199
script.name == client.currentScript ||
12001200
!script.isNative) {
@@ -1332,13 +1332,13 @@ Interface.prototype.setBreakpoint = function(script, line,
13321332
ambiguous;
13331333

13341334
// setBreakpoint() should insert breakpoint on current line
1335-
if (util.isUndefined(script)) {
1335+
if (script === undefined) {
13361336
script = this.client.currentScript;
13371337
line = this.client.currentSourceLine + 1;
13381338
}
13391339

13401340
// setBreakpoint(line-number) should insert breakpoint in current script
1341-
if (util.isUndefined(line) && util.isNumber(script)) {
1341+
if (line === undefined && typeof script === 'number') {
13421342
line = script;
13431343
script = this.client.currentScript;
13441344
}
@@ -1442,7 +1442,7 @@ Interface.prototype.clearBreakpoint = function(script, line) {
14421442
if (bp.scriptId === script ||
14431443
bp.scriptReq === script ||
14441444
(bp.script && bp.script.indexOf(script) !== -1)) {
1445-
if (!util.isUndefined(index)) {
1445+
if (index !== undefined) {
14461446
ambiguous = true;
14471447
}
14481448
scriptId = script;
@@ -1470,11 +1470,11 @@ Interface.prototype.clearBreakpoint = function(script, line) {
14701470

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

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

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

lib/_http_client.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,16 @@ function ClientRequest(options, cb) {
1919
var self = this;
2020
OutgoingMessage.call(self);
2121

22-
if (util.isString(options)) {
22+
if (typeof options === 'string') {
2323
options = url.parse(options);
2424
}
2525

2626
var agent = options.agent;
2727
var defaultAgent = options._defaultAgent || Agent.globalAgent;
2828
if (agent === false) {
2929
agent = new defaultAgent.constructor();
30-
} else if (util.isNullOrUndefined(agent) && !options.createConnection) {
30+
} else if ((agent === null || agent === undefined) &&
31+
!options.createConnection) {
3132
agent = defaultAgent;
3233
}
3334
self.agent = agent;
@@ -56,7 +57,7 @@ function ClientRequest(options, cb) {
5657
var port = options.port = options.port || defaultPort || 80;
5758
var host = options.host = options.hostname || options.host || 'localhost';
5859

59-
if (util.isUndefined(options.setHost)) {
60+
if (options.setHost === undefined) {
6061
var setHost = true;
6162
}
6263

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

71-
if (!util.isArray(options.headers)) {
72+
if (!Array.isArray(options.headers)) {
7273
if (options.headers) {
7374
var keys = Object.keys(options.headers);
7475
for (var i = 0, l = keys.length; i < l; i++) {
@@ -101,7 +102,7 @@ function ClientRequest(options, cb) {
101102
self.useChunkedEncodingByDefault = true;
102103
}
103104

104-
if (util.isArray(options.headers)) {
105+
if (Array.isArray(options.headers)) {
105106
self._storeHeader(self.method + ' ' + self.path + ' HTTP/1.1\r\n',
106107
options.headers);
107108
} else if (self.getHeader('expect')) {
@@ -446,7 +447,7 @@ function tickOnSocket(req, socket) {
446447
httpSocketSetup(socket);
447448

448449
// Propagate headers limit from request object to parser
449-
if (util.isNumber(req.maxHeadersCount)) {
450+
if (typeof req.maxHeadersCount === 'number') {
450451
parser.maxHeaderPairs = req.maxHeadersCount << 1;
451452
} else {
452453
// Set default value because parser may be reused from FreeList

lib/_http_common.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ const IncomingMessage = incoming.IncomingMessage;
88
const readStart = incoming.readStart;
99
const readStop = incoming.readStop;
1010

11-
const isNumber = require('util').isNumber;
1211
const debug = require('util').debuglog('http');
1312
exports.debug = debug;
1413

@@ -69,7 +68,7 @@ function parserOnHeadersComplete(versionMajor, versionMinor, headers, method,
6968

7069
parser.incoming._addHeaderLines(headers, n);
7170

72-
if (isNumber(method)) {
71+
if (typeof method === 'number') {
7372
// server only
7473
parser.incoming.method = HTTPParser.methods[method];
7574
} else {

lib/_http_incoming.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ IncomingMessage.prototype._addHeaderLine = function(field, value, dest) {
130130
switch (field) {
131131
// Array headers:
132132
case 'set-cookie':
133-
if (!util.isUndefined(dest[field])) {
133+
if (dest[field] !== undefined) {
134134
dest[field].push(value);
135135
} else {
136136
dest[field] = [value];
@@ -152,13 +152,13 @@ IncomingMessage.prototype._addHeaderLine = function(field, value, dest) {
152152
case 'location':
153153
case 'max-forwards':
154154
// drop duplicates
155-
if (util.isUndefined(dest[field]))
155+
if (dest[field] === undefined)
156156
dest[field] = value;
157157
break;
158158

159159
default:
160160
// make comma-separated list
161-
if (!util.isUndefined(dest[field]))
161+
if (dest[field] !== undefined)
162162
dest[field] += ', ' + value;
163163
else {
164164
dest[field] = value;

lib/_http_outgoing.js

+13-14
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ OutgoingMessage.prototype._send = function(data, encoding, callback) {
106106
// the same packet. Future versions of Node are going to take care of
107107
// this at a lower level and in a more general way.
108108
if (!this._headerSent) {
109-
if (util.isString(data) &&
109+
if (typeof data === 'string' &&
110110
encoding !== 'hex' &&
111111
encoding !== 'base64') {
112112
data = this._header + data;
@@ -122,13 +122,13 @@ OutgoingMessage.prototype._send = function(data, encoding, callback) {
122122

123123

124124
OutgoingMessage.prototype._writeRaw = function(data, encoding, callback) {
125-
if (util.isFunction(encoding)) {
125+
if (typeof encoding === 'function') {
126126
callback = encoding;
127127
encoding = null;
128128
}
129129

130130
if (data.length === 0) {
131-
if (util.isFunction(callback))
131+
if (typeof callback === 'function')
132132
process.nextTick(callback);
133133
return true;
134134
}
@@ -187,7 +187,7 @@ OutgoingMessage.prototype._storeHeader = function(firstLine, headers) {
187187

188188
if (headers) {
189189
var keys = Object.keys(headers);
190-
var isArray = util.isArray(headers);
190+
var isArray = Array.isArray(headers);
191191
var field, value;
192192

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

203-
if (util.isArray(value)) {
203+
if (Array.isArray(value)) {
204204
for (var j = 0; j < value.length; j++) {
205205
storeHeader(this, state, field, value[j]);
206206
}
@@ -408,7 +408,7 @@ OutgoingMessage.prototype.write = function(chunk, encoding, callback) {
408408
return true;
409409
}
410410

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

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

420420
var len, ret;
421421
if (this.chunkedEncoding) {
422-
if (util.isString(chunk) &&
422+
if (typeof chunk === 'string' &&
423423
encoding !== 'hex' &&
424424
encoding !== 'base64' &&
425425
encoding !== 'binary') {
@@ -428,7 +428,7 @@ OutgoingMessage.prototype.write = function(chunk, encoding, callback) {
428428
ret = this._send(chunk, encoding, callback);
429429
} else {
430430
// buffer, or a non-toString-friendly encoding
431-
if (util.isString(chunk))
431+
if (typeof chunk === 'string')
432432
len = Buffer.byteLength(chunk, encoding);
433433
else
434434
len = chunk.length;
@@ -458,7 +458,7 @@ OutgoingMessage.prototype.write = function(chunk, encoding, callback) {
458458
OutgoingMessage.prototype.addTrailers = function(headers) {
459459
this._trailer = '';
460460
var keys = Object.keys(headers);
461-
var isArray = util.isArray(headers);
461+
var isArray = Array.isArray(headers);
462462
var field, value;
463463
for (var i = 0, l = keys.length; i < l; i++) {
464464
var key = keys[i];
@@ -479,15 +479,15 @@ const crlf_buf = new Buffer('\r\n');
479479

480480

481481
OutgoingMessage.prototype.end = function(data, encoding, callback) {
482-
if (util.isFunction(data)) {
482+
if (typeof data === 'function') {
483483
callback = data;
484484
data = null;
485-
} else if (util.isFunction(encoding)) {
485+
} else if (typeof encoding === 'function') {
486486
callback = encoding;
487487
encoding = null;
488488
}
489489

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

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

503-
if (util.isFunction(callback))
503+
if (typeof callback === 'function')
504504
this.once('finish', callback);
505505

506-
507506
if (!this._header) {
508507
this._implicitHeader();
509508
}

lib/_http_server.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ ServerResponse.prototype._implicitHeader = function() {
153153
ServerResponse.prototype.writeHead = function(statusCode, reason, obj) {
154154
var headers;
155155

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

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

458-
if (!util.isUndefined(req.headers.expect) &&
458+
if (req.headers.expect !== undefined &&
459459
(req.httpVersionMajor == 1 && req.httpVersionMinor == 1) &&
460460
continueExpression.test(req.headers['expect'])) {
461461
res._expect_continue = true;

0 commit comments

Comments
 (0)