From 55906f9077f1c957d9b3ee9de824c69483f59c2e Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Fri, 19 Aug 2016 00:26:36 +0200 Subject: [PATCH 1/3] util: allow symbol-based custom inspection methods MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add a `util.inspect.custom` Symbol which can be used to customize `util.inspect()` output. Providing `obj[util.inspect.custom]` works like providing `obj.inspect`, except that the former allows avoiding name clashes with other `inspect()` methods. Fixes: https://github.com/nodejs/node/issues/8071 PR-URL: https://github.com/nodejs/node/pull/8174 Reviewed-By: James M Snell Reviewed-By: Michaël Zasso --- doc/api/util.md | 45 ++++++++++++--- lib/buffer.js | 5 +- lib/internal/util.js | 4 ++ lib/util.js | 29 ++++++---- test/parallel/test-util-inspect.js | 91 +++++++++++++++++++++++++++--- 5 files changed, 142 insertions(+), 32 deletions(-) diff --git a/doc/api/util.md b/doc/api/util.md index 662c360359a307..4b056a96049135 100644 --- a/doc/api/util.md +++ b/doc/api/util.md @@ -234,18 +234,19 @@ Predefined color codes are: `white`, `grey`, `black`, `blue`, `cyan`, `green`, `magenta`, `red` and `yellow`. There are also `bold`, `italic`, `underline` and `inverse` codes. -### Custom `inspect()` function on Objects +### Custom inspection functions on Objects -Objects also may define their own `inspect(depth)` function which `util.inspect()` -will invoke and use the result of when inspecting the object: +Objects may also define their own `[util.inspect.custom](depth, opts)` +(or, equivalently `inspect(depth, opts)`) function that `util.inspect()` will +invoke and use the result of when inspecting the object: ```js const util = require('util'); -var obj = { name: 'nate' }; -obj.inspect = function(depth) { +const obj = { name: 'nate' }; +obj[util.inspect.custom] = function(depth) { return `{${this.name}}`; }; @@ -253,12 +254,29 @@ util.inspect(obj); // "{nate}" ``` -You may also return another Object entirely, and the returned String will be -formatted according to the returned Object. This is similar to how -`JSON.stringify()` works: +Custom `[util.inspect.custom](depth, opts)` functions typically return a string +but may return a value of any type that will be formatted accordingly by +`util.inspect()`. + +```js +const util = require('util'); + +const obj = { foo: 'this will not show up in the inspect() output' }; +obj[util.inspect.custom] = function(depth) { + return { bar: 'baz' }; +}; + +util.inspect(obj); + // "{ bar: 'baz' }" +``` + +A custom inspection method can alternatively be provided by exposing +an `inspect(depth, opts)` method on the object: ```js -var obj = { foo: 'this will not show up in the inspect() output' }; +const util = require('util'); + +const obj = { foo: 'this will not show up in the inspect() output' }; obj.inspect = function(depth) { return { bar: 'baz' }; }; @@ -267,6 +285,14 @@ util.inspect(obj); // "{ bar: 'baz' }" ``` +### util.inspect.custom + + +A Symbol that can be used to declare custom inspect functions, see +[Custom inspection functions on Objects][]. + ## util.isArray(object)