Skip to content
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

doc: add esm examples to node:util #56793

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

mfdebian
Copy link
Contributor

@mfdebian mfdebian commented Jan 27, 2025

This PR adds the missing ESM counterparts of the CJS examples for the Util documentation.

I also destructured the (require) imports for the current CJS examples following a feedback I had received in previous PRs.

For the numericSeparator example of util.inspect(object[, showHidden[, depth[, colors]]]) I took the liberty to leave the numbers like this:

const thousand = 1000;
const million = 1000000;
...

Instead of:

const thousand = 1_000;
const million = 1_000_000;
...

Because that's actually what we want to showcase, how numericSeparator would add the missing underscores to print:

1_000
1_000_000
...

For the Box and util.inspect.custom examples at Custom inspection functions on objects I added a final console.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:

[
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0,
  ... 56 more items
]
[
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
]

For the promisify() class methods that use this example at util.promisify(original), the one with:

class Foo {
  constructor() {
    this.a = 42;
  }

  bar(callback) {
    callback(null, this.a);
  }
}
...

I updated the error message, since now it's: TypeError: Cannot read properties of undefined (reading 'a') instead of TypeError: 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 the ESM part, this adds the CJS 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.

@nodejs-github-bot nodejs-github-bot added doc Issues and PRs related to the documentations. util Issues and PRs related to the built-in util module. labels Jan 27, 2025
Copy link
Member

@AugustinMauroy AugustinMauroy left a comment

Choose a reason for hiding this comment

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

Why do we need destructed object ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
doc Issues and PRs related to the documentations. util Issues and PRs related to the built-in util module.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants