index.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. 'use strict';
  2. var GetIntrinsic = require('get-intrinsic');
  3. var callBound = require('call-bound');
  4. var $WeakSet = GetIntrinsic('%WeakSet%', true);
  5. /** @type {undefined | (<V>(thisArg: Set<V>, value: V) => boolean)} */
  6. var $setHas = callBound('WeakSet.prototype.has', true);
  7. if ($setHas) {
  8. /** @type {undefined | (<K extends object, V>(thisArg: WeakMap<K, V>, key: K) => boolean)} */
  9. var $mapHas = callBound('WeakMap.prototype.has', true);
  10. /** @type {import('.')} */
  11. module.exports = function isWeakSet(x) {
  12. if (!x || typeof x !== 'object') {
  13. return false;
  14. }
  15. try {
  16. // @ts-expect-error TS can't figure out that $setHas is always truthy here
  17. $setHas(x, $setHas);
  18. if ($mapHas) {
  19. try {
  20. // @ts-expect-error this indeed might not be a weak collection
  21. $mapHas(x, $mapHas);
  22. } catch (e) {
  23. return true;
  24. }
  25. }
  26. // @ts-expect-error TS can't figure out that $WeakSet is always truthy here
  27. return x instanceof $WeakSet; // core-js workaround, pre-v3
  28. } catch (e) {}
  29. return false;
  30. };
  31. } else {
  32. /** @type {import('.')} */
  33. // @ts-expect-error
  34. module.exports = function isWeakSet(x) { // eslint-disable-line no-unused-vars
  35. // `WeakSet` does not exist, or does not have a `has` method
  36. return false;
  37. };
  38. }