Skip to content

Commit fd2e7dd

Browse files
committed
chore: use toStringTag getter
1 parent dd11dcb commit fd2e7dd

File tree

1 file changed

+13
-26
lines changed

1 file changed

+13
-26
lines changed

src/parser/utils.ts

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,24 @@
1-
export function isAnyArrayBuffer(value: unknown): value is ArrayBuffer {
2-
return (
3-
typeof value === 'object' &&
4-
value != null &&
5-
Symbol.toStringTag in value &&
6-
(value[Symbol.toStringTag] === 'ArrayBuffer' ||
7-
value[Symbol.toStringTag] === 'SharedArrayBuffer')
8-
);
9-
}
1+
const TypedArrayPrototypeGetSymbolToStringTag = (() => {
2+
// eslint-disable-next-line @typescript-eslint/unbound-method -- the intention is to call this method with a bound value
3+
const g = Object.getOwnPropertyDescriptor(
4+
Object.getPrototypeOf(Uint8Array.prototype),
5+
Symbol.toStringTag
6+
)!.get!;
107

11-
export function isUint8Array(value: unknown): value is Uint8Array {
12-
return (
13-
typeof value === 'object' &&
14-
value != null &&
15-
Symbol.toStringTag in value &&
16-
value[Symbol.toStringTag] === 'Uint8Array'
17-
);
18-
}
8+
return (value: unknown) => g.call(value);
9+
})();
1910

20-
export function isBigInt64Array(value: unknown): value is BigInt64Array {
21-
return (
22-
typeof value === 'object' &&
23-
value != null &&
24-
Symbol.toStringTag in value &&
25-
value[Symbol.toStringTag] === 'BigInt64Array'
26-
);
11+
export function isUint8Array(value: unknown): value is Uint8Array {
12+
return TypedArrayPrototypeGetSymbolToStringTag(value) === 'Uint8Array';
2713
}
2814

29-
export function isBigUInt64Array(value: unknown): value is BigUint64Array {
15+
export function isAnyArrayBuffer(value: unknown): value is ArrayBuffer {
3016
return (
3117
typeof value === 'object' &&
3218
value != null &&
3319
Symbol.toStringTag in value &&
34-
value[Symbol.toStringTag] === 'BigUint64Array'
20+
(value[Symbol.toStringTag] === 'ArrayBuffer' ||
21+
value[Symbol.toStringTag] === 'SharedArrayBuffer')
3522
);
3623
}
3724

0 commit comments

Comments
 (0)