-
Notifications
You must be signed in to change notification settings - Fork 30.1k
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
More realistic custom inspect example #8875
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
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) { | ||
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 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 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. 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'); | ||
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); | ||
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 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 + ' >'; | ||
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. I know I already signed off on this but, just as a very minor nit, this could be slightly simplified to: return `${options.stylize('Box', 'special')}<${inner}>`; 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. I am not sure I’d find that more readable… long expressions inside So, maybe 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. Do we really want to mix quasiquoting and string concatenation together? Seems like any option we go with (other than just string concatenation IMO) looks weird. |
||
} | ||
} | ||
|
||
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
Box
example 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