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

[node:util] format is optional in util.format? #45669

Closed
Semigradsky opened this issue Nov 29, 2022 · 7 comments
Closed

[node:util] format is optional in util.format? #45669

Semigradsky opened this issue Nov 29, 2022 · 7 comments
Labels
doc Issues and PRs related to the documentations. util Issues and PRs related to the built-in util module.

Comments

@Semigradsky
Copy link
Contributor

Affected URL(s)

https://nodejs.org/docs/latest-v18.x/api/util.html#utilformatformat-args

Description of the problem

By https://nodejs.org/docs/latest-v18.x/api/util.html#utilformatformat-args has required string argument util.format.

But in examples we see:

If the first argument does not contain a valid format specifier, util.format() returns a string that is the concatenation of all arguments separated by spaces:

util.format(1, 2, 3);
// Returns: '1 2 3'

Looks like it the same as having optional format argument.
So it shoud be util.format([format [, ...args]]) instead of util.format(format[, ...args]).

@Semigradsky Semigradsky added the doc Issues and PRs related to the documentations. label Nov 29, 2022
@Semigradsky
Copy link
Contributor Author

Semigradsky commented Nov 29, 2022

The same for util.formatWithOptions.

@aduh95
Copy link
Contributor

aduh95 commented Nov 29, 2022

Do you want to send a PR to fix this?

@BridgeAR
Copy link
Member

The intention is to always have a format argument. If it does not contain a formatter, that's still valid.
The suggestion above util.format([format [, ...args]]) actually means no arguments at all is valid and that would print an empty string but it's also not the intended use.

@Semigradsky
Copy link
Contributor Author

util.format([format], [...args]) maybe?

There are two options:

  1. You can pass format and args: in this case arguments will be formatted by format.
  2. You can pass only args: in this case default formatter is used (returns a string that is the concatenation of all arguments separated by spaces)

util.format('%s:%s', 'foo') - 1
util.format(1, 2, 3) - 2
util.format('%s:%s') - 2
util.format() - 2 // not the intended use, but in fact, works and returns an empty string as expected

@VoltrexKeyva VoltrexKeyva added the util Issues and PRs related to the built-in util module. label Nov 29, 2022
@daltonna
Copy link

Could @Semigradsky @VoltrexKeyva or someone else assign this issue to me? I have some ideas for a fix.

@Semigradsky
Copy link
Contributor Author

@daltonna jsut send PR?

@BridgeAR
Copy link
Member

BridgeAR commented Apr 3, 2025

I don't really believe this is an issue. Since no one picked this up, I am going to close it

@BridgeAR BridgeAR closed this as completed Apr 3, 2025
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

No branches or pull requests

5 participants