Skip to content

Commit 893432a

Browse files
committed
util: add boxed BigInt formatting to util.inspect
Before: > Object(7n) BigInt {} After: > Object(7n) [BigInt: 7n] PR-URL: #19341 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Gus Caplan <me@gus.host>
1 parent 6a5a9ad commit 893432a

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

lib/util.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,13 @@ function formatValue(ctx, value, recurseTimes, ln) {
580580
if (keyLength === 0)
581581
return ctx.stylize(`[Boolean: ${formatted}]`, 'boolean');
582582
base = `[Boolean: ${formatted}]`;
583+
// eslint-disable-next-line valid-typeof
584+
} else if (typeof raw === 'bigint') {
585+
// Make boxed primitive BigInts look like such
586+
const formatted = formatPrimitive(stylizeNoColor, raw);
587+
if (keyLength === 0)
588+
return ctx.stylize(`[BigInt: ${formatted}]`, 'bigint');
589+
base = `[BigInt: ${formatted}]`;
583590
} else if (typeof raw === 'symbol') {
584591
const formatted = formatPrimitive(stylizeNoColor, raw);
585592
return ctx.stylize(`[Symbol: ${formatted}]`, 'symbol');

test/parallel/test-util-inspect-bigint.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@ const assert = require('assert');
88
const { inspect } = require('util');
99

1010
assert.strictEqual(inspect(1n), '1n');
11+
assert.strictEqual(inspect(Object(-1n)), '[BigInt: -1n]');
12+
assert.strictEqual(inspect(Object(13n)), '[BigInt: 13n]');

0 commit comments

Comments
 (0)