Skip to content

Commit

Permalink
Use duck-typing as well as instanceof in sys.inspect
Browse files Browse the repository at this point in the history
This makes it so that inspecting objects from other contexts works as expected.
  • Loading branch information
isaacs authored and ry committed May 29, 2010
1 parent 2fa4de0 commit 3c7873b
Showing 1 changed file with 30 additions and 7 deletions.
37 changes: 30 additions & 7 deletions lib/sys.js
Expand Up @@ -65,21 +65,21 @@ exports.inspect = function (obj, showHidden, depth) {

// Functions without properties can be shortcutted.
if (typeof value === 'function' && keys.length === 0) {
if (value instanceof RegExp) {
if (isRegExp(value)) {
return '' + value;
} else {
return '[Function]';
}
}

// Dates without properties can be shortcutted
if (value instanceof Date && keys.length === 0) {
if (isDate(value) && keys.length === 0) {
return value.toUTCString();
}

var base, type, braces;
// Determine the object type
if (value instanceof Array) {
if (isArray(value)) {
type = 'Array';
braces = ["[", "]"];
} else {
Expand All @@ -89,13 +89,13 @@ exports.inspect = function (obj, showHidden, depth) {

// Make functions say that they are functions
if (typeof value === 'function') {
base = (value instanceof RegExp) ? ' ' + value : ' [Function]';
base = (isRegExp(value)) ? ' ' + value : ' [Function]';
} else {
base = "";
}

// Make dates with properties first say the date
if (value instanceof Date) {
if (isDate(value)) {
base = ' ' + value.toUTCString();
}

Expand All @@ -106,7 +106,7 @@ exports.inspect = function (obj, showHidden, depth) {
}

if (recurseTimes < 0) {
if (value instanceof RegExp) {
if (isRegExp(value)) {
return '' + value;
} else {
return "[Object]";
Expand Down Expand Up @@ -140,7 +140,7 @@ exports.inspect = function (obj, showHidden, depth) {
str = format(value[key], recurseTimes - 1);
}
if (str.indexOf('\n') > -1) {
if (value instanceof Array) {
if (isArray(value)) {
str = str.split('\n').map(function(line) {
return ' ' + line;
}).join('\n').substr(2);
Expand Down Expand Up @@ -191,6 +191,29 @@ exports.inspect = function (obj, showHidden, depth) {
}
return format(obj, (typeof depth === 'undefined' ? 2 : depth));
};
function isArray (ar) {
return ar instanceof Array
|| Array.isArray(ar)
|| (ar && ar !== Object.prototype && isArray(ar.__proto__));
}
function isRegExp (re) {
var s = ""+re;
return re instanceof RegExp // easy case
|| typeof(re) === "function" // duck-type for context-switching evalcx case
&& re.constructor.name === "RegExp"
&& re.compile
&& re.test
&& re.exec
&& s.charAt(0) === "/"
&& s.substr(-1) === "/";
}
function isDate (d) {
if (d instanceof Date) return true;
if (typeof d !== "object") return false;
var properties = Date.prototype && Object.getOwnPropertyNames(Date.prototype);
var proto = d.__proto__ && Object.getOwnPropertyNames(d.__proto__);
return JSON.stringify(proto) === JSON.stringify(properties);
}

var pWarning;

Expand Down

0 comments on commit 3c7873b

Please sign in to comment.