GeezerVison is showing github.com — lib: macro-ify type checks · lightsofapollo/node@0330bdf
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/_debugger.js +10-11Lines changed: 10 additions & 11 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -182,7 +182,7 @@ exports.Client = Client; | 182 | 182 |
| 183 | 183 |
| 184 | 184 | Client.prototype._addHandle = function(desc) { | 185 | | - if (typeof desc != 'object' || typeof desc.handle != 'number') { | | 185 | + if (!IS_OBJECT(desc) || !IS_NUMBER(desc.handle)) { | 186 | 186 | return; | 187 | 187 | } | 188 | 188 |
| @@ -296,7 +296,7 @@ Client.prototype.reqLookup = function(refs, cb) { | 296 | 296 | this.req(req, function(err, res) { | 297 | 297 | if (err) return cb(err); | 298 | 298 | for (var ref in res) { | 299 | | - if (typeof res[ref] == 'object') { | | 299 | + if (IS_OBJECT(res[ref])) { | 300 | 300 | self._addHandle(res[ref]); | 301 | 301 | } | 302 | 302 | } | @@ -559,8 +559,7 @@ Client.prototype.mirrorObject = function(handle, depth, cb) { | 559 | 559 | } | 560 | 560 |
| 561 | 561 |
| 562 | | - if (Array.isArray(mirror) && | 563 | | - typeof prop.name != 'number') { | | 562 | + if (IS_ARRAY(mirror) && !IS_NUMBER(prop.name)) { | 564 | 563 | // Skip the 'length' property. | 565 | 564 | return; | 566 | 565 | } | @@ -593,7 +592,7 @@ Client.prototype.mirrorObject = function(handle, depth, cb) { | 593 | 592 | val = function() {}; | 594 | 593 | } else if (handle.type === 'null') { | 595 | 594 | val = null; | 596 | | - } else if (handle.value !== undefined) { | | 595 | + } else if (!IS_UNDEFINED(handle.value)) { | 597 | 596 | val = handle.value; | 598 | 597 | } else if (handle.type === 'undefined') { | 599 | 598 | val = undefined; | @@ -892,7 +891,7 @@ Interface.prototype.print = function(text, oneline) { | 892 | 891 | if (this.killed) return; | 893 | 892 | this.clearline(); | 894 | 893 |
| 895 | | - this.stdout.write(typeof text === 'string' ? text : util.inspect(text)); | | 894 | + this.stdout.write(IS_STRING(text) ? text : util.inspect(text)); | 896 | 895 |
| 897 | 896 | if (oneline !== true) { | 898 | 897 | this.stdout.write('\n'); | @@ -1214,7 +1213,7 @@ Interface.prototype.scripts = function() { | 1214 | 1213 | this.pause(); | 1215 | 1214 | for (var id in client.scripts) { | 1216 | 1215 | var script = client.scripts[id]; | 1217 | | - if (typeof script == 'object' && script.name) { | | 1216 | + if (IS_OBJECT(script) && script.name) { | 1218 | 1217 | if (displayNatives || | 1219 | 1218 | script.name == client.currentScript || | 1220 | 1219 | !script.isNative) { | @@ -1351,13 +1350,13 @@ Interface.prototype.setBreakpoint = function(script, line, | 1351 | 1350 | ambiguous; | 1352 | 1351 |
| 1353 | 1352 | // setBreakpoint() should insert breakpoint on current line | 1354 | | - if (script === undefined) { | | 1353 | + if (IS_UNDEFINED(script)) { | 1355 | 1354 | script = this.client.currentScript; | 1356 | 1355 | line = this.client.currentSourceLine + 1; | 1357 | 1356 | } | 1358 | 1357 |
| 1359 | 1358 | // setBreakpoint(line-number) should insert breakpoint in current script | 1360 | | - if (line === undefined && typeof script === 'number') { | | 1359 | + if (IS_UNDEFINED(line) && IS_NUMBER(script)) { | 1361 | 1360 | line = script; | 1362 | 1361 | script = this.client.currentScript; | 1363 | 1362 | } | @@ -1452,7 +1451,7 @@ Interface.prototype.clearBreakpoint = function(script, line) { | 1452 | 1451 | if (bp.scriptId === script || | 1453 | 1452 | bp.scriptReq === script || | 1454 | 1453 | (bp.script && bp.script.indexOf(script) !== -1)) { | 1455 | | - if (index !== undefined) { | | 1454 | + if (!IS_UNDEFINED(index)) { | 1456 | 1455 | ambiguous = true; | 1457 | 1456 | } | 1458 | 1457 | if (bp.line === line) { | @@ -1465,7 +1464,7 @@ Interface.prototype.clearBreakpoint = function(script, line) { | 1465 | 1464 |
| 1466 | 1465 | if (ambiguous) return this.error('Script name is ambiguous'); | 1467 | 1466 |
| 1468 | | - if (breakpoint === undefined) { | | 1467 | + if (IS_UNDEFINED(breakpoint)) { | 1469 | 1468 | return this.error('Script : ' + script + ' not found'); | 1470 | 1469 | } | 1471 | 1470 |
| |
Collapse file Copy file name to clipboardExpand all lines: lib/_http_agent.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -246,7 +246,7 @@ Agent.prototype.destroy = function() { | 246 | 246 | }; | 247 | 247 |
| 248 | 248 | Agent.prototype.request = function(options, cb) { | 249 | | - if (typeof options === 'string') { | | 249 | + if (IS_STRING(options)) { | 250 | 250 | options = url.parse(options); | 251 | 251 | } | 252 | 252 | // don't try to do dns lookups of foo.com:8080, just foo.com | |
Collapse file Copy file name to clipboardExpand all lines: lib/_http_client.js +5-5Lines changed: 5 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -44,14 +44,14 @@ function ClientRequest(options, cb) { | 44 | 44 | var self = this; | 45 | 45 | OutgoingMessage.call(self); | 46 | 46 |
| 47 | | - self.agent = options.agent === undefined ? globalAgent : options.agent; | | 47 | + self.agent = IS_UNDEFINED(options.agent) ? globalAgent : options.agent; | 48 | 48 |
| 49 | 49 | var defaultPort = options.defaultPort || 80; | 50 | 50 |
| 51 | 51 | var port = options.port || defaultPort; | 52 | 52 | var host = options.hostname || options.host || 'localhost'; | 53 | 53 |
| 54 | | - if (options.setHost === undefined) { | | 54 | + if (IS_UNDEFINED(options.setHost)) { | 55 | 55 | var setHost = true; | 56 | 56 | } | 57 | 57 |
| @@ -63,7 +63,7 @@ function ClientRequest(options, cb) { | 63 | 63 | self.once('response', cb); | 64 | 64 | } | 65 | 65 |
| 66 | | - if (!Array.isArray(options.headers)) { | | 66 | + if (!IS_ARRAY(options.headers)) { | 67 | 67 | if (options.headers) { | 68 | 68 | var keys = Object.keys(options.headers); | 69 | 69 | for (var i = 0, l = keys.length; i < l; i++) { | @@ -92,7 +92,7 @@ function ClientRequest(options, cb) { | 92 | 92 | self.useChunkedEncodingByDefault = true; | 93 | 93 | } | 94 | 94 |
| 95 | | - if (Array.isArray(options.headers)) { | | 95 | + if (IS_ARRAY(options.headers)) { | 96 | 96 | self._storeHeader(self.method + ' ' + self.path + ' HTTP/1.1\r\n', | 97 | 97 | options.headers); | 98 | 98 | } else if (self.getHeader('expect')) { | @@ -413,7 +413,7 @@ ClientRequest.prototype.onSocket = function(socket) { | 413 | 413 | httpSocketSetup(socket); | 414 | 414 |
| 415 | 415 | // Propagate headers limit from request object to parser | 416 | | - if (typeof req.maxHeadersCount === 'number') { | | 416 | + if (IS_NUMBER(req.maxHeadersCount)) { | 417 | 417 | parser.maxHeaderPairs = req.maxHeadersCount << 1; | 418 | 418 | } else { | 419 | 419 | // Set default value because parser may be reused from FreeList | |
Collapse file Copy file name to clipboardExpand all lines: lib/_http_incoming.js +4-4Lines changed: 4 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -125,7 +125,7 @@ IncomingMessage.prototype._addHeaderLine = function(field, value) { | 125 | 125 | switch (field) { | 126 | 126 | // Array headers: | 127 | 127 | case 'set-cookie': | 128 | | - if (dest[field] !== undefined) { | | 128 | + if (!IS_UNDEFINED(dest[field])) { | 129 | 129 | dest[field].push(value); | 130 | 130 | } else { | 131 | 131 | dest[field] = [value]; | @@ -145,7 +145,7 @@ IncomingMessage.prototype._addHeaderLine = function(field, value) { | 145 | 145 | case 'proxy-authenticate': | 146 | 146 | case 'sec-websocket-extensions': | 147 | 147 | case 'sec-websocket-protocol': | 148 | | - if (dest[field] !== undefined) { | | 148 | + if (!IS_UNDEFINED(dest[field])) { | 149 | 149 | dest[field] += ', ' + value; | 150 | 150 | } else { | 151 | 151 | dest[field] = value; | @@ -156,14 +156,14 @@ IncomingMessage.prototype._addHeaderLine = function(field, value) { | 156 | 156 | default: | 157 | 157 | if (field.slice(0, 2) == 'x-') { | 158 | 158 | // except for x- | 159 | | - if (dest[field] !== undefined) { | | 159 | + if (!IS_UNDEFINED(dest[field])) { | 160 | 160 | dest[field] += ', ' + value; | 161 | 161 | } else { | 162 | 162 | dest[field] = value; | 163 | 163 | } | 164 | 164 | } else { | 165 | 165 | // drop duplicates | 166 | | - if (dest[field] === undefined) dest[field] = value; | | 166 | + if (IS_UNDEFINED(dest[field])) dest[field] = value; | 167 | 167 | } | 168 | 168 | break; | 169 | 169 | } | |
Collapse file Copy file name to clipboardExpand all lines: lib/_http_outgoing.js +8-8Lines changed: 8 additions & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -115,7 +115,7 @@ OutgoingMessage.prototype._send = function(data, encoding) { | 115 | 115 | // the same packet. Future versions of Node are going to take care of | 116 | 116 | // this at a lower level and in a more general way. | 117 | 117 | if (!this._headerSent) { | 118 | | - if (typeof data === 'string') { | | 118 | + if (IS_STRING(data)) { | 119 | 119 | data = this._header + data; | 120 | 120 | } else { | 121 | 121 | this.output.unshift(this._header); | @@ -166,7 +166,7 @@ OutgoingMessage.prototype._buffer = function(data, encoding) { | 166 | 166 |
| 167 | 167 | var length = this.output.length; | 168 | 168 |
| 169 | | - if (length === 0 || typeof data != 'string') { | | 169 | + if (length === 0 || !IS_STRING(data)) { | 170 | 170 | this.output.push(data); | 171 | 171 | this.outputEncodings.push(encoding); | 172 | 172 | return false; | @@ -205,7 +205,7 @@ OutgoingMessage.prototype._storeHeader = function(firstLine, headers) { | 205 | 205 |
| 206 | 206 | if (headers) { | 207 | 207 | var keys = Object.keys(headers); | 208 | | - var isArray = (Array.isArray(headers)); | | 208 | + var isArray = IS_ARRAY(headers); | 209 | 209 | var field, value; | 210 | 210 |
| 211 | 211 | for (var i = 0, l = keys.length; i < l; i++) { | @@ -218,7 +218,7 @@ OutgoingMessage.prototype._storeHeader = function(firstLine, headers) { | 218 | 218 | value = headers[key]; | 219 | 219 | } | 220 | 220 |
| 221 | | - if (Array.isArray(value)) { | | 221 | + if (IS_ARRAY(value)) { | 222 | 222 | for (var j = 0; j < value.length; j++) { | 223 | 223 | storeHeader(this, state, field, value[j]); | 224 | 224 | } | @@ -401,7 +401,7 @@ OutgoingMessage.prototype.write = function(chunk, encoding) { | 401 | 401 | return true; | 402 | 402 | } | 403 | 403 |
| 404 | | - if (typeof chunk !== 'string' && !Buffer.isBuffer(chunk)) { | | 404 | + if (!IS_STRING(chunk) && !IS_BUFFER(chunk)) { | 405 | 405 | throw new TypeError('first argument must be a string or Buffer'); | 406 | 406 | } | 407 | 407 |
| @@ -412,7 +412,7 @@ OutgoingMessage.prototype.write = function(chunk, encoding) { | 412 | 412 |
| 413 | 413 | var len, ret; | 414 | 414 | if (this.chunkedEncoding) { | 415 | | - if (typeof(chunk) === 'string' && | | 415 | + if (IS_STRING(chunk) && | 416 | 416 | encoding !== 'hex' && | 417 | 417 | encoding !== 'base64' && | 418 | 418 | encoding !== 'binary') { | @@ -444,7 +444,7 @@ OutgoingMessage.prototype.write = function(chunk, encoding) { | 444 | 444 | OutgoingMessage.prototype.addTrailers = function(headers) { | 445 | 445 | this._trailer = ''; | 446 | 446 | var keys = Object.keys(headers); | 447 | | - var isArray = (Array.isArray(headers)); | | 447 | + var isArray = IS_ARRAY(headers); | 448 | 448 | var field, value; | 449 | 449 | for (var i = 0, l = keys.length; i < l; i++) { | 450 | 450 | var key = keys[i]; | @@ -466,7 +466,7 @@ var crlf_buf = new Buffer('\r\n'); | 466 | 466 |
| 467 | 467 |
| 468 | 468 | OutgoingMessage.prototype.end = function(data, encoding) { | 469 | | - if (data && typeof data !== 'string' && !Buffer.isBuffer(data)) { | | 469 | + if (data && !IS_STRING(data) && !IS_BUFFER(data)) { | 470 | 470 | throw new TypeError('first argument must be a string or Buffer'); | 471 | 471 | } | 472 | 472 |
| |
Collapse file Copy file name to clipboardExpand all lines: lib/_http_server.js +5-5Lines changed: 5 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -173,7 +173,7 @@ ServerResponse.prototype._implicitHeader = function() { | 173 | 173 | ServerResponse.prototype.writeHead = function(statusCode) { | 174 | 174 | var reasonPhrase, headers, headerIndex; | 175 | 175 |
| 176 | | - if (typeof arguments[1] == 'string') { | | 176 | + if (IS_STRING(arguments[1])) { | 177 | 177 | reasonPhrase = arguments[1]; | 178 | 178 | headerIndex = 2; | 179 | 179 | } else { | @@ -188,13 +188,13 @@ ServerResponse.prototype.writeHead = function(statusCode) { | 188 | 188 | // Slow-case: when progressive API and header fields are passed. | 189 | 189 | headers = this._renderHeaders(); | 190 | 190 |
| 191 | | - if (Array.isArray(obj)) { | | 191 | + if (IS_ARRAY(obj)) { | 192 | 192 | // handle array case | 193 | 193 | // TODO: remove when array is no longer accepted | 194 | 194 | var field; | 195 | 195 | for (var i = 0, len = obj.length; i < len; ++i) { | 196 | 196 | field = obj[i][0]; | 197 | | - if (headers[field] !== undefined) { | | 197 | + if (!IS_UNDEFINED(headers[field])) { | 198 | 198 | obj.push([field, headers[field]]); | 199 | 199 | } | 200 | 200 | } | @@ -332,7 +332,7 @@ function connectionListener(socket) { | 332 | 332 | parser.incoming = null; | 333 | 333 |
| 334 | 334 | // Propagate headers limit from server instance to parser | 335 | | - if (typeof this.maxHeadersCount === 'number') { | | 335 | + if (IS_NUMBER(this.maxHeadersCount)) { | 336 | 336 | parser.maxHeaderPairs = this.maxHeadersCount << 1; | 337 | 337 | } else { | 338 | 338 | // Set default value because parser may be reused from FreeList | @@ -442,7 +442,7 @@ function connectionListener(socket) { | 442 | 442 | } | 443 | 443 | }); | 444 | 444 |
| 445 | | - if (req.headers.expect !== undefined && | | 445 | + if (!IS_UNDEFINED(req.headers.expect) && | 446 | 446 | (req.httpVersionMajor == 1 && req.httpVersionMinor == 1) && | 447 | 447 | continueExpression.test(req.headers['expect'])) { | 448 | 448 | res._expect_continue = true; | |
Collapse file Copy file name to clipboardExpand all lines: lib/_stream_readable.js +10-12Lines changed: 10 additions & 12 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -117,7 +117,7 @@ function Readable(options) { | 117 | 117 | Readable.prototype.push = function(chunk, encoding) { | 118 | 118 | var state = this._readableState; | 119 | 119 |
| 120 | | - if (typeof chunk === 'string' && !state.objectMode) { | | 120 | + if (IS_STRING(chunk) && !state.objectMode) { | 121 | 121 | encoding = encoding || state.defaultEncoding; | 122 | 122 | if (encoding !== state.encoding) { | 123 | 123 | chunk = new Buffer(chunk, encoding); | @@ -138,7 +138,7 @@ function readableAddChunk(stream, state, chunk, encoding, addToFront) { | 138 | 138 | var er = chunkInvalid(state, chunk); | 139 | 139 | if (er) { | 140 | 140 | stream.emit('error', er); | 141 | | - } else if (chunk === null || chunk === undefined) { | | 141 | + } else if (IS_NULL_OR_UNDEFINED(chunk)) { | 142 | 142 | state.reading = false; | 143 | 143 | if (!state.ended) | 144 | 144 | onEofChunk(stream, state); | @@ -219,7 +219,7 @@ function howMuchToRead(n, state) { | 219 | 219 | if (state.objectMode) | 220 | 220 | return n === 0 ? 0 : 1; | 221 | 221 |
| 222 | | - if (isNaN(n) || n === null) { | | 222 | + if (isNaN(n) || IS_NULL(n)) { | 223 | 223 | // only flow one buffer at a time | 224 | 224 | if (state.flowing && state.buffer.length) | 225 | 225 | return state.buffer[0].length; | @@ -256,7 +256,7 @@ Readable.prototype.read = function(n) { | 256 | 256 | state.calledRead = true; | 257 | 257 | var nOrig = n; | 258 | 258 |
| 259 | | - if (typeof n !== 'number' || n > 0) | | 259 | + if (!IS_NUMBER(n) || n > 0) | 260 | 260 | state.emittedReadable = false; | 261 | 261 |
| 262 | 262 | // if we're doing read(0) to trigger a readable event, but we | @@ -345,7 +345,7 @@ Readable.prototype.read = function(n) { | 345 | 345 | else | 346 | 346 | ret = null; | 347 | 347 |
| 348 | | - if (ret === null) { | | 348 | + if (IS_NULL(ret)) { | 349 | 349 | state.needReadable = true; | 350 | 350 | n = 0; | 351 | 351 | } | @@ -363,17 +363,16 @@ Readable.prototype.read = function(n) { | 363 | 363 | if (state.ended && !state.endEmitted && state.length === 0) | 364 | 364 | endReadable(this); | 365 | 365 |
| 366 | | - if (ret !== null) | | 366 | + if (!IS_NULL(ret)) | 367 | 367 | this.emit('data', ret); | 368 | 368 | return ret; | 369 | 369 | }; | 370 | 370 |
| 371 | 371 | function chunkInvalid(state, chunk) { | 372 | 372 | var er = null; | 373 | | - if (!Buffer.isBuffer(chunk) && | 374 | | - 'string' !== typeof chunk && | 375 | | - chunk !== null && | 376 | | - chunk !== undefined && | | 373 | + if (!IS_BUFFER(chunk) && | | 374 | + !IS_STRING(chunk) && | | 375 | + !IS_NULL_OR_UNDEFINED(chunk) && | 377 | 376 | !state.objectMode && | 378 | 377 | !er) { | 379 | 378 | er = new TypeError('Invalid non-string/buffer chunk'); | @@ -775,8 +774,7 @@ Readable.prototype.wrap = function(stream) { | 775 | 774 | // proxy all the other methods. | 776 | 775 | // important when wrapping filters and duplexes. | 777 | 776 | for (var i in stream) { | 778 | | - if (typeof stream[i] === 'function' && | 779 | | - typeof this[i] === 'undefined') { | | 777 | + if (IS_FUNCTION(stream[i]) && IS_UNDEFINED(this[i])) { | 780 | 778 | this[i] = function(method) { return function() { | 781 | 779 | return stream[method].apply(stream, arguments); | 782 | 780 | }}(i); | |
Collapse file Copy file name to clipboardExpand all lines: lib/_stream_transform.js +3-3Lines changed: 3 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -92,7 +92,7 @@ function afterTransform(stream, er, data) { | 92 | 92 | ts.writechunk = null; | 93 | 93 | ts.writecb = null; | 94 | 94 |
| 95 | | - if (data !== null && data !== undefined) | | 95 | + if (!IS_NULL_OR_UNDEFINED(data)) | 96 | 96 | stream.push(data); | 97 | 97 |
| 98 | 98 | if (cb) | @@ -126,7 +126,7 @@ function Transform(options) { | 126 | 126 | this._readableState.sync = false; | 127 | 127 |
| 128 | 128 | this.once('prefinish', function() { | 129 | | - if ('function' === typeof this._flush) | | 129 | + if (IS_FUNCTION(this._flush)) | 130 | 130 | this._flush(function(er) { | 131 | 131 | done(stream, er); | 132 | 132 | }); | @@ -174,7 +174,7 @@ Transform.prototype._write = function(chunk, encoding, cb) { | 174 | 174 | Transform.prototype._read = function(n) { | 175 | 175 | var ts = this._transformState; | 176 | 176 |
| 177 | | - if (ts.writechunk !== null && ts.writecb && !ts.transforming) { | | 177 | + if (!IS_NULL(ts.writechunk) && ts.writecb && !ts.transforming) { | 178 | 178 | ts.transforming = true; | 179 | 179 | this._transform(ts.writechunk, ts.writeencoding, ts.afterTransform); | 180 | 180 | } else { | |
|