Skip to content

Commit 0360ee1

Browse files
committed
[Refactor] use is-array-buffer package
1 parent df3e3c4 commit 0360ee1

File tree

4 files changed

+10
-15
lines changed

4 files changed

+10
-15
lines changed

.eslintrc

+5
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@
1515
"no-magic-numbers": [2, { "ignore": [0, 1] }],
1616
},
1717

18+
"globals": {
19+
"Int8Array": false,
20+
"Uint8Array": false,
21+
},
22+
1823
"overrides": [
1924
{
2025
"files": "example/**",

index.js

+3-14
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,10 @@ var getIterator = require('es-get-iterator');
1515
var getSideChannel = require('side-channel');
1616
var whichTypedArray = require('which-typed-array');
1717
var assign = require('object.assign');
18+
var isArrayBuffer = require('is-array-buffer');
1819

19-
// TODO: use extracted package
20-
var byteLength = callBound('ArrayBuffer.prototype.byteLength', true);
21-
function isArrayBuffer(buffer) {
22-
if (!buffer || typeof buffer !== 'object' || !byteLength) {
23-
return false;
24-
}
25-
try {
26-
byteLength(buffer);
27-
return true;
28-
} catch (e) {
29-
return false;
30-
}
31-
}
20+
var byteLength = callBound('%ArrayBuffer.prototype.byteLength%', true)
21+
|| function byteLength(ab) { return ab.byteLength; }; // in node < 0.11, byteLength is an own nonconfigurable property
3222

3323
var $getTime = callBound('Date.prototype.getTime');
3424
var gPO = Object.getPrototypeOf;
@@ -342,7 +332,6 @@ function objEquiv(a, b, opts, channel) {
342332
if (aIsArrayBuffer !== bIsArrayBuffer) { return false; }
343333
if (aIsArrayBuffer || bIsArrayBuffer) { // && would work too, because both are true or both false here
344334
if (byteLength(a) !== byteLength(b)) { return false; }
345-
/* global Uint8Array */
346335
return typeof Uint8Array === 'function' && internalDeepEqual(new Uint8Array(a), new Uint8Array(b), opts, channel);
347336
}
348337

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"es-get-iterator": "^1.1.2",
3636
"get-intrinsic": "^1.1.3",
3737
"is-arguments": "^1.1.1",
38+
"is-array-buffer": "^3.0.1",
3839
"is-date-object": "^1.0.5",
3940
"is-regex": "^1.1.4",
4041
"isarray": "^2.0.5",

test/cmp.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1159,7 +1159,6 @@ test('TypedArrays', { skip: !hasTypedArrays }, function (t) {
11591159
});
11601160

11611161
t.test('one TypedArray faking as another', { skip: !hasDunderProto }, function (st) {
1162-
/* globals Uint8Array, Int8Array */
11631162
var a = new Uint8Array(10);
11641163
var b = tag(new Int8Array(10), 'Uint8Array');
11651164
b.__proto__ = Uint8Array.prototype; // eslint-disable-line no-proto
@@ -1218,6 +1217,7 @@ test('TypedArrays', { skip: !hasTypedArrays }, function (t) {
12181217
false
12191218
);
12201219

1220+
// node < 0.11 has a nonconfigurable own byteLength property
12211221
t.test('lies about byteLength', { skip: !('byteLength' in ArrayBuffer.prototype) }, function (s2t) {
12221222
var empty4 = new ArrayBuffer(4);
12231223
var empty6 = new ArrayBuffer(6);

0 commit comments

Comments
 (0)