Skip to content

Inconsistent behavior between util.formatWithOptions and util.inspect #29726

Closed
@tiagonapoli

Description

@tiagonapoli

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] } :

const util = require('util')

const obj = { 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 object
console.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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionIssues that look for answers.utilIssues and PRs related to the built-in util module.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions