Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add %J console format specifier #33036

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions doc/api/util.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ corresponding argument. Supported specifiers are:
* `%f`: `parseFloat(value)` is used for all values expect `Symbol`.
* `%j`: JSON. Replaced with the string `'[Circular]'` if the argument contains
circular references.
* `%J`: Indented version of %j.
* `%o`: `Object`. A string representation of an object with generic JavaScript
object formatting. Similar to `util.inspect()` with options
`{ showHidden: true, showProxy: true }`. This will show the full object
Expand Down
9 changes: 6 additions & 3 deletions lib/internal/util/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -1770,14 +1770,14 @@ function hasBuiltInToString(value) {

const firstErrorLine = (error) => error.message.split('\n')[0];
let CIRCULAR_ERROR_MESSAGE;
function tryStringify(arg) {
function tryStringify(arg, indentation = 0) {
try {
return JSONStringify(arg);
return JSONStringify(arg, null, indentation);
} catch (err) {
// Populate the circular error message lazily
if (!CIRCULAR_ERROR_MESSAGE) {
try {
const a = {}; a.a = a; JSONStringify(a);
const a = {}; a.a = a; JSONStringify(a, null, indentation);
} catch (err) {
CIRCULAR_ERROR_MESSAGE = firstErrorLine(err);
}
Expand Down Expand Up @@ -1842,6 +1842,9 @@ function formatWithOptionsInternal(inspectOptions, ...args) {
case 106: // 'j'
tempStr = tryStringify(args[++a]);
break;
case 74: // 'J'
tempStr = tryStringify(args[++a], 2);
break;
case 100: // 'd'
const tempNum = args[++a];
if (typeof tempNum === 'bigint') {
Expand Down
11 changes: 11 additions & 0 deletions test/parallel/test-util-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ assert.strictEqual(util.format(symbol), 'Symbol(foo)');
assert.strictEqual(util.format('foo', symbol), 'foo Symbol(foo)');
assert.strictEqual(util.format('%s', symbol), 'Symbol(foo)');
assert.strictEqual(util.format('%j', symbol), 'undefined');
assert.strictEqual(util.format('%J', symbol), 'undefined');

// Number format specifier
assert.strictEqual(util.format('%d'), '%d');
Expand Down Expand Up @@ -237,6 +238,16 @@ assert.strictEqual(util.format('%j', '42'), '"42"');
assert.strictEqual(util.format('%j %j', 42, 43), '42 43');
assert.strictEqual(util.format('%j %j', 42), '42 %j');

// Indented JSON format specifier
assert.strictEqual(util.format('%J'), '%J');
assert.strictEqual(util.format('%J', 42), '42');
assert.strictEqual(util.format('%J', '42'), '"42"');
assert.strictEqual(util.format('%J %J', 42, 43), '42 43');
assert.strictEqual(util.format('%J %J', 42), '42 %J');
assert.strictEqual(
util.format('%J', { foo: { bar: 42 } })
, '{\n "foo": {\n "bar": 42\n }\n}');

// Object format specifier
const obj = {
foo: 'bar',
Expand Down
2 changes: 1 addition & 1 deletion test/pummel/test-child-process-spawn-loop.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const N = 40;
let finished = false;

function doSpawn(i) {
const child = spawn('python', ['-c', `print ${SIZE} * "C"`]);
const child = spawn('python', ['-c', `print(${SIZE} * "C")`]);
let count = 0;

child.stdout.setEncoding('ascii');
Expand Down