Skip to content

Commit

Permalink
console,util: inspect strings as in < Node.js v12.x
Browse files Browse the repository at this point in the history
This reverts nodejs#23162. The gained
consistency did not outweight the benefit of inspecting the arguments.
Therefore it is best to revert to the former behavior.

Refs: nodejs#29539
Refs: nodejs#23162
  • Loading branch information
BridgeAR authored and Trott committed Oct 14, 2019
1 parent ddc27dc commit 82477e1
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 24 deletions.
45 changes: 27 additions & 18 deletions doc/api/util.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,10 @@ property take precedence over `--trace-deprecation` and
<!-- YAML
added: v0.5.3
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/29592
description: The output string's formatting will again dependent on the type
of the first argument.
- version: v12.11.0
pr-url: https://github.com/nodejs/node/pull/29606
description: The `%c` specifier is ignored now.
Expand Down Expand Up @@ -217,10 +221,11 @@ changes:
* `...args` {any}
* Returns: {string} The formatted string

The `util.format()` method returns a formatted string using the first argument
as a `printf`-like format string which can contain zero or more format
specifiers. Each specifier is replaced with the converted value from the
corresponding argument. Supported specifiers are:
If the first argument `format` is a string and `args` is not empty, the
`util.format()` method returns a formatted string using the first argument as a
`printf`-like format string which can contain zero or more format specifiers.
Each specifier is replaced with the converted value from the corresponding
argument. Supported specifiers are:

* `%s` - `String` will be used to convert all values except `BigInt`, `Object`
and `-0`. `BigInt` values will be represented with an `n` and Objects that
Expand Down Expand Up @@ -251,32 +256,36 @@ util.format('%s:%s', 'foo');
// Returns: 'foo:%s'
```

Values that are not part of the format string are formatted using
`util.inspect()` if their type is not `string`.

If there are more arguments passed to the `util.format()` method than the
number of specifiers, the extra arguments are concatenated to the returned
string, separated by spaces:
If there are more arguments than the number of specifiers, the extra arguments
are concatenated to the returned string, separated by spaces:

```js
util.format('%s:%s', 'foo', 'bar', 'baz');
// Returns: 'foo:bar baz'
```

If the first argument does not contain a valid format specifier, `util.format()`
returns a string that is the concatenation of all arguments separated by spaces:
Values that are not consumed by the format string are formatted using
`util.inspect()` if their type is not `string`:

```js
util.format('string', 5n, 'string');
// Returns 'string 5n string'
```

All values are formatted using `util.inspect()`, if the first argument's type is
not `string`:

```js
util.format(1, 2, 3);
// Returns: '1 2 3'
util.format(5n, 'string \n line 1', [1]);
// Returns: "5n 'string \\n line 1' [ 1 ]"
```

If only one argument is passed to `util.format()`, it is returned as it is
without any formatting:
If only a single argument of type `string` is passed to `util.format()`, it is
returned as it is without any formatting:

```js
util.format('%% %s');
// Returns: '%% %s'
util.format('%% %s \n line 2');
// Returns: '%% %s \n line 2'
```

`util.format()` is a synchronous method that is intended as a debugging tool.
Expand Down
6 changes: 5 additions & 1 deletion lib/internal/util/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -1700,7 +1700,11 @@ function formatWithOptionsInternal(inspectOptions, ...args) {
while (a < args.length) {
const value = args[a];
str += join;
str += typeof value !== 'string' ? inspect(value, inspectOptions) : value;
if (typeof value !== 'string' || typeof first !== 'string') {
str += inspect(value, inspectOptions);
} else {
str += value;
}
join = ' ';
a++;
}
Expand Down
18 changes: 13 additions & 5 deletions test/parallel/test-util-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -367,17 +367,17 @@ Object.setPrototypeOf(BadCustomError, Error);
assert.strictEqual(util.format(new BadCustomError('foo')),
'[BadCustomError: foo]');

// The format of arguments should not depend on type of the first argument
// The argument formatting depends on the type of the first argument.
assert.strictEqual(util.format('1', '1'), '1 1');
assert.strictEqual(util.format(1, '1'), '1 1');
assert.strictEqual(util.format(1, '1'), "1 '1'");
assert.strictEqual(util.format('1', 1), '1 1');
assert.strictEqual(util.format(1, -0), '1 -0');
assert.strictEqual(util.format('1', () => {}), '1 [Function (anonymous)]');
assert.strictEqual(util.format(1, () => {}), '1 [Function (anonymous)]');
assert.strictEqual(util.format('1', "'"), "1 '");
assert.strictEqual(util.format(1, "'"), "1 '");
assert.strictEqual(util.format(1, "'"), '1 "\'"');
assert.strictEqual(util.format('1', 'number'), '1 number');
assert.strictEqual(util.format(1, 'number'), '1 number');
assert.strictEqual(util.format(1, 'number'), "1 'number'");
assert.strictEqual(util.format(5n), '5n');
assert.strictEqual(util.format(5n, 5n), '5n 5n');

Expand All @@ -393,7 +393,15 @@ assert.strictEqual(
'\u001b[33m1\u001b[39m ' +
'\u001b[33m5n\u001b[39m ' +
'\u001b[1mnull\u001b[22m ' +
'foobar'
"\u001b[32m'foobar'\u001b[39m"
);

assert.strictEqual(
util.formatWithOptions(
{ colors: true },
'string', true, 'foobar'
),
'string \u001b[33mtrue\u001b[39m foobar'
);

assert.strictEqual(
Expand Down

0 comments on commit 82477e1

Please sign in to comment.