index.js 599 B

123456789101112131415161718192021222324
  1. 'use strict';
  2. var callBound = require('call-bound');
  3. // eslint-disable-next-line no-extra-parens
  4. var $deref = /** @type {<T extends WeakKey>(thisArg: WeakRef<T>) => T | undefined} */ (callBound('WeakRef.prototype.deref', true));
  5. /** @type {import('.')} */
  6. module.exports = typeof WeakRef === 'undefined'
  7. ? function isWeakRef(_value) { // eslint-disable-line no-unused-vars
  8. return false;
  9. }
  10. : function isWeakRef(value) {
  11. if (!value || typeof value !== 'object') {
  12. return false;
  13. }
  14. try {
  15. // @ts-expect-error
  16. $deref(value);
  17. return true;
  18. } catch (e) {
  19. return false;
  20. }
  21. };