Skip to content

Commit f3427e6

Browse files
committed
✨ feat: Show special props of TypedArray when showHidden is "always"
1 parent d832859 commit f3427e6

File tree

4 files changed

+47
-10
lines changed

4 files changed

+47
-10
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ Aside from the features listed above, **showify** also supports many more specia
193193
**Collections:**
194194

195195
- **Array:** `Array` objects are displayed as `[e1, e2, ...]`. Empty slots are displayed as `<${n} empty item${n === 1 ? "" : "s"}>`. For subclasses of `Array` or typed arrays, the `${className}` is displayed with its size, e.g., `MyArray(4) [1, <2 empty items>, 2, { foo: "bar" }]` or `Uint8Array(3) [1, 2, 3]`.
196+
- **Typed arrays:** Typed arrays (e.g., `Uint8Array`, `Int16Array`, etc.) are displayed as `${CtorName}(${length}) [v1, v2, ...]`. When `showHidden` is `"always"`, the following special properties are also displayed: `[BYTES_PER_ELEMENT]`, `[length]`, `[byteLength]`, `[byteOffset]`, and `[buffer]`.
196197
- **Long Arrays:** If `maxArrayLength` is set and the array has more elements than `maxArrayLength`, it is displayed as `[${e1}, ${e2}, ... ${n} more item${n === 1 ? "" : "s"}]`, e.g., `[1, 2, 3, 4, ... 2 more items]`.
197198
- **Map:** `Map` objects are displayed as `Map(${size}) { key1 => value1, key2 => value2, ... }`.
198199
- **Set:** `Set` objects are displayed as `Set(${size}) { value1, value2, ... }`.

packages/lite/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@ The following features are removed from the original package:
2222
- ANSI color support.
2323
- `quoteStyle` is limited to `"single"` or `"double"`.
2424
- The `showHidden: "exclude-meta"` option for `show()`.
25-
- Special support for `ArrayBuffer`, `SharedArrayBuffer` and `DataView`.
25+
- Special support for `ArrayBuffer`, `SharedArrayBuffer`, `DataView`, and special properties of typed arrays.

src/index.ts

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -812,7 +812,21 @@ function buildTree(
812812
if (className !== "Array" && "length" in value && typeof value.length === "number")
813813
prefix = text(`${className}(${value.length})`);
814814
// Hide `length` property if `showHidden` is `exclude-meta`
815-
if (showHidden === "exclude-meta") omittedKeys.add("length");
815+
if (showHidden === "exclude-meta") {
816+
omittedKeys.add("length");
817+
} else if (
818+
showHidden &&
819+
showHidden !== "none" &&
820+
// Show special properties for typed arrays
821+
ArrayBuffer.isView(value) &&
822+
!(value instanceof DataView)
823+
) {
824+
pushExtraProperty("BYTES_PER_ELEMENT", (value as Uint8Array).BYTES_PER_ELEMENT);
825+
pushExtraProperty("length", (value as Uint8Array).length);
826+
pushExtraProperty("byteLength", value.byteLength);
827+
pushExtraProperty("byteOffset", value.byteOffset);
828+
pushExtraProperty("buffer", value.buffer);
829+
}
816830
}
817831

818832
// Wrapper objects for primitives
@@ -894,14 +908,17 @@ function buildTree(
894908
),
895909
);
896910

897-
const formatKey = (key: string | symbol): string =>
898-
typeof key === "symbol" ? c.symbol(key.toString())
899-
// Always quote keys if `quoteKeys` is set to `"always"`
900-
: quoteKeys === "always" ? c.string(stringifyString(key, quoteStyle))
901-
// For string keys that are valid identifiers, we should show them as is
902-
: /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(key) ? key
903-
// For other string keys, we should wrap them with quotes
904-
: c.string(stringifyString(key, quoteStyle));
911+
function formatKey(key: string | symbol): string {
912+
return (
913+
typeof key === "symbol" ? c.symbol(key.toString())
914+
// Always quote keys if `quoteKeys` is set to `"always"`
915+
: quoteKeys === "always" ? c.string(stringifyString(key, quoteStyle))
916+
// For string keys that are valid identifiers, we should show them as is
917+
: /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(key) ? key
918+
// For other string keys, we should wrap them with quotes
919+
: c.string(stringifyString(key, quoteStyle))
920+
);
921+
}
905922

906923
// Object key/value pair
907924
const objectEntries = otherKeys.map((key) => {

test/array.spec.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,4 +248,23 @@ describe("Array", () => {
248248
util.inspect([1, 2, 3], { showHidden: true, colors: true }),
249249
);
250250
});
251+
252+
it('should show special properties of typed arrays when `showHidden` is not `"none"`', () => {
253+
const arr = new Uint8Array([1, 2, 3]);
254+
255+
expect(show(arr, { showHidden: "exclude-meta" })).toEqual("Uint8Array(3) [1, 2, 3]");
256+
257+
expect(show(arr, { showHidden: "always" })).toEqual(
258+
"Uint8Array(3) [1, 2, 3, [BYTES_PER_ELEMENT]: 1, [length]: 3, [byteLength]: 3, [byteOffset]: 0, [buffer]: ArrayBuffer { [Uint8Contents]: <01 02 03>, [byteLength]: 3 }]",
259+
);
260+
expect(inspect(arr, { showHidden: "always" })).toEqual(
261+
util
262+
.inspect(arr, { showHidden: true })
263+
// util.inspect does not show [Uint8Contents] for ArrayBuffer, seems to be BUG
264+
.replace(
265+
"ArrayBuffer { [byteLength]: 3 }",
266+
"ArrayBuffer { [Uint8Contents]: <01 02 03>, [byteLength]: 3 }",
267+
),
268+
);
269+
});
251270
});

0 commit comments

Comments
 (0)