From 893432ad928e25854950c0b5c581dfb3081ed4bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Zasso?= Date: Wed, 14 Mar 2018 10:03:36 +0100 Subject: [PATCH] util: add boxed BigInt formatting to util.inspect Before: > Object(7n) BigInt {} After: > Object(7n) [BigInt: 7n] PR-URL: https://github.com/nodejs/node/pull/19341 Reviewed-By: Ruben Bridgewater Reviewed-By: James M Snell Reviewed-By: Colin Ihrig Reviewed-By: Gus Caplan --- lib/util.js | 7 +++++++ test/parallel/test-util-inspect-bigint.js | 2 ++ 2 files changed, 9 insertions(+) diff --git a/lib/util.js b/lib/util.js index b64d103cb30bca..b9ad76f4ad3ebd 100644 --- a/lib/util.js +++ b/lib/util.js @@ -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'); diff --git a/test/parallel/test-util-inspect-bigint.js b/test/parallel/test-util-inspect-bigint.js index cb50c1f6982460..5e0233640e8313 100644 --- a/test/parallel/test-util-inspect-bigint.js +++ b/test/parallel/test-util-inspect-bigint.js @@ -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]');