You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Inconsistent behavior between util.formatWithOptions and util.inspect #29726
The following snippet shows a possible unexpected output for formatWithOptions. On console.log(util.formatWithOptions({ }, '%s', obj)) I expected a output { a: [ '123' ] }, but got { a: [Array] } :
constutil=require('util')constobj={a: ['123']}/************ formatWithOptions ************/// { a: [Array] }console.log(util.formatWithOptions({},'%s',obj))// { a: [ '123' ] } - Note: this is not respecting the api docs, the first argument has to be an objectconsole.log(util.formatWithOptions('%s',obj))/******************************************//************ inspect ************/// { a: [ '123' ] }console.log(util.inspect(obj,{}))// { a: [ '123' ] }console.log(util.inspect(obj))/*******************************/
Since util.formatWithOptions('%s', obj) doesn't respect the api docs, maybe this is an expected behavior for util.formatWithOptions, in this case maybe it could be changed to have similar behavior to inspect?
changed the title [-]Unexpected output with util.formatWithOptions[/-][+]Inconsistent behavior between util.formatWithOptions and util.inspect[/+]on Sep 26, 2019
@tiagonapoli yes, that is intentional. The reason is that inspecting objects with %s was until recently not inspected at all but String(obj) was used and that would result in e.g. [object Object] for input like { a: true }.
If you want to pass through objects, please use the %O formatter. That way the depth is handled as with util.inspect().
Activity
[-]Unexpected output with util.formatWithOptions[/-][+]Inconsistent behavior between util.formatWithOptions and util.inspect[/+]tiagonapoli commentedon Sep 26, 2019
Saw that the following code specifies that the depth for
%sis garanteed to be 0, overriding the giveninspectOptions, is this the behavior intended?node/lib/internal/util/inspect.js
Lines 1611 to 1616 in 1fa4037
BridgeAR commentedon Sep 26, 2019
@tiagonapoli yes, that is intentional. The reason is that inspecting objects with
%swas until recently not inspected at all butString(obj)was used and that would result in e.g.[object Object]for input like{ a: true }.If you want to pass through objects, please use the
%Oformatter. That way the depth is handled as withutil.inspect().BridgeAR commentedon Sep 26, 2019
And
util.formatWithOptions('%s', obj)should actually throw an error. You pass through'%s'as options.util: validate formatWithOptions inspectOptions
util: validate formatWithOptions inspectOptions