Skip to content

Commit

Permalink
[Fix] TAs can take a DataView in node 0.8; use a simpler check
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Feb 3, 2024
1 parent 0852be6 commit 69a03f6
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 12 deletions.
4 changes: 4 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@

"extends": "@ljharb",

"globals": {
"DataView": false,
},

"rules": {
"new-cap": ["error", {
"capIsNewExceptions": [
Expand Down
13 changes: 4 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
var callBind = require('call-bind');
var callBound = require('call-bind/callBound');
var GetIntrinsic = require('get-intrinsic');
var isTypedArray = require('is-typed-array');

var $ArrayBuffer = GetIntrinsic('ArrayBuffer', true);
var $Float32Array = GetIntrinsic('Float32Array', true);
var $byteLength = callBound('ArrayBuffer.prototype.byteLength', true);
var $toString = callBound('Object.prototype.toString');

// in node 0.10, ArrayBuffers have no prototype methods, but have an own slot-checking `slice` method
var abSlice = $ArrayBuffer && !$byteLength && new $ArrayBuffer().slice;
Expand All @@ -29,14 +28,10 @@ module.exports = $byteLength || $abSlice
return false;
}
}
: $Float32Array
// in node 0.8, ArrayBuffers have no prototype or own methods
: $ArrayBuffer
// in node 0.8, ArrayBuffers have no prototype or own methods, but also no Symbol.toStringTag
? function IsArrayBuffer(obj) {
try {
return (new $Float32Array(obj)).buffer === obj && !isTypedArray(obj);
} catch (e) {
return typeof obj === 'object' && e.name === 'RangeError';
}
return $toString(obj) === '[object ArrayBuffer]';
}
: function isArrayBuffer(obj) { // eslint-disable-line no-unused-vars
return false;
Expand Down
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,8 @@
"startingVersion": "2.0.1"
},
"dependencies": {
"call-bind": "^1.0.5",
"get-intrinsic": "^1.2.2",
"is-typed-array": "^1.1.13"
"call-bind": "^1.0.2",
"get-intrinsic": "^1.2.1"
},
"publishConfig": {
"ignore": [
Expand Down
3 changes: 3 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ test('isArrayBuffer', function (t) {
var ab42 = new ArrayBuffer(42);
st.equal(isArrayBuffer(ab42), true, inspect(ab42) + ' is an ArrayBuffer');

var dv = new DataView(ab42);
st.equal(isArrayBuffer(dv), false, inspect(dv) + ' is not an ArrayBuffer');

st.end();
});

Expand Down

0 comments on commit 69a03f6

Please sign in to comment.