Skip to content

Commit

Permalink
chore: use toStringTag getter
Browse files Browse the repository at this point in the history
  • Loading branch information
nbbeeken committed Nov 26, 2024
1 parent dd11dcb commit fd2e7dd
Showing 1 changed file with 13 additions and 26 deletions.
39 changes: 13 additions & 26 deletions src/parser/utils.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,24 @@
export function isAnyArrayBuffer(value: unknown): value is ArrayBuffer {
return (
typeof value === 'object' &&
value != null &&
Symbol.toStringTag in value &&
(value[Symbol.toStringTag] === 'ArrayBuffer' ||
value[Symbol.toStringTag] === 'SharedArrayBuffer')
);
}
const TypedArrayPrototypeGetSymbolToStringTag = (() => {
// eslint-disable-next-line @typescript-eslint/unbound-method -- the intention is to call this method with a bound value
const g = Object.getOwnPropertyDescriptor(
Object.getPrototypeOf(Uint8Array.prototype),
Symbol.toStringTag
)!.get!;

export function isUint8Array(value: unknown): value is Uint8Array {
return (
typeof value === 'object' &&
value != null &&
Symbol.toStringTag in value &&
value[Symbol.toStringTag] === 'Uint8Array'
);
}
return (value: unknown) => g.call(value);
})();

export function isBigInt64Array(value: unknown): value is BigInt64Array {
return (
typeof value === 'object' &&
value != null &&
Symbol.toStringTag in value &&
value[Symbol.toStringTag] === 'BigInt64Array'
);
export function isUint8Array(value: unknown): value is Uint8Array {
return TypedArrayPrototypeGetSymbolToStringTag(value) === 'Uint8Array';
}

export function isBigUInt64Array(value: unknown): value is BigUint64Array {
export function isAnyArrayBuffer(value: unknown): value is ArrayBuffer {
return (
typeof value === 'object' &&
value != null &&
Symbol.toStringTag in value &&
value[Symbol.toStringTag] === 'BigUint64Array'
(value[Symbol.toStringTag] === 'ArrayBuffer' ||
value[Symbol.toStringTag] === 'SharedArrayBuffer')
);
}

Expand Down

0 comments on commit fd2e7dd

Please sign in to comment.