Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nodejs/node.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorRuben Bridgewater <ruben@bridgewater.de>2019-12-19 18:12:30 +0300
committerRuben Bridgewater <ruben@bridgewater.de>2019-12-25 13:19:43 +0300
commit489e77c119dbef43b035338bc7eb3d4a2d1ae0c8 (patch)
tree943fd9579951f71610c2a6e5e7c81a80d40afea3 /lib
parentb45eeee01741013c4a37887817c71b779d9edb12 (diff)
util: add (typed) array length to the default output
Align the inspect output with the one used in the Chrome dev tools. A recent survey outlined that most users prefer to see the number of set and map entries. This should count as well for array sizes. The size is only added to regular arrays in case the constructor is not the default constructor. Typed arrays always indicate their size. PR-URL: https://github.com/nodejs/node/pull/31027 Reviewed-By: Michaƫl Zasso <targos@protonmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Anto Aravinth <anto.aravinth.cse@gmail.com>
Diffstat (limited to 'lib')
-rw-r--r--lib/internal/util/inspect.js61
1 files changed, 27 insertions, 34 deletions
diff --git a/lib/internal/util/inspect.js b/lib/internal/util/inspect.js
index 701cc34cebf..7054e35477e 100644
--- a/lib/internal/util/inspect.js
+++ b/lib/internal/util/inspect.js
@@ -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;
@@ -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.
@@ -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]}]`;
@@ -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) {
@@ -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) {