index.js 1.0 KB

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