Skip to content

Commit 80496a5

Browse files
util: add inspect suffix to BigInt64Array elements
This commit updates `util.inspect` to add an `n` suffix to BigInts that appear in BigInt64Arrays. BigInts are formatted with an `n` suffix in most cases, but this did not occur in BigInt64Arrays due to an apparent oversight where the implementation of `inspect` for typed arrays assumed that all typed array elements are numbers. PR-URL: #21499 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Yuta Hiroto <hello@hiroppy.me>
1 parent dcb371f commit 80496a5

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

lib/util.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,10 @@ function formatNumber(fn, value) {
739739
return fn(`${value}`, 'number');
740740
}
741741

742+
function formatBigInt(fn, value) {
743+
return fn(`${value}n`, 'bigint');
744+
}
745+
742746
function formatPrimitive(fn, value, ctx) {
743747
if (typeof value === 'string') {
744748
if (ctx.compact === false &&
@@ -779,7 +783,7 @@ function formatPrimitive(fn, value, ctx) {
779783
return formatNumber(fn, value);
780784
// eslint-disable-next-line valid-typeof
781785
if (typeof value === 'bigint')
782-
return fn(`${value}n`, 'bigint');
786+
return formatBigInt(fn, value);
783787
if (typeof value === 'boolean')
784788
return fn(`${value}`, 'boolean');
785789
if (typeof value === 'undefined')
@@ -915,8 +919,11 @@ function formatTypedArray(ctx, value, recurseTimes, keys) {
915919
const maxLength = Math.min(Math.max(0, ctx.maxArrayLength), value.length);
916920
const remaining = value.length - maxLength;
917921
const output = new Array(maxLength + (remaining > 0 ? 1 : 0));
922+
const elementFormatter = value.length > 0 && typeof value[0] === 'number' ?
923+
formatNumber :
924+
formatBigInt;
918925
for (var i = 0; i < maxLength; ++i)
919-
output[i] = formatNumber(ctx.stylize, value[i]);
926+
output[i] = elementFormatter(ctx.stylize, value[i]);
920927
if (remaining > 0)
921928
output[i] = `... ${remaining} more item${remaining > 1 ? 's' : ''}`;
922929
if (ctx.showHidden) {

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

+2
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@ const { inspect } = require('util');
1010
assert.strictEqual(inspect(1n), '1n');
1111
assert.strictEqual(inspect(Object(-1n)), '[BigInt: -1n]');
1212
assert.strictEqual(inspect(Object(13n)), '[BigInt: 13n]');
13+
assert.strictEqual(inspect(new BigInt64Array([0n])), 'BigInt64Array [ 0n ]');
14+
assert.strictEqual(inspect(new BigUint64Array([0n])), 'BigUint64Array [ 0n ]');

0 commit comments

Comments
 (0)