index.js 856 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. 'use strict';
  2. var GetIntrinsic = require('get-intrinsic');
  3. var $ArrayBuffer = GetIntrinsic('%ArrayBuffer%');
  4. var $DataView = GetIntrinsic('%DataView%', true);
  5. var callBound = require('call-bind/callBound');
  6. // node <= 0.10, < 0.11.4 has a nonconfigurable own property instead of a prototype getter
  7. var $dataViewBuffer = callBound('DataView.prototype.buffer', true);
  8. var isTypedArray = require('is-typed-array');
  9. /** @type {import('.')} */
  10. module.exports = function isDataView(x) {
  11. if (!x || typeof x !== 'object' || !$DataView || isTypedArray(x)) {
  12. return false;
  13. }
  14. if ($dataViewBuffer) {
  15. try {
  16. $dataViewBuffer(x);
  17. return true;
  18. } catch (e) {
  19. return false;
  20. }
  21. }
  22. if (
  23. ('getInt8' in x)
  24. && typeof x.getInt8 === 'function'
  25. && x.getInt8 === new $DataView(new $ArrayBuffer(1)).getInt8
  26. ) {
  27. return true;
  28. }
  29. return false;
  30. };