Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 36.4k
More realistic custom inspect example#8875
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.
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -286,13 +286,31 @@ invoke and use the result of when inspecting the object: | ||
| ```js | ||
| const util = require('util'); | ||
| const obj = { name: 'nate' }; | ||
| obj[util.inspect.custom] = function(depth) { | ||
| return `{${this.name}}`; | ||
| }; | ||
| class Box { | ||
| constructor(value) { | ||
| this.value = value; | ||
| } | ||
| util.inspect(obj); | ||
| // "{nate}" | ||
| inspect(depth, options) { | ||
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @addaleax ... what do you think.. should this be modified to use the new symbol you introduced as an alternative to using Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jasnell We could do that, but the motivation for introducing the symbol-based alternative were objects that have a regular Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ContributorAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will do when I have time and energy. | ||
| if (depth < 0) { | ||
| return options.stylize('[Box]', 'special'); | ||
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Havvy ... likewise here, a comment that describes briefly what | ||
| } | ||
| const newOptions = Object.assign({}, options, { | ||
| depth: options.depth === null ? null : options.depth - 1 | ||
| }); | ||
| // Five space padding because that's the size of "Box< ". | ||
| const padding = ' '.repeat(5); | ||
| const inner = util.inspect(this.value, newOptions).replace(/\n/g, '\n' + padding); | ||
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And a comment here about what this call to ContributorAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will do when I have time and energy. | ||
| return options.stylize('Box', 'special') + '< ' + inner + ' >'; | ||
| ||
| } | ||
| } | ||
| const box = new Box(true); | ||
| util.inspect(box); | ||
| // "Box< true >" | ||
| ``` | ||
| Custom `[util.inspect.custom](depth, opts)` functions typically return a string | ||
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.
I’m not sure, so this is just a suggestion, but maybe leaving the “simpler” example first would be helpful? The
Boxexample is good but maybe a bit overwhelming on the first look?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.
If we keep it, then I'd argue that we'd want to also add text saying it's a minimal example.
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.
@Havvy Agreed. Maybe subheadings that say something like:
A simple example
A more realistic example