Skip to content

Commit d73f20c

Browse files
support for when isView is missing
1 parent 81e5bf7 commit d73f20c

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

underscore.js

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@
1919
// Save the previous value of the `_` variable.
2020
var previousUnderscore = root._;
2121

22-
// not every runtime supports ArrayBuffer
23-
var supportsArrayBuffer = typeof ArrayBuffer !== 'undefined';
24-
2522
// Save bytes in the minified (but not gzipped) version:
2623
var ArrayProto = Array.prototype, ObjProto = Object.prototype;
2724
var SymbolProto = typeof Symbol !== 'undefined' ? Symbol.prototype : null;
@@ -48,6 +45,27 @@
4845
this._wrapped = obj;
4946
};
5047

48+
// not every runtime supports ArrayBuffer
49+
var supportsArrayBuffer = typeof ArrayBuffer !== 'undefined';
50+
// list from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Typed_arrays
51+
var typedArrayNames = [
52+
'[object Int8Array]',
53+
'[object Int16Array]',
54+
'[object Int32Array]',
55+
'[object Uint8Array]',
56+
'[object Uint8ClampedArray]',
57+
'[object Uint16Array]',
58+
'[object Uint32Array]',
59+
'[object Float32Array]',
60+
'[object Float64Array]'
61+
];
62+
var isTypedArray = function(a) {
63+
// second check is from the above whitelist, but the first check is more future proof
64+
// since new typed arrays may arrive
65+
return (supportsArrayBuffer && ArrayBuffer.isView && ArrayBuffer.isView(a) && !(a instanceof DataView))
66+
|| _.contains(typedArrayNames, toString.call(a));
67+
};
68+
5169
// Export the Underscore object for **Node.js**, with
5270
// backwards-compatibility for their old module API. If we're in
5371
// the browser, add `_` as a global object.
@@ -1208,7 +1226,7 @@
12081226
if (className !== toString.call(b)) return false;
12091227

12101228
// isView returns true when it's a typed array or DataView
1211-
if (supportsArrayBuffer && ArrayBuffer.isView(a) && !(a instanceof DataView)) {
1229+
if (isTypedArray(a)) {
12121230
// If a and b are of the same typed array, we compare them as DataView
12131231
return deepEq(new DataView(a.buffer), new DataView(b.buffer), aStack, bStack);
12141232
}

0 commit comments

Comments
 (0)