IsLooselyEqual.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. 'use strict';
  2. var isFinite = require('math-intrinsics/isFinite');
  3. var IsStrictlyEqual = require('./IsStrictlyEqual');
  4. var StringToBigInt = require('./StringToBigInt');
  5. var ToNumber = require('./ToNumber');
  6. var ToPrimitive = require('./ToPrimitive');
  7. var Type = require('./Type');
  8. var isObject = require('../helpers/isObject');
  9. // https://262.ecma-international.org/13.0/#sec-islooselyequal
  10. module.exports = function IsLooselyEqual(x, y) {
  11. if (Type(x) === Type(y)) {
  12. return IsStrictlyEqual(x, y);
  13. }
  14. if (x == null && y == null) {
  15. return true;
  16. }
  17. if (typeof x === 'number' && typeof y === 'string') {
  18. return IsLooselyEqual(x, ToNumber(y));
  19. }
  20. if (typeof x === 'string' && typeof y === 'number') {
  21. return IsLooselyEqual(ToNumber(x), y);
  22. }
  23. if (typeof x === 'bigint' && typeof y === 'string') {
  24. var n = StringToBigInt(y);
  25. if (typeof n === 'undefined') {
  26. return false;
  27. }
  28. return IsLooselyEqual(x, n);
  29. }
  30. if (typeof x === 'string' && typeof y === 'bigint') {
  31. return IsLooselyEqual(y, x);
  32. }
  33. if (typeof x === 'boolean') {
  34. return IsLooselyEqual(ToNumber(x), y);
  35. }
  36. if (typeof y === 'boolean') {
  37. return IsLooselyEqual(x, ToNumber(y));
  38. }
  39. if ((typeof x === 'string' || typeof x === 'number' || typeof x === 'symbol' || typeof x === 'bigint') && isObject(y)) {
  40. return IsLooselyEqual(x, ToPrimitive(y));
  41. }
  42. if (isObject(x) && (typeof y === 'string' || typeof y === 'number' || typeof y === 'symbol' || typeof y === 'bigint')) {
  43. return IsLooselyEqual(ToPrimitive(x), y);
  44. }
  45. if ((typeof x === 'bigint' && typeof y === 'number') || (typeof x === 'number' && typeof y === 'bigint')) {
  46. if (!isFinite(x) || !isFinite(y)) {
  47. return false;
  48. }
  49. // eslint-disable-next-line eqeqeq
  50. return x == y; // shortcut for step 13.b.
  51. }
  52. return false;
  53. };