Skip to content

Commit 93be5d7

Browse files
committed
Detect type support and only create supported type methods
1 parent 260dca0 commit 93be5d7

File tree

1 file changed

+65
-40
lines changed

1 file changed

+65
-40
lines changed

support/types.js

Lines changed: 65 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,36 @@ function uncurryThis(f) {
77
return f.call.bind(f);
88
}
99

10-
var TypedArrayPrototype = Object.getPrototypeOf(Uint8Array.prototype);
10+
var supported = {
11+
BigInt: typeof BigInt !== 'undefined',
12+
Symbol: typeof Symbol !== 'undefined',
13+
Uint8Array: typeof Uint8Array !== 'undefined',
14+
ArrayBuffer: typeof ArrayBuffer !== 'undefined'
15+
};
1116

12-
var TypedArrayProto_toStringTag =
13-
uncurryThis(
14-
Object.getOwnPropertyDescriptor(TypedArrayPrototype,
15-
Symbol.toStringTag).get);
17+
if (supported.Uint8Array) {
18+
var TypedArrayPrototype = Object.getPrototypeOf(Uint8Array.prototype);
19+
20+
var TypedArrayProto_toStringTag =
21+
uncurryThis(
22+
Object.getOwnPropertyDescriptor(TypedArrayPrototype,
23+
Symbol.toStringTag).get);
24+
25+
}
1626

1727
var ObjectToString = uncurryThis(Object.prototype.toString);
1828

1929
var numberValue = uncurryThis(Number.prototype.valueOf);
2030
var stringValue = uncurryThis(String.prototype.valueOf);
2131
var booleanValue = uncurryThis(Boolean.prototype.valueOf);
22-
var bigIntValue = uncurryThis(BigInt.prototype.valueOf);
23-
var symbolValue = uncurryThis(Symbol.prototype.valueOf);
32+
33+
if (supported.BigInt) {
34+
var bigIntValue = uncurryThis(BigInt.prototype.valueOf);
35+
}
36+
37+
if (supported.Symbol) {
38+
var symbolValue = uncurryThis(Symbol.prototype.valueOf);
39+
}
2440

2541
function checkBoxedPrimitive(value, prototypeValueOf) {
2642
if (typeof value !== 'object') {
@@ -35,78 +51,95 @@ function checkBoxedPrimitive(value, prototypeValueOf) {
3551
}
3652

3753
var methods = [
54+
{
55+
name: 'isArgumentsObject',
56+
function: require('is-arguments'),
57+
},
3858
{
3959
name: 'isArrayBufferView',
60+
supported: supported.ArrayBuffer,
4061
function: ArrayBuffer.isView,
4162
},
4263
{
4364
name: 'isTypedArray',
65+
supported: supported.Uint8Array,
4466
function: function(value) {
4567
return TypedArrayProto_toStringTag(value) !== undefined;
4668
}
4769
},
4870
{
4971
name: 'isUint8Array',
72+
supported: supported.Uint8Array,
5073
function: function(value) {
5174
return TypedArrayProto_toStringTag(value) === 'Uint8Array';
5275
}
5376
},
5477
{
5578
name: 'isUint8ClampedArray',
79+
supported: supported.Uint8Array,
5680
function: function(value) {
5781
return TypedArrayProto_toStringTag(value) === 'Uint8ClampedArray';
5882
}
5983
},
6084
{
6185
name: 'isUint16Array',
86+
supported: supported.Uint8Array,
6287
function: function(value) {
6388
return TypedArrayProto_toStringTag(value) === 'Uint16Array';
6489
}
6590
},
6691
{
6792
name: 'isUint32Array',
93+
supported: supported.Uint8Array,
6894
function: function(value) {
6995
return TypedArrayProto_toStringTag(value) === 'Uint32Array';
7096
}
7197
},
7298
{
7399
name: 'isInt8Array',
100+
supported: supported.Uint8Array,
74101
function: function(value) {
75102
return TypedArrayProto_toStringTag(value) === 'Int8Array';
76103
}
77104
},
78105
{
79106
name: 'isInt16Array',
107+
supported: supported.Uint8Array,
80108
function: function(value) {
81109
return TypedArrayProto_toStringTag(value) === 'Int16Array';
82110
}
83111
},
84112
{
85113
name: 'isInt32Array',
114+
supported: supported.Uint8Array,
86115
function: function(value) {
87116
return TypedArrayProto_toStringTag(value) === 'Int32Array';
88117
}
89118
},
90119
{
91120
name: 'isFloat32Array',
121+
supported: supported.Uint8Array,
92122
function: function(value) {
93123
return TypedArrayProto_toStringTag(value) === 'Float32Array';
94124
}
95125
},
96126
{
97127
name: 'isFloat64Array',
128+
supported: supported.Uint8Array,
98129
function: function(value) {
99130
return TypedArrayProto_toStringTag(value) === 'Float64Array';
100131
}
101132
},
102133
{
103134
name: 'isBigInt64Array',
135+
supported: supported.Uint8Array,
104136
function: function(value) {
105137
return TypedArrayProto_toStringTag(value) === 'BigInt64Array';
106138
}
107139
},
108140
{
109141
name: 'isBigUint64Array',
142+
supported: supported.Uint8Array,
110143
function: function(value) {
111144
return TypedArrayProto_toStringTag(value) === 'BigUint64Array';
112145
}
@@ -153,12 +186,6 @@ var methods = [
153186
return ObjectToString(value) === '[object DataView]';
154187
}
155188
},
156-
{
157-
name: 'isArrayBuffer',
158-
function: function(value) {
159-
return ObjectToString(value) === '[object ArrayBuffer]';
160-
}
161-
},
162189
{
163190
name: 'isSharedArrayBuffer',
164191
function: function(value) {
@@ -221,12 +248,14 @@ var methods = [
221248
},
222249
{
223250
name: 'isBigIntObject',
251+
supported: supported.BigInt,
224252
function: function(value) {
225253
return checkBoxedPrimitive(value, bigIntValue);
226254
}
227255
},
228256
{
229257
name: 'isSymbolObject',
258+
supported: supported.Symbol,
230259
function: function(value) {
231260
return checkBoxedPrimitive(value, symbolValue);
232261
}
@@ -238,45 +267,41 @@ var methods = [
238267
exports.isNumberObject(value) ||
239268
exports.isStringObject(value) ||
240269
exports.isBooleanObject(value) ||
241-
exports.isBigIntObject(value) ||
242-
exports.isSymbolObject(value)
270+
(supported.BigInt && exports.isBigIntObject(value)) ||
271+
(supported.Symbol && exports.isSymbolObject(value))
243272
);
244273
}
245274
},
246275
{
247276
name: 'isAnyArrayBuffer',
277+
supported: supported.Uint8Array,
248278
function: function(value) {
249279
return (
250280
exports.isArrayBuffer(value) ||
251281
exports.isSharedArrayBuffer(value)
252282
);
253283
}
254-
}
255-
];
256-
257-
methods.forEach(function(method) {
258-
exports[method.name] = method.function;
259-
});
260-
261-
Object.defineProperty(exports, 'isProxy', {
262-
enumerable: false,
263-
value: function() {
264-
throw new Error('Proxies can not be detected in userland');
265284
},
266-
});
267-
268-
Object.defineProperty(exports, 'isExternal', {
269-
enumerable: false,
270-
value: function() {
271-
throw new Error('External values can not be detected in userland');
285+
{
286+
name: 'isProxy',
287+
supported: false
272288
},
273-
});
274-
275-
Object.defineProperty(exports, 'isModuleNamespaceObject', {
276-
enumerable: false,
277-
value: function() {
278-
throw new Error('Module namespace objects are not supported');
289+
{
290+
name: 'isExternal',
291+
supported: false
279292
},
280-
});
293+
{
294+
name: 'isModuleNamespaceObject',
295+
supported: false
296+
},
297+
];
281298

282-
exports.isArgumentsObject = require('is-arguments');
299+
methods.forEach(function(method) {
300+
const supported = method.supported !== false;
301+
Object.defineProperty(exports, method.name, {
302+
enumerable: supported,
303+
value: supported ? method.function : function() {
304+
throw new Error(method.name + ' is not supported');
305+
}
306+
});
307+
});

0 commit comments

Comments
 (0)