|
|
@ -34,9 +34,50 @@ var error = exports.error = function (x) { |
|
|
|
* @param {Object} value The object to print out |
|
|
|
* @param {Boolean} showHidden Flag that shows hidden (not enumerable) |
|
|
|
* properties of objects. |
|
|
|
* @param {Number} depth Depth in which to descend in object. Default is 2. |
|
|
|
* @param {Boolean} colors Flag to turn on ANSI escape codes to color the |
|
|
|
* output. Default is false (no coloring). |
|
|
|
*/ |
|
|
|
exports.inspect = function (obj, showHidden, depth) { |
|
|
|
exports.inspect = function (obj, showHidden, depth, colors) { |
|
|
|
var seen = []; |
|
|
|
|
|
|
|
var stylize = function (str, styleType) { |
|
|
|
// http://en.wikipedia.org/wiki/ANSI_escape_code#graphics
|
|
|
|
var styles = { 'bold' : [1, 22] |
|
|
|
, 'italic' : [3, 23] |
|
|
|
, 'underline' : [4, 24] |
|
|
|
, 'inverse' : [7, 27] |
|
|
|
, 'white' : [37, 39] |
|
|
|
, 'grey' : [90, 39] |
|
|
|
, 'black' : [30, 39] |
|
|
|
, 'blue' : [34, 39] |
|
|
|
, 'cyan' : [36, 39] |
|
|
|
, 'green' : [32, 39] |
|
|
|
, 'magenta' : [35, 39] |
|
|
|
, 'red' : [31, 39] |
|
|
|
, 'yellow' : [33, 39] |
|
|
|
}; |
|
|
|
var style = { "special": "grey" |
|
|
|
, "number": "blue" |
|
|
|
, "boolean": "blue" |
|
|
|
, "undefined": "red" |
|
|
|
, "null": "red" |
|
|
|
, "string": "green" |
|
|
|
, "date": "magenta" |
|
|
|
//, "name": intentionally not styling
|
|
|
|
, "regexp": "cyan" |
|
|
|
}[styleType]; |
|
|
|
if (style) { |
|
|
|
return '\033[' + styles[style][0] + 'm' + str + |
|
|
|
'\033[' + styles[style][1] + 'm'; |
|
|
|
} else { |
|
|
|
return str; |
|
|
|
} |
|
|
|
}; |
|
|
|
if (! colors) { |
|
|
|
stylize = function(str, styleType) { return str; }; |
|
|
|
} |
|
|
|
|
|
|
|
function format(value, recurseTimes) { |
|
|
|
// Provide a hook for user-specified inspect functions.
|
|
|
|
// Check that value is an object with an inspect function on it
|
|
|
@ -50,16 +91,18 @@ exports.inspect = function (obj, showHidden, depth) { |
|
|
|
|
|
|
|
// Primitive types cannot have properties
|
|
|
|
switch (typeof value) { |
|
|
|
case 'undefined': return 'undefined'; |
|
|
|
case 'string': return JSON.stringify(value).replace(/'/g, "\\'") |
|
|
|
case 'undefined': return stylize('undefined', 'undefined'); |
|
|
|
case 'string': return stylize( |
|
|
|
JSON.stringify(value).replace(/'/g, "\\'") |
|
|
|
.replace(/\\"/g, '"') |
|
|
|
.replace(/(^"|"$)/g, "'"); |
|
|
|
case 'number': return '' + value; |
|
|
|
case 'boolean': return '' + value; |
|
|
|
.replace(/(^"|"$)/g, "'"), |
|
|
|
'string'); |
|
|
|
case 'number': return stylize('' + value, 'number'); |
|
|
|
case 'boolean': return stylize('' + value, 'boolean'); |
|
|
|
} |
|
|
|
// For some reason typeof null is "object", so special case here.
|
|
|
|
if (value === null) { |
|
|
|
return 'null'; |
|
|
|
return stylize('null', 'null'); |
|
|
|
} |
|
|
|
|
|
|
|
// Look up the keys of the object.
|
|
|
@ -74,15 +117,15 @@ exports.inspect = function (obj, showHidden, depth) { |
|
|
|
// Functions without properties can be shortcutted.
|
|
|
|
if (typeof value === 'function' && keys.length === 0) { |
|
|
|
if (isRegExp(value)) { |
|
|
|
return '' + value; |
|
|
|
return stylize('' + value, 'regexp'); |
|
|
|
} else { |
|
|
|
return '[Function]'; |
|
|
|
return stylize('[Function]', 'special'); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// Dates without properties can be shortcutted
|
|
|
|
if (isDate(value) && keys.length === 0) { |
|
|
|
return value.toUTCString(); |
|
|
|
return stylize(value.toUTCString(), 'date'); |
|
|
|
} |
|
|
|
|
|
|
|
var base, type, braces; |
|
|
@ -115,9 +158,9 @@ exports.inspect = function (obj, showHidden, depth) { |
|
|
|
|
|
|
|
if (recurseTimes < 0) { |
|
|
|
if (isRegExp(value)) { |
|
|
|
return '' + value; |
|
|
|
return stylize('' + value, "regexp"); |
|
|
|
} else { |
|
|
|
return "[Object]"; |
|
|
|
return stylize("[Object]", "special"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
@ -126,13 +169,13 @@ exports.inspect = function (obj, showHidden, depth) { |
|
|
|
if (value.__lookupGetter__) { |
|
|
|
if (value.__lookupGetter__(key)) { |
|
|
|
if (value.__lookupSetter__(key)) { |
|
|
|
str = "[Getter/Setter]"; |
|
|
|
str = stylize("[Getter/Setter]", "special"); |
|
|
|
} else { |
|
|
|
str = "[Getter]"; |
|
|
|
str = stylize("[Getter]", "special"); |
|
|
|
} |
|
|
|
} else { |
|
|
|
if (value.__lookupSetter__(key)) { |
|
|
|
str = "[Setter]"; |
|
|
|
str = stylize("[Setter]", "special"); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -160,7 +203,7 @@ exports.inspect = function (obj, showHidden, depth) { |
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
str = '[Circular]'; |
|
|
|
str = stylize('[Circular]', 'special'); |
|
|
|
} |
|
|
|
} |
|
|
|
if (typeof name === 'undefined') { |
|
|
@ -170,11 +213,13 @@ exports.inspect = function (obj, showHidden, depth) { |
|
|
|
name = JSON.stringify('' + key); |
|
|
|
if (name.match(/^"([a-zA-Z_][a-zA-Z_0-9]*)"$/)) { |
|
|
|
name = name.substr(1, name.length-2); |
|
|
|
name = stylize(name, "name"); |
|
|
|
} |
|
|
|
else { |
|
|
|
name = name.replace(/'/g, "\\'") |
|
|
|
.replace(/\\"/g, '"') |
|
|
|
.replace(/(^"|"$)/g, "'"); |
|
|
|
name = stylize(name, "string"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|