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('../helpers/isNaN');
|
---|
10 |
|
---|
11 | // https://262.ecma-international.org/11.0/#sec-abstract-equality-comparison
|
---|
12 |
|
---|
13 | module.exports = function AbstractEqualityComparison(x, y) {
|
---|
14 | var xType = Type(x);
|
---|
15 | var yType = Type(y);
|
---|
16 | if (xType === yType) {
|
---|
17 | return StrictEqualityComparison(x, y);
|
---|
18 | }
|
---|
19 | if (x == null && y == null) {
|
---|
20 | return true;
|
---|
21 | }
|
---|
22 | if (xType === 'Number' && yType === 'String') {
|
---|
23 | return AbstractEqualityComparison(x, ToNumber(y));
|
---|
24 | }
|
---|
25 | if (xType === 'String' && yType === 'Number') {
|
---|
26 | return AbstractEqualityComparison(ToNumber(x), y);
|
---|
27 | }
|
---|
28 | if (xType === 'BigInt' && yType === 'String') {
|
---|
29 | var n = StringToBigInt(y);
|
---|
30 | if (isNaN(n)) {
|
---|
31 | return false;
|
---|
32 | }
|
---|
33 | return AbstractEqualityComparison(x, n);
|
---|
34 | }
|
---|
35 | if (xType === 'String' && yType === 'BigInt') {
|
---|
36 | return AbstractEqualityComparison(y, x);
|
---|
37 | }
|
---|
38 | if (xType === 'Boolean') {
|
---|
39 | return AbstractEqualityComparison(ToNumber(x), y);
|
---|
40 | }
|
---|
41 | if (yType === 'Boolean') {
|
---|
42 | return AbstractEqualityComparison(x, ToNumber(y));
|
---|
43 | }
|
---|
44 | if ((xType === 'String' || xType === 'Number' || xType === 'BigInt' || xType === 'Symbol') && yType === 'Object') {
|
---|
45 | return AbstractEqualityComparison(x, ToPrimitive(y));
|
---|
46 | }
|
---|
47 | if (xType === 'Object' && (yType === 'String' || yType === 'Number' || yType === 'BigInt' || yType === 'Symbol')) {
|
---|
48 | return AbstractEqualityComparison(ToPrimitive(x), y);
|
---|
49 | }
|
---|
50 | if ((xType === 'BigInt' && yType === 'Number') || (xType === 'Number' && yType === 'BigInt')) {
|
---|
51 | if (isNaN(x) || isNaN(y) || x === Infinity || y === Infinity || x === -Infinity || y === -Infinity) {
|
---|
52 | return false;
|
---|
53 | }
|
---|
54 | return x == y; // eslint-disable-line eqeqeq
|
---|
55 | }
|
---|
56 | return false;
|
---|
57 | };
|
---|