Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
util: implement %o as formatting specifier.
Nit fix and changing behavior of %o
  • Loading branch information
gla5001 committed Aug 1, 2017
commit 0b373b4d0f96cd348af9f1aec929c07eeedb1744
8 changes: 5 additions & 3 deletions doc/api/util.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +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.
Similar to `util.inspect()` without options.
* `%O` - Object. Same as `%o`.
* `%o` - Object. A string representation of an object
with optimally useful formatting.
* `%O` - Object. A string representation of an object
Copy link
Copy Markdown
Contributor

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vsemozhetbyt Hows that look?

Copy link
Copy Markdown
Contributor

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.

with generic JavaScript object formatting.
Similar to `util.inspect()` without options.
* `%%` - single percent sign (`'%'`). This does not consume an argument.

If the placeholder does not have a corresponding argument, the placeholder is
Expand Down
6 changes: 5 additions & 1 deletion lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,15 @@ function format(f) {
str += String(arguments[a++]);
break;
case 79: // 'O'
case 111: // 'o'
if (lastPos < i)
str += f.slice(lastPos, i);
str += inspect(arguments[a++]);
break;
case 111: // 'o'
if (lastPos < i)
str += f.slice(lastPos, i);
str += tryStringify(arguments[a++], null, ' ');
break;
case 37: // '%'
if (lastPos < i)
str += f.slice(lastPos, i);
Expand Down
11 changes: 5 additions & 6 deletions test/parallel/test-util-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,20 +116,19 @@ const nestedObj = {
};
assert.strictEqual(util.format('%o'), '%o');
assert.strictEqual(util.format('%o', 42), '42');
assert.strictEqual(util.format('%o', 'foo'), '\'foo\'');
assert.strictEqual(util.format('%o', 'foo'), '"foo"');
assert.strictEqual(
util.format('%o', obj),
'{ foo: \'bar\', foobar: 1, func: [Function: func] }');
'{"foo":"bar","foobar":1}');
assert.strictEqual(
util.format('%o', nestedObj),
'{ foo: \'bar\', foobar: { foo: \'bar\', func: [Function: func] } }');
'{"foo":"bar","foobar":{"foo":"bar"}}');
assert.strictEqual(
util.format('%o %o', obj, obj),
'{ foo: \'bar\', foobar: 1, func: [Function: func] } ' +
'{ foo: \'bar\', foobar: 1, func: [Function: func] }');
'{"foo":"bar","foobar":1} {"foo":"bar","foobar":1}');
assert.strictEqual(
util.format('%o %o', obj),
'{ foo: \'bar\', foobar: 1, func: [Function: func] } %o');
'{"foo":"bar","foobar":1} %o');

assert.strictEqual(util.format('%O'), '%O');
assert.strictEqual(util.format('%O', 42), '42');
Expand Down