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