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
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?
The text was updated successfully, but these errors were encountered:
tiagonapoli
changed the title
Unexpected output with util.formatWithOptions
Inconsistent behavior between util.formatWithOptions and util.inspect
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().
The following snippet shows a possible unexpected output for
formatWithOptions
. Onconsole.log(util.formatWithOptions({ }, '%s', obj))
I expected a output{ a: [ '123' ] }
, but got{ a: [Array] }
:Since
util.formatWithOptions('%s', obj)
doesn't respect the api docs, maybe this is an expected behavior forutil.formatWithOptions
, in this case maybe it could be changed to have similar behavior to inspect?The text was updated successfully, but these errors were encountered: