index.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. 'use strict';
  2. var callBind = require('call-bind');
  3. var callBound = require('call-bind/callBound');
  4. var GetIntrinsic = require('get-intrinsic');
  5. var isTypedArray = require('is-typed-array');
  6. var $ArrayBuffer = GetIntrinsic('ArrayBuffer', true);
  7. var $Float32Array = GetIntrinsic('Float32Array', true);
  8. var $byteLength = callBound('ArrayBuffer.prototype.byteLength', true);
  9. // in node 0.10, ArrayBuffers have no prototype methods, but have an own slot-checking `slice` method
  10. var abSlice = $ArrayBuffer && !$byteLength && new $ArrayBuffer().slice;
  11. var $abSlice = abSlice && callBind(abSlice);
  12. module.exports = $byteLength || $abSlice
  13. ? function isArrayBuffer(obj) {
  14. if (!obj || typeof obj !== 'object') {
  15. return false;
  16. }
  17. try {
  18. if ($byteLength) {
  19. $byteLength(obj);
  20. } else {
  21. $abSlice(obj, 0);
  22. }
  23. return true;
  24. } catch (e) {
  25. return false;
  26. }
  27. }
  28. : $Float32Array
  29. // in node 0.8, ArrayBuffers have no prototype or own methods
  30. ? function IsArrayBuffer(obj) {
  31. try {
  32. return (new $Float32Array(obj)).buffer === obj && !isTypedArray(obj);
  33. } catch (e) {
  34. return typeof obj === 'object' && e.name === 'RangeError';
  35. }
  36. }
  37. : function isArrayBuffer(obj) { // eslint-disable-line no-unused-vars
  38. return false;
  39. };