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

Ambiguity in docs on console.log and util.format #6341

Closed
vsemozhetbyt opened this issue Apr 21, 2016 · 7 comments
Closed

Ambiguity in docs on console.log and util.format #6341

vsemozhetbyt opened this issue Apr 21, 2016 · 7 comments
Labels
console Issues and PRs related to the console subsystem. doc Issues and PRs related to the documentations. good first issue Issues that are suitable for first-time contributors. util Issues and PRs related to the built-in util module.

Comments

@vsemozhetbyt
Copy link
Contributor

vsemozhetbyt commented Apr 21, 2016

  • Subsystem: console, util

Here is a fragment from console docs:

If formatting elements (e.g. %d) are not found in the first string then util.inspect() is called on each argument and the resulting string values are concatenated.

Here is a slightly different fragment from util docs:

If the first argument is not a format string then util.format() returns a string that is the concatenation of all its arguments separated by spaces. Each argument is converted to a string with util.inspect().

However, the behavior of both functions differs not only due to a presence of % elements in the string. According to the code (if I get it right), the behavior diverges due to merely typeof of the first argument.

Here is a test code with output confusing for the naive reader (including me):

const util = require('util');

console.log(util.inspect(1));
console.log(util.inspect('str'));
console.log();

console.log(1, 'str');
console.log('str', 1);
console.log();

console.log(util.format(1, 'str'));
console.log(util.format('str', 1));
console.log();

The output:

1
'str'

1 'str'
str 1

1 'str'
str 1

The first block shows the elements for future concatenation. The second and third ones show the output of differently disposed arguments, with none of the strings containing the % format elements.

@mscdex mscdex added util Issues and PRs related to the built-in util module. doc Issues and PRs related to the documentations. console Issues and PRs related to the console subsystem. labels Apr 21, 2016
@addaleax addaleax added the good first issue Issues that are suitable for first-time contributors. label Apr 30, 2017
@Trott
Copy link
Member

Trott commented May 27, 2017

@nodejs/documentation

@nattelog
Copy link
Contributor

Has anybody done some work on this issue? I would love to start contributing to this project and this seems like a good start. Is it possible to get help if needed?

@vsemozhetbyt
Copy link
Contributor Author

@nattelog If I recall correctly, no, nobody works on this. You can open a PR with a proposed additions to the doc and we can have all the discussion/corrections there, also you can ask any preliminary questions here.

@nattelog
Copy link
Contributor

@vsemozhetbyt I do not quite understand the ambiguity here or how your test code confirms it. What are your expected outputs from the test? For me (given a quick look) it seems legit? Or what am I missing?

@vsemozhetbyt
Copy link
Contributor Author

vsemozhetbyt commented Jun 24, 2017

@nattelog I mean the doc probably should warn that output depends not only on a format of a first string but also on that if it is a string at all.

@vsemozhetbyt
Copy link
Contributor Author

vsemozhetbyt commented Jun 24, 2017

If I get it right, we have 3 cases with arguments:

  1. format string + any => string with interpolation of arguments.
  2. simple string + any => string with concatenation of inspect() of arguments with exclusion for strings (quotes are stripped).
  3. not string + any => string with concatenation of inspect() of arguments.
> console.log('str%d', 1);
str1
> console.log('str', 1);
str 1
> console.log(1, 'str');
1 'str'

@vsemozhetbyt
Copy link
Contributor Author

@nattelog Sorry, it seems this should be superseded by #13908, I think that issue is more clear. Feel free to rise a PR for that issue when the solution is defined.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
console Issues and PRs related to the console subsystem. doc Issues and PRs related to the documentations. good first issue Issues that are suitable for first-time contributors. util Issues and PRs related to the built-in util module.
Projects
None yet
Development

No branches or pull requests

5 participants