doc: add esm examples to node:util
#56793
Open
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.
This PR adds the missing
ESM
counterparts of theCJS
examples for the Util documentation.I also destructured the (
require
) imports for the currentCJS
examples following a feedback I had received in previous PRs.For the
numericSeparator
example ofutil.inspect(object[, showHidden[, depth[, colors]]])
I took the liberty to leave the numbers like this:Instead of:
Because that's actually what we want to showcase, how
numericSeparator
would add the missing underscores to print:For the
Box
andutil.inspect.custom
examples at Custom inspection functions on objects I added a finalconsole.log
so users don't have to add it themselves when copy/pasting the example to see an output.For the
util.inspect.defaultOptions
example I incremented the number of 0's to be added just so the output is more dramatic and it would showcase the function better imo, it would also print a square on the terminal which looks pretty awesome:For the
promisify()
class methods that use this example atutil.promisify(original)
, the one with:I updated the error message, since now it's:
TypeError: Cannot read properties of undefined (reading 'a')
instead ofTypeError: Cannot read property 'a' of undefined
.Lastly, for the
util.styleText(format, text[, options])
there was a typo on the example that recently got fixed but only for theESM
part, this adds theCJS
fix for that typo.I think that's all, tell me what you think and I can update/change things around!
I've tested every single example and they all work/behave as expected.
As always, best regards 💚
Edit:
For the
util.deprecate(fn, msg[, code])
example (the second one) I also added the functions and messages so users can just copy/paste that portion of code and run it without encountering errors.