Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

util: add (typed) array length to the default output #31027

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 27 additions & 34 deletions lib/internal/util/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ const setSizeGetter = uncurryThis(
ObjectGetOwnPropertyDescriptor(SetPrototype, 'size').get);
const mapSizeGetter = uncurryThis(
ObjectGetOwnPropertyDescriptor(MapPrototype, 'size').get);
const typedArraySizeGetter = uncurryThis(
ObjectGetOwnPropertyDescriptor(
ObjectGetPrototypeOf(Uint8Array.prototype), 'length').get);

let hexSlice;

Expand Down Expand Up @@ -567,18 +570,18 @@ function addPrototypeProperties(ctx, main, obj, recurseTimes, isProto, output) {
} while (++depth !== 3);
}

function getPrefix(constructor, tag, fallback) {
function getPrefix(constructor, tag, fallback, size = '') {
if (constructor === null) {
if (tag !== '') {
return `[${fallback}: null prototype] [${tag}] `;
return `[${fallback}${size}: null prototype] [${tag}] `;
}
return `[${fallback}: null prototype] `;
return `[${fallback}${size}: null prototype] `;
}

if (tag !== '' && constructor !== tag) {
return `${constructor} [${tag}] `;
return `${constructor}${size} [${tag}] `;
}
return `${constructor} `;
return `${constructor}${size} `;
}

// Look up the keys of the object.
Expand Down Expand Up @@ -763,58 +766,48 @@ function formatRaw(ctx, value, recurseTimes, typedArray) {
if (value[SymbolIterator] || constructor === null) {
noIterator = false;
if (ArrayIsArray(value)) {
keys = getOwnNonIndexProperties(value, filter);
// Only set the constructor for non ordinary ("Array [...]") arrays.
const prefix = getPrefix(constructor, tag, 'Array');
braces = [`${prefix === 'Array ' ? '' : prefix}[`, ']'];
const prefix = (constructor !== 'Array' || tag !== '') ?
getPrefix(constructor, tag, 'Array', `(${value.length})`) :
'';
keys = getOwnNonIndexProperties(value, filter);
braces = [`${prefix}[`, ']'];
if (value.length === 0 && keys.length === 0 && protoProps === undefined)
return `${braces[0]}]`;
extrasType = kArrayExtrasType;
formatter = formatArray;
} else if (isSet(value)) {
const size = setSizeGetter(value);
const prefix = getPrefix(constructor, tag, 'Set', `(${size})`);
keys = getKeys(value, ctx.showHidden);
let prefix = '';
if (constructor !== null) {
if (constructor === tag)
tag = '';
prefix = getPrefix(`${constructor}(${size})`, tag, '');
formatter = formatSet.bind(null, value, size);
} else {
prefix = getPrefix(constructor, tag, `Set(${size})`);
formatter = formatSet.bind(null, SetPrototypeValues(value), size);
}
formatter = constructor !== null ?
formatSet.bind(null, value) :
formatSet.bind(null, SetPrototypeValues(value));
if (size === 0 && keys.length === 0 && protoProps === undefined)
return `${prefix}{}`;
braces = [`${prefix}{`, '}'];
} else if (isMap(value)) {
const size = mapSizeGetter(value);
const prefix = getPrefix(constructor, tag, 'Map', `(${size})`);
keys = getKeys(value, ctx.showHidden);
let prefix = '';
if (constructor !== null) {
if (constructor === tag)
tag = '';
prefix = getPrefix(`${constructor}(${size})`, tag, '');
formatter = formatMap.bind(null, value, size);
} else {
prefix = getPrefix(constructor, tag, `Map(${size})`);
formatter = formatMap.bind(null, MapPrototypeEntries(value), size);
}
formatter = constructor !== null ?
formatMap.bind(null, value) :
formatMap.bind(null, MapPrototypeEntries(value));
if (size === 0 && keys.length === 0 && protoProps === undefined)
return `${prefix}{}`;
braces = [`${prefix}{`, '}'];
} else if (isTypedArray(value)) {
keys = getOwnNonIndexProperties(value, filter);
let bound = value;
let prefix = '';
let fallback = '';
if (constructor === null) {
const constr = findTypedConstructor(value);
prefix = getPrefix(constructor, tag, constr.name);
fallback = constr.name;
// Reconstruct the array information.
bound = new constr(value);
} else {
prefix = getPrefix(constructor, tag);
}
const size = typedArraySizeGetter(value);
const prefix = getPrefix(constructor, tag, fallback, `(${size})`);
braces = [`${prefix}[`, ']'];
if (value.length === 0 && keys.length === 0 && !ctx.showHidden)
return `${braces[0]}]`;
Expand Down Expand Up @@ -1430,7 +1423,7 @@ function formatTypedArray(value, ctx, ignored, recurseTimes) {
return output;
}

function formatSet(value, size, ctx, ignored, recurseTimes) {
function formatSet(value, ctx, ignored, recurseTimes) {
const output = [];
ctx.indentationLvl += 2;
for (const v of value) {
Expand All @@ -1440,7 +1433,7 @@ function formatSet(value, size, ctx, ignored, recurseTimes) {
return output;
}

function formatMap(value, size, ctx, ignored, recurseTimes) {
function formatMap(value, ctx, ignored, recurseTimes) {
const output = [];
ctx.indentationLvl += 2;
for (const [k, v] of value) {
Expand Down
8 changes: 4 additions & 4 deletions test/parallel/test-assert-deep.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ assert.throws(
{
code: 'ERR_ASSERTION',
message: `${defaultMsgStartFull} ... Lines skipped\n\n` +
'+ Uint8Array [\n' +
'- Buffer [Uint8Array] [\n 120,\n...\n 122,\n 10\n ]'
'+ Uint8Array(4) [\n' +
'- Buffer(4) [Uint8Array] [\n 120,\n...\n 122,\n 10\n ]'
}
);
assert.deepEqual(arr, buf);
Expand All @@ -66,7 +66,7 @@ assert.deepEqual(arr, buf);
{
code: 'ERR_ASSERTION',
message: `${defaultMsgStartFull}\n\n` +
' Buffer [Uint8Array] [\n' +
' Buffer(4) [Uint8Array] [\n' +
' 120,\n' +
' 121,\n' +
' 122,\n' +
Expand All @@ -86,7 +86,7 @@ assert.deepEqual(arr, buf);
{
code: 'ERR_ASSERTION',
message: `${defaultMsgStartFull}\n\n` +
' Uint8Array [\n' +
' Uint8Array(4) [\n' +
' 120,\n' +
' 121,\n' +
' 122,\n' +
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-buffer-inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ b.inspect = undefined;
b.prop = new Uint8Array(0);
assert.strictEqual(
util.inspect(b),
'<Buffer 31 32, inspect: undefined, prop: Uint8Array []>'
'<Buffer 31 32, inspect: undefined, prop: Uint8Array(0) []>'
);

b = Buffer.alloc(0);
Expand Down
6 changes: 3 additions & 3 deletions test/parallel/test-fs-read-empty-buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ assert.throws(
{
code: 'ERR_INVALID_ARG_VALUE',
message: 'The argument \'buffer\' is empty and cannot be written. ' +
'Received Uint8Array []'
'Received Uint8Array(0) []'
}
);

Expand All @@ -24,7 +24,7 @@ assert.throws(
{
code: 'ERR_INVALID_ARG_VALUE',
message: 'The argument \'buffer\' is empty and cannot be written. ' +
'Received Uint8Array []'
'Received Uint8Array(0) []'
}
);

Expand All @@ -35,7 +35,7 @@ assert.throws(
{
code: 'ERR_INVALID_ARG_VALUE',
message: 'The argument \'buffer\' is empty and cannot be written. ' +
'Received Uint8Array []'
'Received Uint8Array(0) []'
}
);
})();
2 changes: 1 addition & 1 deletion test/parallel/test-util-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ assert.strictEqual(util.format('%s', () => 5), '() => 5');
class Foobar extends Array { aaa = true; }
assert.strictEqual(
util.format('%s', new Foobar(5)),
'Foobar [ <5 empty items>, aaa: true ]'
'Foobar(5) [ <5 empty items>, aaa: true ]'
);

// Subclassing:
Expand Down
Loading