IsLooselyEqual.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. 'use strict';
  2. var isFinite = require('../helpers/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. // https://262.ecma-international.org/13.0/#sec-islooselyequal
  9. module.exports = function IsLooselyEqual(x, y) {
  10. var xType = Type(x);
  11. var yType = Type(y);
  12. if (xType === yType) {
  13. return IsStrictlyEqual(x, y);
  14. }
  15. if (x == null && y == null) {
  16. return true;
  17. }
  18. if (xType === 'Number' && yType === 'String') {
  19. return IsLooselyEqual(x, ToNumber(y));
  20. }
  21. if (xType === 'String' && yType === 'Number') {
  22. return IsLooselyEqual(ToNumber(x), y);
  23. }
  24. if (xType === 'BigInt' && yType === 'String') {
  25. var n = StringToBigInt(y);
  26. if (typeof n === 'undefined') {
  27. return false;
  28. }
  29. return IsLooselyEqual(x, n);
  30. }
  31. if (xType === 'String' && yType === 'BigInt') {
  32. return IsLooselyEqual(y, x);
  33. }
  34. if (xType === 'Boolean') {
  35. return IsLooselyEqual(ToNumber(x), y);
  36. }
  37. if (yType === 'Boolean') {
  38. return IsLooselyEqual(x, ToNumber(y));
  39. }
  40. if ((xType === 'String' || xType === 'Number' || xType === 'Symbol' || xType === 'BigInt') && yType === 'Object') {
  41. return IsLooselyEqual(x, ToPrimitive(y));
  42. }
  43. if (xType === 'Object' && (yType === 'String' || yType === 'Number' || yType === 'Symbol' || yType === 'BigInt')) {
  44. return IsLooselyEqual(ToPrimitive(x), y);
  45. }
  46. if ((xType === 'BigInt' && yType === 'Number') || (xType === 'Number' && yType === 'BigInt')) {
  47. if (!isFinite(x) || !isFinite(y)) {
  48. return false;
  49. }
  50. // eslint-disable-next-line eqeqeq
  51. return x == y; // shortcut for step 13.b.
  52. }
  53. return false;
  54. };