-
Notifications
You must be signed in to change notification settings - Fork 30.1k
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
util: implement %o as formatting specifier #14558
Conversation
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
One ubuntu1604-arm64 failure in the CI seems unrelated. @gla5001 Thank you for your contribution! |
/cc @Fishrock123 |
This may possibly also need documentation added (I cannot remember how extensively we've documented the formatting specifiers) |
Yup. Overlooked that. I'll update docs |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, except one nit.
doc/api/util.md
Outdated
@@ -167,6 +167,9 @@ corresponding argument. Supported placeholders are: | |||
* `%f` - Floating point value. | |||
* `%j` - JSON. Replaced with the string `'[Circular]'` if the argument | |||
contains circular references. | |||
* `%o` - Object. A string representation of an object. | |||
Similar to `util.inspect()` without options. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Indent this line
Is it too late to consider |
Or if there's no consensus let's implement just one, since changing it later will be |
I think it'd make sense to indent |
So im happy to implement whatever. Just need a consensus on it.
|
Quoting #14545 (comment)
|
I don't agree with @refack here. I don't think that JSON-ifying is more detailed, rather it is less, as JSON can't represent everything. |
Considering browsers differentiate this by using a |
Nit fix and changing behavior of %o
Should they be the same then? By JSON-ifying one and inspecting the other, you now the option of displaying more or less detail. Which could be useful in some cases? |
doc/api/util.md
Outdated
@@ -167,6 +167,11 @@ corresponding argument. Supported placeholders are: | |||
* `%f` - Floating point value. | |||
* `%j` - JSON. Replaced with the string `'[Circular]'` if the argument | |||
contains circular references. | |||
* `%o` - Object. A string representation of an object | |||
with optimally useful formatting. | |||
* `%O` - Object. A string representation of an object |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems we should state explicitly that JSON stringification is involved here with its restrictions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@vsemozhetbyt Hows that look?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems explicit enough if we do have a consensus for this behavior.
@Fishrock123 So are you suggesting to reverse may suggestion for so tl;dr in terms of "human usefulness" > const nestedObj = {
... foo: 'bar',
... foobar: {
..... foo: 'bar',
..... func: function() {}
..... }
... };
> console.log("%j", nestedObj)
{"foo":"bar","foobar":{"foo":"bar"}}
> console.log(JSON.stringify(nestedObj, null, ' '))
{
"foo": "bar",
"foobar": {
"foo": "bar"
}
}
> console.log(util.inspect(nestedObj))
{ foo: 'bar', foobar: { foo: 'bar', func: [Function: func] } } or go big and > console.log(util.inspect(nestedObj, {showHidden: true, breakLength:10}))
{ foo: 'bar',
foobar:
{ foo: 'bar',
func:
{ [Function: func]
[length]: 0,
[name]: 'func',
[prototype]: [Object] } } } |
Another idea ( @gla5001 sorry for the noise ), add |
Landed in 211df3f! Thank you for your efforts, great first contribution! 🎉 |
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 <refack@gmail.com> Reviewed-By: Roman Reiss <me@silverwind.io> Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Evan Lucas <evanlucas@me.com> Reviewed-By: Timothy Gu <timothygu99@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: Yuta Hiroto <hello@about-hiroppy.com>
awesome. thanks! |
Just a few tips for the future:
We are looking forward to your next contribution! |
@gla5001 congrats on GitHub promoting you from: |
P.S. a nice second contribution might be adding an |
No issue just my comment above #14558 (comment) |
I would not suggest that as a PR, as I doubt it's something the community is interested in (I personally think it's a bad idea). It would need a dedicated issue with discussion and appropriate input from decider-people. |
Well @gla5001 opening an issue and discussing a new feature is also a nice contribution. |
Starting discussion #14638 |
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 <refack@gmail.com> Reviewed-By: Roman Reiss <me@silverwind.io> Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Evan Lucas <evanlucas@me.com> Reviewed-By: Timothy Gu <timothygu99@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: Yuta Hiroto <hello@about-hiroppy.com>
Notable changes * **Inspector** * `require()` is available in the inspector console now. [#8837](#8837) * **N-API** * New APIs for creating number values have been introduced. [#14573](#14573) * **Stream** * For `Duplex` streams, the high water mark option can now be set independently for the readable and the writable side. [#14636](#14636) * **Util** * `util.format` now supports the `%o` and `%O` specifiers for printing objects. [#14558](#14558) PR-URL: #14811
Notable changes * **HTTP2** * Experimental support for the built-in `http2` has been added via the `--expose-http2` flag. [#14239](#14239) * **Inspector** * `require()` is available in the inspector console now. [#8837](#8837) * Multiple contexts, as created by the `vm` module, are supported now. [#14465](#14465) * **N-API** * New APIs for creating number values have been introduced. [#14573](#14573) * **Stream** * For `Duplex` streams, the high water mark option can now be set independently for the readable and the writable side. [#14636](#14636) * **Util** * `util.format` now supports the `%o` and `%O` specifiers for printing objects. [#14558](#14558) PR-URL: #14811
Notable changes * **HTTP2** * Experimental support for the built-in `http2` has been added via the `--expose-http2` flag. [#14239](#14239) * **Inspector** * `require()` is available in the inspector console now. [#8837](#8837) * Multiple contexts, as created by the `vm` module, are supported now. [#14465](#14465) * **N-API** * New APIs for creating number values have been introduced. [#14573](#14573) * **Stream** * For `Duplex` streams, the high water mark option can now be set independently for the readable and the writable side. [#14636](#14636) * **Util** * `util.format` now supports the `%o` and `%O` specifiers for printing objects. [#14558](#14558) PR-URL: #14811
Notable changes * **HTTP2** * Experimental support for the built-in `http2` has been added via the `--expose-http2` flag. [#14239](nodejs/node#14239) * **Inspector** * `require()` is available in the inspector console now. [#8837](nodejs/node#8837) * Multiple contexts, as created by the `vm` module, are supported now. [#14465](nodejs/node#14465) * **N-API** * New APIs for creating number values have been introduced. [#14573](nodejs/node#14573) * **Stream** * For `Duplex` streams, the high water mark option can now be set independently for the readable and the writable side. [#14636](nodejs/node#14636) * **Util** * `util.format` now supports the `%o` and `%O` specifiers for printing objects. [#14558](nodejs/node#14558) PR-URL: nodejs/node#14811
Release team were +1 on backporting to v6.x. |
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: #14545
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passesAffected core subsystem(s)
util