GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — util: use a shared symbol for util.inspect.custom by chocolateboy · Pull Request #20857 · nodejs/node

util: use a shared symbol for util.inspect.custom #20857

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Sign up for GitHub

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
chocolateboy wants to merge 12 commits into nodejs:master from chocolateboy:shared-util-inspect-custom

Uh oh!

There was an error while loading. Please reload this page.

Closed

util: use a shared symbol for util.inspect.custom #20857

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
12 commits
Select commit Hold shift + click to select a range
54d8720
util: use a shared symbol for util.inspect.custom
chocolateboy May 20, 2018
e3f495b
docstyle: change example variable name from INSPECT -> inspect
chocolateboy May 20, 2018
3d51309
doctweak: add link to MDN re: `Symbol.for` and the global symbol regi…
chocolateboy May 20, 2018
1b5cce5
doctweak: move inline link to link section at the bottom of the file
chocolateboy May 20, 2018
51c1854
doctweak: make the role of the `inspect` hook clearer in the example
chocolateboy May 20, 2018
53fb3ef
doctweak: remove example of non-Node.js (browser) usage
chocolateboy May 20, 2018
e09a356
docfix: add a `changes` section
chocolateboy May 20, 2018
646dfe8
doctweak: add a link to the symbol to the inspect-hook section
chocolateboy May 21, 2018
dda610d
doctweak: tweak the util.inspect.custom wording
chocolateboy May 21, 2018
8e3f30f
docfix: sort the links ASCIIbetically
chocolateboy May 21, 2018
fa5c8c6
docfix: fix formatting
chocolateboy May 22, 2018
ae8e27b
namespace the util.inspect.custom shared symbol:
chocolateboy Sep 7, 2018
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

46 changes: 40 additions & 6 deletions 46 doc/api/util.md
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -569,9 +569,10 @@ terminals.

<!-- type=misc -->

Objects may also define their own `[util.inspect.custom](depth, opts)` function
that `util.inspect()` will invoke and use the result of when inspecting the
object:
Objects may also define their own
[`[util.inspect.custom](depth, opts)`][util.inspect.custom] function,
which `util.inspect()` will invoke and use the result of when inspecting
the object:

```js
const util = require('util');
Expand Down Expand Up @@ -623,10 +624,41 @@ util.inspect(obj);
### util.inspect.custom
<!-- YAML
added: v6.6.0
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/20857
description: This is now defined as a shared symbol.
-->

* {symbol} that can be used to declare custom inspect functions, see
[Custom inspection functions on Objects][].
* {symbol} that can be used to declare custom inspect functions.

In addition to being accessible through `util.inspect.custom`, this
symbol is [registered globally][global symbol registry] and can be
accessed in any environment as `Symbol.for('nodejs.util.inspect.custom')`.

```js
const inspect = Symbol.for('nodejs.util.inspect.custom');

class Password {
constructor(value) {
this.value = value;
}

toString() {
return 'xxxxxxxx';
}

[inspect]() {
return `Password <${this.toString()}>`;
}
}

const password = new Password('r0sebud');
console.log(password);
// Prints Password <xxxxxxxx>
Comment thread Outdated

@vsemozhetbyt vsemozhetbyt May 22, 2018 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe // Prints "Password <xxxxxxxx>". ?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@chocolateboy chocolateboy May 22, 2018

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would have been my preference, but most other Prints comments don't do that.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@vsemozhetbyt vsemozhetbyt May 22, 2018

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, maybe this unification is a task for some other PR)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
```

See [Custom inspection functions on Objects][] for more details.

### util.inspect.defaultOptions
<!-- YAML
Expand Down Expand Up @@ -2071,7 +2103,6 @@ Deprecated predecessor of `console.log`.
[`Array.isArray()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray
[`ArrayBuffer`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer
[`ArrayBuffer.isView()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer/isView
[async function]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function
[`assert.deepStrictEqual()`]: assert.html#assert_assert_deepstrictequal_actual_expected_message
[`Buffer.isBuffer()`]: buffer.html#buffer_class_method_buffer_isbuffer_obj
[`console.error()`]: console.html#console_console_error_data_args
Expand Down Expand Up @@ -2113,6 +2144,9 @@ Deprecated predecessor of `console.log`.
[Module Namespace Object]: https://tc39.github.io/ecma262/#sec-module-namespace-exotic-objects
[WHATWG Encoding Standard]: https://encoding.spec.whatwg.org/
[Common System Errors]: errors.html#errors_common_system_errors
[async function]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function
[constructor]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/constructor
[global symbol registry]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/for
[list of deprecated APIS]: deprecations.html#deprecations_list_of_deprecated_apis
[semantically incompatible]: https://github.com/nodejs/node/issues/4179
[util.inspect.custom]: #util_util_inspect_custom
2 changes: 1 addition & 1 deletion 2 lib/internal/util.js
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ module.exports = {

// Symbol used to provide a custom inspect function for an object as an
// alternative to using 'inspect'
customInspectSymbol: Symbol('util.inspect.custom'),
customInspectSymbol: Symbol.for('nodejs.util.inspect.custom'),

// Used by the buffer module to capture an internal reference to the
// default isEncoding implementation, just in case userland overrides it.
Expand Down
2 changes: 1 addition & 1 deletion 2 test/parallel/test-assert.js
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,7 @@ assert.throws(
'+ {}\n' +
'- {\n' +
"- loop: 'forever',\n" +
'- [Symbol(util.inspect.custom)]: [Function]\n' +
'- [Symbol(nodejs.util.inspect.custom)]: [Function]\n' +
'- }'
});

Expand Down
8 changes: 4 additions & 4 deletions 8 test/parallel/test-console.js
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -214,11 +214,11 @@ for (const expected of expectedStrings) {
}

assert.strictEqual(strings.shift(),
"{ foo: 'bar',\n [Symbol(util.inspect.custom)]: " +
'[Function: [util.inspect.custom]] }\n');
"{ foo: 'bar',\n [Symbol(nodejs.util.inspect.custom)]: " +
'[Function: [nodejs.util.inspect.custom]] }\n');
assert.strictEqual(strings.shift(),
"{ foo: 'bar',\n [Symbol(util.inspect.custom)]: " +
'[Function: [util.inspect.custom]] }\n');
"{ foo: 'bar',\n [Symbol(nodejs.util.inspect.custom)]: " +
'[Function: [nodejs.util.inspect.custom]] }\n');
assert.ok(strings.shift().includes('foo: [Object]'));
assert.strictEqual(strings.shift().includes('baz'), false);
assert.strictEqual(strings.shift(), 'inspect inspect\n');
Expand Down
20 changes: 17 additions & 3 deletions 20 test/parallel/test-util-inspect.js
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,7 @@ assert.strictEqual(util.inspect(Object.create(Date.prototype)), 'Date {}');
{
const x = { [util.inspect.custom]: util.inspect };
assert(util.inspect(x).includes(
'[Symbol(util.inspect.custom)]:\n { [Function: inspect]'));
'[Symbol(nodejs.util.inspect.custom)]:\n { [Function: inspect]'));
}

// `util.inspect` should display the escaped value of a key.
Expand Down Expand Up @@ -758,12 +758,26 @@ util.inspect({ hasOwnProperty: null });
};

util.inspect(subject, { customInspectOptions: true });

// util.inspect.custom is a shared symbol which can be accessed as
// Symbol.for("nodejs.util.inspect.custom").
const inspect = Symbol.for('nodejs.util.inspect.custom');

subject[inspect] = () => ({ baz: 'quux' });

assert.strictEqual(util.inspect(subject), '{ baz: \'quux\' }');

subject[inspect] = (depth, opts) => {
assert.strictEqual(opts.customInspectOptions, true);
};

util.inspect(subject, { customInspectOptions: true });
}

{
// Returning `this` from a custom inspection function works.
const subject = { a: 123, [util.inspect.custom]() { return this; } };
const UIC = 'util.inspect.custom';
const UIC = 'nodejs.util.inspect.custom';
assert.strictEqual(util.inspect(subject),
`{ a: 123,\n [Symbol(${UIC})]: [Function: [${UIC}]] }`);
}
Expand Down Expand Up @@ -1188,7 +1202,7 @@ util.inspect(process);
const obj = { [util.inspect.custom]: 'fhqwhgads' };
assert.strictEqual(
util.inspect(obj),
"{ [Symbol(util.inspect.custom)]: 'fhqwhgads' }"
"{ [Symbol(nodejs.util.inspect.custom)]: 'fhqwhgads' }"
);
}

Expand Down
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.