Skip to content

Commit bfbff20

Browse files
author
Will Binns-Smith
committed
Use which-typed-array and is-typed-array for typed array detection
1 parent ee6c104 commit bfbff20

File tree

3 files changed

+20
-101
lines changed

3 files changed

+20
-101
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ script:
1212
- 'npm test'
1313
# Run browser tests on one node version.
1414
- 'if [ "${TRAVIS_PULL_REQUEST}" = "false" ] && [ "${TRAVIS_NODE_VERSION}" = "stable" ]; then npm run test:browsers; fi'
15-
- 'if [ "${TRAVIS_PULL_REQUEST}" = "false" ] && [ "${TRAVIS_NODE_VERSION}" = "stable" ]; then npm run test:browsers:polyfills; fi'
15+
- 'if [ "${TRAVIS_PULL_REQUEST}" = "false" ] && [ "${TRAVIS_NODE_VERSION}" = "stable" ]; then npm run test:browsers:with-polyfills; fi'
1616
addons:
1717
sauce_connect: true
1818
hosts:

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
"inherits": "^2.0.3",
1414
"is-arguments": "^1.0.4",
1515
"is-generator-function": "^1.0.7",
16-
"safe-buffer": "^5.1.2"
16+
"is-typed-array": "^1.1.3",
17+
"safe-buffer": "^5.1.2",
18+
"which-typed-array": "^1.1.2"
1719
},
1820
"devDependencies": {
1921
"airtap": "~1.0.0",

support/types.js

Lines changed: 16 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,17 @@
33

44
'use strict';
55

6-
var isBuffer = require('./isBuffer');
7-
86
var isArgumentsObject = require('is-arguments');
97
var isGeneratorFunction = require('is-generator-function');
8+
var whichTypedArray = require('which-typed-array');
9+
var isTypedArray = require('is-typed-array');
1010

1111
function uncurryThis(f) {
1212
return f.call.bind(f);
1313
}
1414

1515
var BigIntSupported = typeof BigInt !== 'undefined';
1616
var SymbolSupported = typeof Symbol !== 'undefined';
17-
var SymbolToStringTagSupported = SymbolSupported && typeof Symbol.toStringTag !== 'undefined';
18-
var Uint8ArraySupported = typeof Uint8Array !== 'undefined';
19-
var ArrayBufferSupported = typeof ArrayBuffer !== 'undefined';
20-
21-
if (Uint8ArraySupported && SymbolToStringTagSupported) {
22-
var TypedArrayPrototype = Object.getPrototypeOf(Uint8Array.prototype);
23-
24-
var TypedArrayProto_toStringTag =
25-
uncurryThis(
26-
Object.getOwnPropertyDescriptor(TypedArrayPrototype,
27-
Symbol.toStringTag).get);
28-
29-
}
3017

3118
var ObjectToString = uncurryThis(Object.prototype.toString);
3219

@@ -55,8 +42,8 @@ function checkBoxedPrimitive(value, prototypeValueOf) {
5542
}
5643

5744
exports.isArgumentsObject = isArgumentsObject;
58-
5945
exports.isGeneratorFunction = isGeneratorFunction;
46+
exports.isTypedArray = isTypedArray;
6047

6148
// Taken from here and modified for better browser support
6249
// https://github.com/sindresorhus/p-is-promise/blob/cda35a513bda03f977ad5cde3a079d237e82d7ef/index.js
@@ -77,7 +64,7 @@ function isPromise(input) {
7764
exports.isPromise = isPromise;
7865

7966
function isArrayBufferView(value) {
80-
if (ArrayBufferSupported && ArrayBuffer.isView) {
67+
if (typeof ArrayBuffer !== 'undefined' && ArrayBuffer.isView) {
8168
return ArrayBuffer.isView(value);
8269
}
8370

@@ -88,129 +75,59 @@ function isArrayBufferView(value) {
8875
}
8976
exports.isArrayBufferView = isArrayBufferView;
9077

91-
function isTypedArray(value) {
92-
if (Uint8ArraySupported && SymbolToStringTagSupported) {
93-
return TypedArrayProto_toStringTag(value) !== undefined;
94-
} else {
95-
return (
96-
isUint8Array(value) ||
97-
isUint8ClampedArray(value) ||
98-
isUint16Array(value) ||
99-
isUint32Array(value) ||
100-
isInt8Array(value) ||
101-
isInt16Array(value) ||
102-
isInt32Array(value) ||
103-
isFloat32Array(value) ||
104-
isFloat64Array(value) ||
105-
isBigInt64Array(value) ||
106-
isBigUint64Array(value)
107-
);
108-
}
109-
}
110-
exports.isTypedArray = isTypedArray;
11178

11279
function isUint8Array(value) {
113-
if (Uint8ArraySupported && SymbolToStringTagSupported) {
114-
return TypedArrayProto_toStringTag(value) === 'Uint8Array';
115-
} else {
116-
return (
117-
ObjectToString(value) === '[object Uint8Array]' ||
118-
// If it's a Buffer instance _and_ has a `.buffer` property,
119-
// this is an ArrayBuffer based buffer; thus it's an Uint8Array
120-
// (Old Node.js had a custom non-Uint8Array implementation)
121-
isBuffer(value) && value.buffer !== undefined
122-
);
123-
}
80+
return whichTypedArray(value) === 'Uint8Array';
12481
}
12582
exports.isUint8Array = isUint8Array;
12683

12784
function isUint8ClampedArray(value) {
128-
if (Uint8ArraySupported && SymbolToStringTagSupported) {
129-
return TypedArrayProto_toStringTag(value) === 'Uint8ClampedArray';
130-
} else {
131-
return ObjectToString(value) === '[object Uint8ClampedArray]';
132-
}
85+
return whichTypedArray(value) === 'Uint8ClampedArray';
13386
}
13487
exports.isUint8ClampedArray = isUint8ClampedArray;
13588

13689
function isUint16Array(value) {
137-
if (Uint8ArraySupported && SymbolToStringTagSupported) {
138-
return TypedArrayProto_toStringTag(value) === 'Uint16Array';
139-
} else {
140-
return ObjectToString(value) === '[object Uint16Array]';
141-
}
90+
return whichTypedArray(value) === 'Uint16Array';
14291
}
14392
exports.isUint16Array = isUint16Array;
14493

14594
function isUint32Array(value) {
146-
if (Uint8ArraySupported && SymbolToStringTagSupported) {
147-
return TypedArrayProto_toStringTag(value) === 'Uint32Array';
148-
} else {
149-
return ObjectToString(value) === '[object Uint32Array]';
150-
}
95+
return whichTypedArray(value) === 'Uint32Array';
15196
}
15297
exports.isUint32Array = isUint32Array;
15398

15499
function isInt8Array(value) {
155-
if (Uint8ArraySupported && SymbolToStringTagSupported) {
156-
return TypedArrayProto_toStringTag(value) === 'Int8Array';
157-
} else {
158-
return ObjectToString(value) === '[object Int8Array]';
159-
}
100+
return whichTypedArray(value) === 'Int8Array';
160101
}
161102
exports.isInt8Array = isInt8Array;
162103

163104
function isInt16Array(value) {
164-
if (Uint8ArraySupported && SymbolToStringTagSupported) {
165-
return TypedArrayProto_toStringTag(value) === 'Int16Array';
166-
} else {
167-
return ObjectToString(value) === '[object Int16Array]';
168-
}
105+
return whichTypedArray(value) === 'Int16Array';
169106
}
170107
exports.isInt16Array = isInt16Array;
171108

172109
function isInt32Array(value) {
173-
if (Uint8ArraySupported && SymbolToStringTagSupported) {
174-
return TypedArrayProto_toStringTag(value) === 'Int32Array';
175-
} else {
176-
return ObjectToString(value) === '[object Int32Array]';
177-
}
110+
return whichTypedArray(value) === 'Int32Array';
178111
}
179112
exports.isInt32Array = isInt32Array;
180113

181114
function isFloat32Array(value) {
182-
if (Uint8ArraySupported && SymbolToStringTagSupported) {
183-
return TypedArrayProto_toStringTag(value) === 'Float32Array';
184-
} else {
185-
return ObjectToString(value) === '[object Float32Array]';
186-
}
115+
return whichTypedArray(value) === 'Float32Array';
187116
}
188117
exports.isFloat32Array = isFloat32Array;
189118

190119
function isFloat64Array(value) {
191-
if (Uint8ArraySupported && SymbolToStringTagSupported) {
192-
return TypedArrayProto_toStringTag(value) === 'Float64Array';
193-
} else {
194-
return ObjectToString(value) === '[object Float64Array]';
195-
}
120+
return whichTypedArray(value) === 'Float64Array';
196121
}
197122
exports.isFloat64Array = isFloat64Array;
198123

199124
function isBigInt64Array(value) {
200-
if (Uint8ArraySupported && SymbolToStringTagSupported) {
201-
return TypedArrayProto_toStringTag(value) === 'BigInt64Array';
202-
} else {
203-
return ObjectToString(value) === '[object BigInt64Array]';
204-
}
125+
return whichTypedArray(value) === 'BigInt64Array';
205126
}
206127
exports.isBigInt64Array = isBigInt64Array;
207128

208129
function isBigUint64Array(value) {
209-
if (Uint8ArraySupported && SymbolToStringTagSupported) {
210-
return TypedArrayProto_toStringTag(value) === 'BigUint64Array';
211-
} else {
212-
return ObjectToString(value) === '[object BigUint64Array]';
213-
}
130+
return whichTypedArray(value) === 'BigUint64Array';
214131
}
215132
exports.isBigUint64Array = isBigUint64Array;
216133

@@ -405,7 +322,7 @@ function isBoxedPrimitive(value) {
405322
exports.isBoxedPrimitive = isBoxedPrimitive;
406323

407324
function isAnyArrayBuffer(value) {
408-
return Uint8ArraySupported && (
325+
return typeof Uint8Array !== 'undefined' && (
409326
isArrayBuffer(value) ||
410327
isSharedArrayBuffer(value)
411328
);

0 commit comments

Comments
 (0)