index.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. 'use strict';
  2. var $WeakMap = typeof WeakMap === 'function' && WeakMap.prototype ? WeakMap : null;
  3. var $WeakSet = typeof WeakSet === 'function' && WeakSet.prototype ? WeakSet : null;
  4. var exported;
  5. if (!$WeakMap) {
  6. /** @type {import('.')} */
  7. // eslint-disable-next-line no-unused-vars
  8. exported = function isWeakMap(x) {
  9. // `WeakMap` is not present in this environment.
  10. return false;
  11. };
  12. }
  13. var $mapHas = $WeakMap ? $WeakMap.prototype.has : null;
  14. var $setHas = $WeakSet ? $WeakSet.prototype.has : null;
  15. if (!exported && !$mapHas) {
  16. /** @type {import('.')} */
  17. // eslint-disable-next-line no-unused-vars
  18. exported = function isWeakMap(x) {
  19. // `WeakMap` does not have a `has` method
  20. return false;
  21. };
  22. }
  23. /** @type {import('.')} */
  24. module.exports = exported || function isWeakMap(x) {
  25. if (!x || typeof x !== 'object') {
  26. return false;
  27. }
  28. try {
  29. $mapHas.call(x, $mapHas);
  30. if ($setHas) {
  31. try {
  32. $setHas.call(x, $setHas);
  33. } catch (e) {
  34. return true;
  35. }
  36. }
  37. // @ts-expect-error TS can't figure out that $WeakMap is always truthy here
  38. return x instanceof $WeakMap; // core-js workaround, pre-v3
  39. } catch (e) {}
  40. return false;
  41. };