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: https://github.com/nodejs/node/pull/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>
This commit is contained in:
parent
dcb371ff1f
commit
80496a5570
11
lib/util.js
11
lib/util.js
@ -739,6 +739,10 @@ function formatNumber(fn, value) {
|
|||||||
return fn(`${value}`, 'number');
|
return fn(`${value}`, 'number');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function formatBigInt(fn, value) {
|
||||||
|
return fn(`${value}n`, 'bigint');
|
||||||
|
}
|
||||||
|
|
||||||
function formatPrimitive(fn, value, ctx) {
|
function formatPrimitive(fn, value, ctx) {
|
||||||
if (typeof value === 'string') {
|
if (typeof value === 'string') {
|
||||||
if (ctx.compact === false &&
|
if (ctx.compact === false &&
|
||||||
@ -779,7 +783,7 @@ function formatPrimitive(fn, value, ctx) {
|
|||||||
return formatNumber(fn, value);
|
return formatNumber(fn, value);
|
||||||
// eslint-disable-next-line valid-typeof
|
// eslint-disable-next-line valid-typeof
|
||||||
if (typeof value === 'bigint')
|
if (typeof value === 'bigint')
|
||||||
return fn(`${value}n`, 'bigint');
|
return formatBigInt(fn, value);
|
||||||
if (typeof value === 'boolean')
|
if (typeof value === 'boolean')
|
||||||
return fn(`${value}`, 'boolean');
|
return fn(`${value}`, 'boolean');
|
||||||
if (typeof value === 'undefined')
|
if (typeof value === 'undefined')
|
||||||
@ -915,8 +919,11 @@ function formatTypedArray(ctx, value, recurseTimes, keys) {
|
|||||||
const maxLength = Math.min(Math.max(0, ctx.maxArrayLength), value.length);
|
const maxLength = Math.min(Math.max(0, ctx.maxArrayLength), value.length);
|
||||||
const remaining = value.length - maxLength;
|
const remaining = value.length - maxLength;
|
||||||
const output = new Array(maxLength + (remaining > 0 ? 1 : 0));
|
const output = new Array(maxLength + (remaining > 0 ? 1 : 0));
|
||||||
|
const elementFormatter = value.length > 0 && typeof value[0] === 'number' ?
|
||||||
|
formatNumber :
|
||||||
|
formatBigInt;
|
||||||
for (var i = 0; i < maxLength; ++i)
|
for (var i = 0; i < maxLength; ++i)
|
||||||
output[i] = formatNumber(ctx.stylize, value[i]);
|
output[i] = elementFormatter(ctx.stylize, value[i]);
|
||||||
if (remaining > 0)
|
if (remaining > 0)
|
||||||
output[i] = `... ${remaining} more item${remaining > 1 ? 's' : ''}`;
|
output[i] = `... ${remaining} more item${remaining > 1 ? 's' : ''}`;
|
||||||
if (ctx.showHidden) {
|
if (ctx.showHidden) {
|
||||||
|
@ -10,3 +10,5 @@ const { inspect } = require('util');
|
|||||||
assert.strictEqual(inspect(1n), '1n');
|
assert.strictEqual(inspect(1n), '1n');
|
||||||
assert.strictEqual(inspect(Object(-1n)), '[BigInt: -1n]');
|
assert.strictEqual(inspect(Object(-1n)), '[BigInt: -1n]');
|
||||||
assert.strictEqual(inspect(Object(13n)), '[BigInt: 13n]');
|
assert.strictEqual(inspect(Object(13n)), '[BigInt: 13n]');
|
||||||
|
assert.strictEqual(inspect(new BigInt64Array([0n])), 'BigInt64Array [ 0n ]');
|
||||||
|
assert.strictEqual(inspect(new BigUint64Array([0n])), 'BigUint64Array [ 0n ]');
|
||||||
|
Loading…
x
Reference in New Issue
Block a user