Permalink
Browse files

wip

  • Loading branch information...
TooTallNate committed Oct 14, 2012
1 parent 4b238b4 commit 336c8c4c240349c52101890177f5aafacd47d782
Showing with 39 additions and 16 deletions.
  1. +8 −3 doc/api/util.markdown
  2. +16 −13 lib/util.js
  3. +15 −0 test/simple/test-util-inspect.js
View
@@ -100,6 +100,7 @@ via `util.inspect.styles` and `util.inspect.colors` objects.
`util.inspect.styles` is a map assigning each style a color
from `util.inspect.colors`.
Highlighted styles and their default values are:
+
* `number` (yellow)
* `boolean` (yellow)
* `string` (green)
@@ -114,13 +115,17 @@ Predefined color codes are: `white`, `grey`, `black`, `blue`, `cyan`,
`green`, `magenta`, `red` and `yellow`.
There are also `bold`, `italic`, `underline` and `inverse` codes.
-Objects also may define their own `inspect(depth)` function which `util.inspect()`
-will invoke and use the result of when inspecting the object:
+### Custom `inspect()` output for objects
+
+Objects also may define their own `inspect(options)` function which
+`util.inspect()` will invoke and use the result of when inspecting
+the object. The function gets passed the `options` objects that
+specifies the options used:
var util = require('util');
var obj = { name: 'nate' };
- obj.inspect = function(depth) {
+ obj.inspect = function(options) {
return '{' + this.name + '}';
};
View
@@ -199,15 +199,15 @@ function arrayToHash(array) {
}
-function formatValue(ctx, value, recurseTimes) {
+function formatValue(ctx, value) {
// 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' &&
// 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)) {
- return String(value.inspect(recurseTimes));
+ return String(value.inspect(ctx));
}
// Primitive types cannot have properties
@@ -274,7 +274,7 @@ function formatValue(ctx, value, recurseTimes) {
return braces[0] + base + braces[1];
}
- if (recurseTimes < 0) {
+ if (ctx.depth < 0) {
if (isRegExp(value)) {
return ctx.stylize(RegExp.prototype.toString.call(value), 'regexp');
} else {
@@ -286,10 +286,10 @@ function formatValue(ctx, value, recurseTimes) {
var output;
if (array) {
- output = formatArray(ctx, value, recurseTimes, visibleKeys, keys);
+ output = formatArray(ctx, value, visibleKeys, keys);
} else {
output = keys.map(function(key) {
- return formatProperty(ctx, value, recurseTimes, visibleKeys, key, array);
+ return formatProperty(ctx, value, visibleKeys, key, array);
});
}
@@ -328,28 +328,28 @@ function formatError(value) {
}
-function formatArray(ctx, value, recurseTimes, visibleKeys, keys) {
+function formatArray(ctx, value, visibleKeys, keys) {
var output = [];
for (var i = 0, l = value.length; i < l; ++i) {
if (hasOwnProperty(value, String(i))) {
- output.push(formatProperty(ctx, value, recurseTimes, visibleKeys,
+ output.push(formatProperty(ctx, value, visibleKeys,
String(i), true));
} else {
output.push('');
}
}
keys.forEach(function(key) {
if (!key.match(/^\d+$/)) {
- output.push(formatProperty(ctx, value, recurseTimes, visibleKeys,
+ output.push(formatProperty(ctx, value, visibleKeys,
key, true));
}
});
return output;
}
-function formatProperty(ctx, value, recurseTimes, visibleKeys, key, array) {
- var name, str, desc;
+function formatProperty(ctx, value, visibleKeys, key, array) {
+ var name, str, desc, depth;
desc = Object.getOwnPropertyDescriptor(value, key) || { value: value[key] };
if (desc.get) {
if (desc.set) {
@@ -367,10 +367,13 @@ function formatProperty(ctx, value, recurseTimes, visibleKeys, key, array) {
}
if (!str) {
if (ctx.seen.indexOf(desc.value) < 0) {
- if (recurseTimes === null) {
- str = formatValue(ctx, desc.value, null);
+ depth = ctx.depth;
+ if (depth === null) {
+ str = formatValue(ctx, desc.value);
} else {
- str = formatValue(ctx, desc.value, recurseTimes - 1);
+ ctx.depth = depth - 1;
+ str = formatValue(ctx, desc.value);
+ ctx.depth = depth;
}
if (str.indexOf('\n') > -1) {
if (array) {
@@ -157,3 +157,18 @@ assert(util.inspect(subject, { customInspect: true }).indexOf('123') !== -1);
assert(util.inspect(subject, { customInspect: true }).indexOf('inspect') === -1);
assert(util.inspect(subject, { customInspect: false }).indexOf('123') === -1);
assert(util.inspect(subject, { customInspect: false }).indexOf('inspect') !== -1);
+
+// custom "inspect()" functions get the options object
+subject = {};
+subject.a = {};
+subject.a.b = {};
+subject.a.b.foo = 'bar';
+subject.a.b.inspect = function(options) {
+ if (options.depth === -1) { return '[Obj]'; }
+ return JSON.stringify(options);
+};
+
+//assert(util.inspect(subject, { colors: true }).indexOf());
+console.log(util.inspect(subject, { colors: true, depth: 2 }));
+console.log(util.inspect(subject, { depth: 1 }));
+console.log(util.inspect(subject, { depth: 0 }));

0 comments on commit 336c8c4

Please sign in to comment.