Created
February 3, 2010 18:29
-
-
Save creationix/293860 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
From fa8b7dc1513e03ca014d3ffd234c6d8e0c9e8d5f Mon Sep 17 00:00:00 2001 | |
From: Tim Caswell <[email protected]> | |
Date: Wed, 3 Feb 2010 12:28:05 -0600 | |
Subject: [PATCH] Use the new Object.getOwnPropertyNames to show hidden properties on inspect. Added as a second optional argument to inspect. | |
For example: | |
node> puts(inspect([1,2,3])) | |
[ | |
1, | |
2, | |
3 | |
] | |
node> puts(inspect([1,2,3], true)) | |
[ | |
1, | |
2, | |
3, | |
["length"]: 3 | |
] | |
node> var myobj = Object.create({}, {visible: {value: 1, enumerable: true},hidden: {value: 2}}) | |
{ | |
"visible": 1 | |
} | |
node> puts(inspect(myobj, true)) | |
{ | |
["hidden"]: 2, | |
"visible": 1 | |
} | |
--- | |
lib/sys.js | 36 +++++++++++++++++++++++++----------- | |
test/mjsunit/test-sys.js | 8 +++++++- | |
2 files changed, 32 insertions(+), 12 deletions(-) | |
diff --git a/lib/sys.js b/lib/sys.js | |
index c4b959f..eddab20 100644 | |
--- a/lib/sys.js | |
+++ b/lib/sys.js | |
@@ -22,8 +22,8 @@ exports.error = function (x) { | |
* | |
* @param {Object} value The object to print out | |
*/ | |
-exports.inspect = function (value) { | |
- return formatter(value, '', []); | |
+exports.inspect = function (value, showHidden) { | |
+ return formatter(value, '', [], showHidden); | |
}; | |
exports.p = function (x) { | |
@@ -82,7 +82,7 @@ exports.inherits = process.inherits; | |
* contains all objects above the current one in the heirachy, used to | |
* prevent getting stuck in a loop on circular references | |
*/ | |
-var formatter = function(value, indent, parents) { | |
+var formatter = function(value, indent, parents, showHidden) { | |
if (value instanceof RegExp) return value.toString(); | |
switch(typeof(value)) { | |
case 'string': return JSON.stringify(value); | |
@@ -96,11 +96,14 @@ var formatter = function(value, indent, parents) { | |
parents.push(value); | |
if (value instanceof Array && Object.keys(value).length === value.length) { | |
- return formatObject(value, indent, parents, '[]', function(x, f) { | |
+ return formatObject(value, indent, parents, '[]', function(x, f, hidden) { | |
+ if (hidden) { | |
+ return "[" + f(x) + ']: ' + f(value[x]); | |
+ } | |
return f(value[x]); | |
- }); | |
+ }, showHidden); | |
} else { | |
- return formatObject(value, indent, parents, '{}', function(x, f) { | |
+ return formatObject(value, indent, parents, '{}', function(x, f, hidden) { | |
var child; | |
if (value.__lookupGetter__(x)) { | |
if (value.__lookupSetter__(x)) { | |
@@ -115,8 +118,11 @@ var formatter = function(value, indent, parents) { | |
child = f(value[x]); | |
} | |
} | |
+ if (hidden) { | |
+ return "[" + f(x) + ']: ' + child; | |
+ } | |
return f(x) + ': ' + child; | |
- }); | |
+ }, showHidden); | |
} | |
return buffer; | |
default: | |
@@ -127,16 +133,24 @@ var formatter = function(value, indent, parents) { | |
/** | |
* Helper function for formatting either an array or an object, used internally by formatter | |
*/ | |
-var formatObject = function(obj, indent, parents, parenthesis, entryFormatter) { | |
+var formatObject = function(obj, indent, parents, parenthesis, entryFormatter, showHidden) { | |
var buffer = parenthesis[0]; | |
var values = []; | |
var x; | |
var localFormatter = function(value) { | |
- return formatter(value, indent + ' ', parents); | |
+ return formatter(value, indent + ' ', parents, showHidden); | |
}; | |
- for (x in obj) { | |
- values.push(indent + ' ' + entryFormatter(x, localFormatter)); | |
+ if (showHidden) { | |
+ var visibleKeys = Object.keys(obj); | |
+ var allKeys = Object.getOwnPropertyNames(obj); | |
+ allKeys.forEach(function (x) { | |
+ values.push(indent + ' ' + entryFormatter(x, localFormatter, visibleKeys.indexOf(x.toString()) < 0)); | |
+ }) | |
+ } else { | |
+ for (x in obj) { | |
+ values.push(indent + ' ' + entryFormatter(x, localFormatter)); | |
+ } | |
} | |
if (values.length > 0) { | |
buffer += "\n" + values.join(",\n") + "\n" + indent; | |
diff --git a/test/mjsunit/test-sys.js b/test/mjsunit/test-sys.js | |
index df0422c..306c251 100644 | |
--- a/test/mjsunit/test-sys.js | |
+++ b/test/mjsunit/test-sys.js | |
@@ -24,7 +24,13 @@ assert.equal('{\n "a": [Function]\n}', inspect({a: function() {}})); | |
assert.equal('{\n "a": 1,\n "b": 2\n}', inspect({a: 1, b: 2})); | |
assert.equal('{\n "a": {}\n}', inspect({'a': {}})); | |
assert.equal('{\n "a": {\n "b": 2\n }\n}', inspect({'a': {'b': 2}})); | |
- | |
+assert.equal('[\n 1,\n 2,\n 3,\n ["length"]: 3\n]', inspect([1,2,3], true)); | |
+assert.equal("{\n \"visible\": 1\n}", | |
+ inspect(Object.create({}, {visible:{value:1,enumerable:true},hidden:{value:2}})) | |
+); | |
+assert.equal("{\n [\"hidden\"]: 2,\n \"visible\": 1\n}", | |
+ inspect(Object.create({}, {visible:{value:1,enumerable:true},hidden:{value:2}}), true) | |
+); | |
// Dynamic properties | |
assert.equal( | |
"{\n \"readonly\": [Getter],\n \"readwrite\": [Getter/Setter],\n \"writeonly\": [Setter]\n}", | |
-- | |
1.6.5.2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment