diff --git a/.eslintrc b/.eslintrc index 66ef141..eab8409 100755 --- a/.eslintrc +++ b/.eslintrc @@ -15,6 +15,11 @@ "no-magic-numbers": [2, { "ignore": [0, 1] }], }, + "globals": { + "Int8Array": false, + "Uint8Array": false, + }, + "overrides": [ { "files": "example/**", diff --git a/index.js b/index.js index cdd1cc7..9fc94a9 100644 --- a/index.js +++ b/index.js @@ -15,20 +15,10 @@ var getIterator = require('es-get-iterator'); var getSideChannel = require('side-channel'); var whichTypedArray = require('which-typed-array'); var assign = require('object.assign'); +var isArrayBuffer = require('is-array-buffer'); -// TODO: use extracted package -var byteLength = callBound('ArrayBuffer.prototype.byteLength', true); -function isArrayBuffer(buffer) { - if (!buffer || typeof buffer !== 'object' || !byteLength) { - return false; - } - try { - byteLength(buffer); - return true; - } catch (e) { - return false; - } -} +var byteLength = callBound('%ArrayBuffer.prototype.byteLength%', true) + || function byteLength(ab) { return ab.byteLength; }; // in node < 0.11, byteLength is an own nonconfigurable property var $getTime = callBound('Date.prototype.getTime'); var gPO = Object.getPrototypeOf; @@ -342,7 +332,6 @@ function objEquiv(a, b, opts, channel) { if (aIsArrayBuffer !== bIsArrayBuffer) { return false; } if (aIsArrayBuffer || bIsArrayBuffer) { // && would work too, because both are true or both false here if (byteLength(a) !== byteLength(b)) { return false; } - /* global Uint8Array */ return typeof Uint8Array === 'function' && internalDeepEqual(new Uint8Array(a), new Uint8Array(b), opts, channel); } diff --git a/package.json b/package.json index 1b3d137..1b04a8e 100755 --- a/package.json +++ b/package.json @@ -35,6 +35,7 @@ "es-get-iterator": "^1.1.2", "get-intrinsic": "^1.1.3", "is-arguments": "^1.1.1", + "is-array-buffer": "^3.0.1", "is-date-object": "^1.0.5", "is-regex": "^1.1.4", "isarray": "^2.0.5", diff --git a/test/cmp.js b/test/cmp.js index 721a422..c4f5376 100644 --- a/test/cmp.js +++ b/test/cmp.js @@ -1159,7 +1159,6 @@ test('TypedArrays', { skip: !hasTypedArrays }, function (t) { }); t.test('one TypedArray faking as another', { skip: !hasDunderProto }, function (st) { - /* globals Uint8Array, Int8Array */ var a = new Uint8Array(10); var b = tag(new Int8Array(10), 'Uint8Array'); b.__proto__ = Uint8Array.prototype; // eslint-disable-line no-proto @@ -1218,6 +1217,7 @@ test('TypedArrays', { skip: !hasTypedArrays }, function (t) { false ); + // node < 0.11 has a nonconfigurable own byteLength property t.test('lies about byteLength', { skip: !('byteLength' in ArrayBuffer.prototype) }, function (s2t) { var empty4 = new ArrayBuffer(4); var empty6 = new ArrayBuffer(6);