Skip to content

Commit e2b3cf3

Browse files
committed
Fall back to less accurate typed array check if Symbol.ToStringTag is undefined
1 parent b8b3b95 commit e2b3cf3

File tree

1 file changed

+72
-25
lines changed

1 file changed

+72
-25
lines changed

support/types.js

Lines changed: 72 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,13 @@ function uncurryThis(f) {
99
return f.call.bind(f);
1010
}
1111

12-
var supported = {
13-
BigInt: typeof BigInt !== 'undefined',
14-
Symbol: typeof Symbol !== 'undefined',
15-
Uint8Array: typeof Uint8Array !== 'undefined',
16-
ArrayBuffer: typeof ArrayBuffer !== 'undefined'
17-
};
12+
var BigIntSupported = typeof BigInt !== 'undefined';
13+
var SymbolSupported = typeof Symbol !== 'undefined';
14+
var SymbolToStringTagSupported = SymbolSupported && typeof Symbol.toStringTag !== 'undefined';
15+
var Uint8ArraySupported = typeof Uint8Array !== 'undefined';
16+
var ArrayBufferSupported = typeof ArrayBuffer !== 'undefined';
1817

19-
if (supported.Uint8Array) {
18+
if (Uint8ArraySupported && SymbolToStringTagSupported) {
2019
var TypedArrayPrototype = Object.getPrototypeOf(Uint8Array.prototype);
2120

2221
var TypedArrayProto_toStringTag =
@@ -32,11 +31,11 @@ var numberValue = uncurryThis(Number.prototype.valueOf);
3231
var stringValue = uncurryThis(String.prototype.valueOf);
3332
var booleanValue = uncurryThis(Boolean.prototype.valueOf);
3433

35-
if (supported.BigInt) {
34+
if (BigIntSupported) {
3635
var bigIntValue = uncurryThis(BigInt.prototype.valueOf);
3736
}
3837

39-
if (supported.Symbol) {
38+
if (SymbolSupported) {
4039
var symbolValue = uncurryThis(Symbol.prototype.valueOf);
4140
}
4241

@@ -60,79 +59,127 @@ var methods = [
6059
{
6160
name: 'isArrayBufferView',
6261
function: function isArrayBufferView(value) {
63-
return supported.ArrayBuffer && ArrayBuffer.isView(value);
62+
return ArrayBufferSupported && ArrayBuffer.isView(value);
6463
},
6564
},
6665
{
6766
name: 'isTypedArray',
6867
function: function isTypedArray(value) {
69-
return supported.Uint8Array && TypedArrayProto_toStringTag(value) !== undefined;
68+
if (Uint8ArraySupported && SymbolToStringTagSupported) {
69+
return TypedArrayProto_toStringTag(value) !== undefined;
70+
} else {
71+
return ObjectToString(value) === '[object TypedArray]';
72+
}
7073
}
7174
},
7275
{
7376
name: 'isUint8Array',
7477
function: function isUint8Array(value) {
75-
return supported.Uint8Array && TypedArrayProto_toStringTag(value) === 'Uint8Array';
78+
if (Uint8ArraySupported && SymbolToStringTagSupported) {
79+
return TypedArrayProto_toStringTag(value) === 'Uint8Array';
80+
} else {
81+
return ObjectToString(value) === '[object Uint8Array]';
82+
}
7683
}
7784
},
7885
{
7986
name: 'isUint8ClampedArray',
8087
function: function isUint8ClampedArray(value) {
81-
return supported.Uint8Array && TypedArrayProto_toStringTag(value) === 'Uint8ClampedArray';
88+
if (Uint8ArraySupported && SymbolToStringTagSupported) {
89+
return TypedArrayProto_toStringTag(value) === 'Uint8ClampedArray';
90+
} else {
91+
return ObjectToString(value) === '[object Uint8ClampedArray]';
92+
}
8293
}
8394
},
8495
{
8596
name: 'isUint16Array',
8697
function: function isUint16Array(value) {
87-
return supported.Uint8Array && TypedArrayProto_toStringTag(value) === 'Uint16Array';
98+
if (Uint8ArraySupported && SymbolToStringTagSupported) {
99+
return TypedArrayProto_toStringTag(value) === 'Uint16Array';
100+
} else {
101+
return ObjectToString(value) === '[object Uint16Array]';
102+
}
88103
}
89104
},
90105
{
91106
name: 'isUint32Array',
92107
function: function isUint32Array(value) {
93-
return supported.Uint8Array && TypedArrayProto_toStringTag(value) === 'Uint32Array';
108+
if (Uint8ArraySupported && SymbolToStringTagSupported) {
109+
return TypedArrayProto_toStringTag(value) === 'Uint32Array';
110+
} else {
111+
return ObjectToString(value) === '[object Uint32Array]';
112+
}
94113
}
95114
},
96115
{
97116
name: 'isInt8Array',
98117
function: function isInt8Array(value) {
99-
return supported.Uint8Array && TypedArrayProto_toStringTag(value) === 'Int8Array';
118+
if (Uint8ArraySupported && SymbolToStringTagSupported) {
119+
return TypedArrayProto_toStringTag(value) === 'Int8Array';
120+
} else {
121+
return ObjectToString(value) === '[object Int8Array]';
122+
}
100123
}
101124
},
102125
{
103126
name: 'isInt16Array',
104127
function: function isInt16Array(value) {
105-
return supported.Uint8Array && TypedArrayProto_toStringTag(value) === 'Int16Array';
128+
if (Uint8ArraySupported && SymbolToStringTagSupported) {
129+
return TypedArrayProto_toStringTag(value) === 'Int16Array';
130+
} else {
131+
return ObjectToString(value) === '[object Int16Array]';
132+
}
106133
}
107134
},
108135
{
109136
name: 'isInt32Array',
110137
function: function isInt32Array(value) {
111-
return supported.Uint8Array && TypedArrayProto_toStringTag(value) === 'Int32Array';
138+
if (Uint8ArraySupported && SymbolToStringTagSupported) {
139+
return TypedArrayProto_toStringTag(value) === 'Int32Array';
140+
} else {
141+
return ObjectToString(value) === '[object Int32Array]';
142+
}
112143
}
113144
},
114145
{
115146
name: 'isFloat32Array',
116147
function: function isFloat32Array(value) {
117-
return supported.Uint8Array && TypedArrayProto_toStringTag(value) === 'Float32Array';
148+
if (Uint8ArraySupported && SymbolToStringTagSupported) {
149+
return TypedArrayProto_toStringTag(value) === 'Float32Array';
150+
} else {
151+
return ObjectToString(value) === '[object Float32Array]';
152+
}
118153
}
119154
},
120155
{
121156
name: 'isFloat64Array',
122157
function: function isFloat64Array(value) {
123-
return supported.Uint8Array && TypedArrayProto_toStringTag(value) === 'Float64Array';
158+
if (Uint8ArraySupported && SymbolToStringTagSupported) {
159+
return TypedArrayProto_toStringTag(value) === 'Float64Array';
160+
} else {
161+
return ObjectToString(value) === '[object Float64Array]';
162+
}
124163
}
125164
},
126165
{
127166
name: 'isBigInt64Array',
128167
function: function isBigInt64Array(value) {
129-
return supported.Uint8Array && TypedArrayProto_toStringTag(value) === 'BigInt64Array';
168+
if (Uint8ArraySupported && SymbolToStringTagSupported) {
169+
return TypedArrayProto_toStringTag(value) === 'BigInt64Array';
170+
} else {
171+
return ObjectToString(value) === '[object BigInt64Array]';
172+
}
130173
}
131174
},
132175
{
133176
name: 'isBigUint64Array',
134177
function: function isBigUint64Array(value) {
135-
return supported.Uint8Array && TypedArrayProto_toStringTag(value) === 'BigUint64Array';
178+
if (Uint8ArraySupported && SymbolToStringTagSupported) {
179+
return TypedArrayProto_toStringTag(value) === 'BigUint64Array';
180+
} else {
181+
return ObjectToString(value) === '[object BigUint64Array]';
182+
}
136183
}
137184
},
138185
{
@@ -240,13 +287,13 @@ var methods = [
240287
{
241288
name: 'isBigIntObject',
242289
function: function isBigIntObject(value) {
243-
return supported.BigInt && checkBoxedPrimitive(value, bigIntValue);
290+
return BigIntSupported && checkBoxedPrimitive(value, bigIntValue);
244291
}
245292
},
246293
{
247294
name: 'isSymbolObject',
248295
function: function isSymbolObject(value) {
249-
return supported.Symbol && checkBoxedPrimitive(value, symbolValue);
296+
return SymbolSupported && checkBoxedPrimitive(value, symbolValue);
250297
}
251298
},
252299
{
@@ -264,7 +311,7 @@ var methods = [
264311
{
265312
name: 'isAnyArrayBuffer',
266313
function: function isAnyArrayBuffer(value) {
267-
return supported.Uint8Array && (
314+
return Uint8ArraySupported && (
268315
exports.isArrayBuffer(value) ||
269316
exports.isSharedArrayBuffer(value)
270317
);

0 commit comments

Comments
 (0)