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

console,util: implement %o as formatting specifier #14545

Closed
Trott opened this issue Jul 30, 2017 · 11 comments
Closed

console,util: implement %o as formatting specifier #14545

Trott opened this issue Jul 30, 2017 · 11 comments
Labels
feature request Issues that request new features to be added to Node.js. util Issues and PRs related to the built-in util module.

Comments

@Trott
Copy link
Member

Trott commented Jul 30, 2017

  • Version: master
  • Platform: all
  • Subsystem: console,util

console.log() and util.format() support %d, %i, %f, etc. but not %o for objects which seems to be supported in major browsers.

Refs: #10292

/cc @silverwind

@Trott Trott added the feature request Issues that request new features to be added to Node.js. label Jul 30, 2017
@mscdex mscdex added the util Issues and PRs related to the built-in util module. label Jul 30, 2017
@silverwind
Copy link
Contributor

silverwind commented Jul 30, 2017

There's also %O which is supported in major browsers. According to the spec it's pretty similar to %o.

%O

An object with generic JavaScript object formatting is a potentially expandable representation of a generic JavaScript object.

%o

An object with optimally useful formatting is an implementation-specific, potentially-interactive representation of an object judged to be maximally useful and informative.

We could pretty-print %O, but other than that, I'm not seeing us implementing more differences. In Browsers, I don't see a difference on them either.

@XadillaX
Copy link
Contributor

So which shall we implement? Or both?

@TimothyGu
Copy link
Member

The difference between %o and %O is intended to be much like the difference between console.log and console.dir, with %o being like console.dir and %O being like console.log:

screenshot from 2017-07-31 11-50-48

Unfortunately browsers didn't implement %o and %O that way, and in Node.js console.log and console.dir are equivalent anyway. I'd be alright with just util.inspect the objects for both formatters.

@Fishrock123
Copy link
Contributor

Yeah I'd also be fine with it giving our typical object expansion.

@domenic is this about how you'd expect it to work in Node?

@domenic
Copy link
Contributor

domenic commented Jul 31, 2017

Yeah I'd expect either %o or both %o and %O to map to the usual util.inspect. Especially given that there's no opportunity for interaction it makes sense for them to be similar.

If you wanted to come up with some differentiator, you could. E.g., maybe one of them is meant to be more like valid JS syntax, and the other has little annotations like the name of the class or something. Or, %O special-cases certain important Node objects to given them interesting representations beyond just their properties (similar to how %O in browsers on DOM nodes produces an interactive <element> representation). But I'm not sure what those special object representations would be.

But keeping things simple, and having both be the usual object expansion, also makes sense to me.

@Fishrock123 Fishrock123 added the good first issue Issues that are suitable for first-time contributors. label Jul 31, 2017
@Fishrock123
Copy link
Contributor

Marking as good first contribution as I think mapping it would be fairly straightforward.

@gla5001
Copy link
Contributor

gla5001 commented Jul 31, 2017

is this still available? I'd like to take a stab at it as a first contribution.

@Fishrock123
Copy link
Contributor

@gla5001 it would seem so, yes.

@gla5001
Copy link
Contributor

gla5001 commented Jul 31, 2017

cool. i'll go ahead and work it.

Just so i'm clear, it seems like the behavior of the '%o' formatting specifier should match the behavior of the '%j' specifier. Is that correct?

@vsemozhetbyt
Copy link
Contributor

vsemozhetbyt commented Jul 31, 2017

@gla5001 I do not think so:

> const obj = { func: function() {} };

> console.log('%j', obj);
{}

>  console.log(util.inspect(obj));
{ func: [Function: func] }

JSON output has restrictions the util.inspect() has not.

@Fishrock123
Copy link
Contributor

@gla5001 No, by the discussion it should just return util.inspect() on the passed value.

gla5001 added a commit to gla5001/node that referenced this issue Jul 31, 2017
Implementing the %o and %O as formatting specifier for util.format.
Based on discussion in issue, this specifier should just call
util.inspect to format passed in value.

Fixes: nodejs#14545
@vsemozhetbyt vsemozhetbyt removed the good first issue Issues that are suitable for first-time contributors. label Aug 1, 2017
addaleax pushed a commit that referenced this issue Aug 10, 2017
Implementing the %o and %O formatting specifiers for util.format.
Based on discussion in issue, this specifier should just call
util.inspect to format the value.

PR-URL: #14558
Fixes: #14545
Reviewed-By: Refael Ackermann <[email protected]>
Reviewed-By: Roman Reiss <[email protected]>
Reviewed-By: Vse Mozhet Byt <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Evan Lucas <[email protected]>
Reviewed-By: Timothy Gu <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Tobias Nießen <[email protected]>
Reviewed-By: Yuta Hiroto <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Issues that request new features to be added to Node.js. util Issues and PRs related to the built-in util module.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants