diff options
Diffstat (limited to 'lib/util.js')
-rw-r--r-- | lib/util.js | 71 |
1 files changed, 34 insertions, 37 deletions
diff --git a/lib/util.js b/lib/util.js index fae4593867..993e3632e5 100644 --- a/lib/util.js +++ b/lib/util.js @@ -21,7 +21,7 @@ var formatRegExp = /%[sdj%]/g; exports.format = function(f) { - if (typeof f !== 'string') { + if (!IS_STRING(f)) { var objects = []; for (var i = 0; i < arguments.length; i++) { objects.push(inspect(arguments[i])); @@ -44,7 +44,7 @@ exports.format = function(f) { } }); for (var x = args[i]; i < len; x = args[++i]) { - if (x === null || typeof x !== 'object') { + if (IS_NULL(x) || !IS_OBJECT(x)) { str += ' ' + x; } else { str += ' ' + inspect(x); @@ -117,7 +117,7 @@ function inspect(obj, opts) { // legacy... if (arguments.length >= 3) ctx.depth = arguments[2]; if (arguments.length >= 4) ctx.colors = arguments[3]; - if (typeof opts === 'boolean') { + if (IS_BOOLEAN(opts)) { // legacy... ctx.showHidden = opts; } else if (opts) { @@ -125,10 +125,10 @@ function inspect(obj, opts) { exports._extend(ctx, opts); } // set default options - if (typeof ctx.showHidden === 'undefined') ctx.showHidden = false; - if (typeof ctx.depth === 'undefined') ctx.depth = 2; - if (typeof ctx.colors === 'undefined') ctx.colors = false; - if (typeof ctx.customInspect === 'undefined') ctx.customInspect = true; + if (IS_UNDEFINED(ctx.showHidden)) ctx.showHidden = false; + if (IS_UNDEFINED(ctx.depth)) ctx.depth = 2; + if (IS_UNDEFINED(ctx.colors)) ctx.colors = false; + if (IS_UNDEFINED(ctx.customInspect)) ctx.customInspect = true; if (ctx.colors) ctx.stylize = stylizeWithColor; return formatValue(ctx, obj, ctx.depth); } @@ -197,13 +197,15 @@ function arrayToHash(array) { function formatValue(ctx, value, recurseTimes) { // Provide a hook for user-specified inspect functions. // Check that value is an object with an inspect function on it - if (ctx.customInspect && value && typeof value.inspect === 'function' && + if (ctx.customInspect && + value && + IS_FUNCTION(value.inspect) && // Filter out the util module, it's inspect function is special value.inspect !== exports.inspect && // Also filter out any prototype objects using the circular check. !(value.constructor && value.constructor.prototype === value)) { var ret = value.inspect(recurseTimes); - if ('string' !== typeof ret) { + if (!IS_STRING(ret)) { ret = formatValue(ctx, ret, recurseTimes); } return ret; @@ -225,7 +227,7 @@ function formatValue(ctx, value, recurseTimes) { // Some type of object without properties can be shortcutted. if (keys.length === 0) { - if (typeof value === 'function') { + if (IS_FUNCTION(value)) { var name = value.name ? ': ' + value.name : ''; return ctx.stylize('[Function' + name + ']', 'special'); } @@ -249,7 +251,7 @@ function formatValue(ctx, value, recurseTimes) { } // Make functions say that they are functions - if (typeof value === 'function') { + if (IS_FUNCTION(value)) { var n = value.name ? ': ' + value.name : ''; base = ' [Function' + n + ']'; } @@ -299,26 +301,21 @@ function formatValue(ctx, value, recurseTimes) { function formatPrimitive(ctx, value) { - switch (typeof value) { - case 'undefined': - return ctx.stylize('undefined', 'undefined'); - - case 'string': - var simple = '\'' + JSON.stringify(value).replace(/^"|"$/g, '') - .replace(/'/g, "\\'") - .replace(/\\"/g, '"') + '\''; - return ctx.stylize(simple, 'string'); - - case 'number': - return ctx.stylize('' + value, 'number'); - - case 'boolean': - return ctx.stylize('' + value, 'boolean'); - } + if (IS_UNDEFINED(value)) + return ctx.stylize('undefined', 'undefined'); + if (IS_STRING(value)) { + var simple = '\'' + JSON.stringify(value).replace(/^"|"$/g, '') + .replace(/'/g, "\\'") + .replace(/\\"/g, '"') + '\''; + return ctx.stylize(simple, 'string'); + } + if (IS_NUMBER(value)) + return ctx.stylize('' + value, 'number'); + if (IS_BOOLEAN(value)) + return ctx.stylize('' + value, 'boolean'); // For some reason typeof null is "object", so special case here. - if (value === null) { + if (IS_NULL(value)) return ctx.stylize('null', 'null'); - } } @@ -366,7 +363,7 @@ function formatProperty(ctx, value, recurseTimes, visibleKeys, key, array) { } if (!str) { if (ctx.seen.indexOf(desc.value) < 0) { - if (recurseTimes === null) { + if (IS_NULL(recurseTimes)) { str = formatValue(ctx, desc.value, null); } else { str = formatValue(ctx, desc.value, recurseTimes - 1); @@ -386,7 +383,7 @@ function formatProperty(ctx, value, recurseTimes, visibleKeys, key, array) { str = ctx.stylize('[Circular]', 'special'); } } - if (typeof name === 'undefined') { + if (IS_UNDEFINED(name)) { if (array && key.match(/^\d+$/)) { return str; } @@ -430,25 +427,25 @@ function reduceToSingleString(output, base, braces) { // NOTE: These type checking functions intentionally don't use `instanceof` // because it is fragile and can be easily faked with `Object.create()`. function isArray(ar) { - return Array.isArray(ar); + return IS_ARRAY(ar); } exports.isArray = isArray; function isRegExp(re) { - return typeof re === 'object' && objectToString(re) === '[object RegExp]'; + return IS_OBJECT(re) && objectToString(re) === '[object RegExp]'; } exports.isRegExp = isRegExp; function isDate(d) { - return typeof d === 'object' && objectToString(d) === '[object Date]'; + return IS_OBJECT(d) && objectToString(d) === '[object Date]'; } exports.isDate = isDate; function isError(e) { - return typeof e === 'object' && objectToString(e) === '[object Error]'; + return IS_OBJECT(e) && objectToString(e) === '[object Error]'; } exports.isError = isError; @@ -509,7 +506,7 @@ exports.inherits = function(ctor, superCtor) { exports._extend = function(origin, add) { // Don't do anything if add isn't an object - if (!add || typeof add !== 'object') return origin; + if (!add || !IS_OBJECT(add)) return origin; var keys = Object.keys(add); var i = keys.length; @@ -604,7 +601,7 @@ exports.pump = exports.deprecate(function(readStream, writeStream, callback) { var uv; exports._errnoException = function(err, syscall) { - if (typeof uv === 'undefined') uv = process.binding('uv'); + if (IS_UNDEFINED(uv)) uv = process.binding('uv'); var errname = uv.errname(err); var e = new Error(syscall + ' ' + errname); e.code = errname; |