Skip to content

Commit ced10d7

Browse files
committed
chore: remove WeakMap cache of types and use toStringTag getter
1 parent e251a8a commit ced10d7

File tree

1 file changed

+31
-7
lines changed

1 file changed

+31
-7
lines changed

src/parser/utils.ts

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,51 @@
11
export function isAnyArrayBuffer(value: unknown): value is ArrayBuffer {
2-
return ['[object ArrayBuffer]', '[object SharedArrayBuffer]'].includes(
3-
Object.prototype.toString.call(value)
2+
return (
3+
typeof value === 'object' &&
4+
value != null &&
5+
Symbol.toStringTag in value &&
6+
(value[Symbol.toStringTag] === 'ArrayBuffer' ||
7+
value[Symbol.toStringTag] === 'SharedArrayBuffer')
48
);
59
}
610

711
export function isUint8Array(value: unknown): value is Uint8Array {
8-
return Object.prototype.toString.call(value) === '[object Uint8Array]';
12+
return (
13+
typeof value === 'object' &&
14+
value != null &&
15+
Symbol.toStringTag in value &&
16+
value[Symbol.toStringTag] === 'Uint8Array'
17+
);
918
}
1019

1120
export function isBigInt64Array(value: unknown): value is BigInt64Array {
12-
return Object.prototype.toString.call(value) === '[object BigInt64Array]';
21+
return (
22+
typeof value === 'object' &&
23+
value != null &&
24+
Symbol.toStringTag in value &&
25+
value[Symbol.toStringTag] === 'BigInt64Array'
26+
);
1327
}
1428

1529
export function isBigUInt64Array(value: unknown): value is BigUint64Array {
16-
return Object.prototype.toString.call(value) === '[object BigUint64Array]';
30+
return (
31+
typeof value === 'object' &&
32+
value != null &&
33+
Symbol.toStringTag in value &&
34+
value[Symbol.toStringTag] === 'BigUint64Array'
35+
);
1736
}
1837

1938
export function isRegExp(d: unknown): d is RegExp {
2039
return Object.prototype.toString.call(d) === '[object RegExp]';
2140
}
2241

23-
export function isMap(d: unknown): d is Map<unknown, unknown> {
24-
return Object.prototype.toString.call(d) === '[object Map]';
42+
export function isMap(value: unknown): d is Map<unknown, unknown> {
43+
return (
44+
typeof value === 'object' &&
45+
value != null &&
46+
Symbol.toStringTag in value &&
47+
value[Symbol.toStringTag] === 'Map'
48+
);
2549
}
2650

2751
export function isDate(d: unknown): d is Date {

0 commit comments

Comments
 (0)