GeezerVison is showing github.com — Ambiguity in docs on console.log and util.format · Issue #6341 · nodejs/node
Ambiguity in docs on console.log and util.format #6341Labels consoleIssues and PRs related to the console subsystem.docIssues and PRs related to Node.js documentation.good first issueIssues that are suitable for first-time contributors.utilIssues and PRs related to the built-in util module. Description
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. Activityadded utilIssues and PRs related to the built-in util module. docIssues and PRs related to Node.js documentation. consoleIssues and PRs related to the console subsystem. on Apr 21, 2016Trott commented on May 27, 2017 nattelog commented on Jun 15, 2017 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? Reacted by Vse Mozhe Buty vsemozhetbyt commented on Jun 15, 2017 ContributorAuthor More actions @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 commented on Jun 24, 2017 @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 commented on Jun 24, 2017 ContributorAuthor More actions @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 commented on Jun 24, 2017 ContributorAuthor More actions If I get it right, we have 3 cases with arguments:
- format string + any => string with interpolation of arguments.
- simple string + any => string with concatenation of
inspect() of arguments with exclusion for strings (quotes are stripped).
- 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 commented on Jun 25, 2017 ContributorAuthor More actions @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.
MetadataMetadataconsoleIssues and PRs related to the console subsystem.docIssues and PRs related to Node.js documentation.good first issueIssues that are suitable for first-time contributors.utilIssues and PRs related to the built-in util module. DevelopmentNo branches or pull requestsIssue actions
|