Skip to content

Commit

Permalink
util: add boxed BigInt formatting to util.inspect
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
targos committed Mar 17, 2018
1 parent 6a5a9ad commit 893432a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,13 @@ function formatValue(ctx, value, recurseTimes, ln) {
if (keyLength === 0)
return ctx.stylize(`[Boolean: ${formatted}]`, 'boolean');
base = `[Boolean: ${formatted}]`;
// eslint-disable-next-line valid-typeof
} else if (typeof raw === 'bigint') {
// Make boxed primitive BigInts look like such
const formatted = formatPrimitive(stylizeNoColor, raw);
if (keyLength === 0)
return ctx.stylize(`[BigInt: ${formatted}]`, 'bigint');
base = `[BigInt: ${formatted}]`;
} else if (typeof raw === 'symbol') {
const formatted = formatPrimitive(stylizeNoColor, raw);
return ctx.stylize(`[Symbol: ${formatted}]`, 'symbol');
Expand Down
2 changes: 2 additions & 0 deletions test/parallel/test-util-inspect-bigint.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ const assert = require('assert');
const { inspect } = require('util');

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

0 comments on commit 893432a

Please sign in to comment.