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