1 | import _arrayFromIterator from "./_arrayFromIterator.js";
|
---|
2 | import _includesWith from "./_includesWith.js";
|
---|
3 | import _functionName from "./_functionName.js";
|
---|
4 | import _has from "./_has.js";
|
---|
5 | import _objectIs from "./_objectIs.js";
|
---|
6 | import keys from "../keys.js";
|
---|
7 | import type from "../type.js";
|
---|
8 | /**
|
---|
9 | * private _uniqContentEquals function.
|
---|
10 | * That function is checking equality of 2 iterator contents with 2 assumptions
|
---|
11 | * - iterators lengths are the same
|
---|
12 | * - iterators values are unique
|
---|
13 | *
|
---|
14 | * false-positive result will be returned for comparison of, e.g.
|
---|
15 | * - [1,2,3] and [1,2,3,4]
|
---|
16 | * - [1,1,1] and [1,2,3]
|
---|
17 | * */
|
---|
18 |
|
---|
19 | function _uniqContentEquals(aIterator, bIterator, stackA, stackB) {
|
---|
20 | var a = _arrayFromIterator(aIterator);
|
---|
21 |
|
---|
22 | var b = _arrayFromIterator(bIterator);
|
---|
23 |
|
---|
24 | function eq(_a, _b) {
|
---|
25 | return _equals(_a, _b, stackA.slice(), stackB.slice());
|
---|
26 | } // if *a* array contains any element that is not included in *b*
|
---|
27 |
|
---|
28 |
|
---|
29 | return !_includesWith(function (b, aItem) {
|
---|
30 | return !_includesWith(eq, aItem, b);
|
---|
31 | }, b, a);
|
---|
32 | }
|
---|
33 |
|
---|
34 | export default function _equals(a, b, stackA, stackB) {
|
---|
35 | if (_objectIs(a, b)) {
|
---|
36 | return true;
|
---|
37 | }
|
---|
38 |
|
---|
39 | var typeA = type(a);
|
---|
40 |
|
---|
41 | if (typeA !== type(b)) {
|
---|
42 | return false;
|
---|
43 | }
|
---|
44 |
|
---|
45 | if (typeof a['fantasy-land/equals'] === 'function' || typeof b['fantasy-land/equals'] === 'function') {
|
---|
46 | return typeof a['fantasy-land/equals'] === 'function' && a['fantasy-land/equals'](b) && typeof b['fantasy-land/equals'] === 'function' && b['fantasy-land/equals'](a);
|
---|
47 | }
|
---|
48 |
|
---|
49 | if (typeof a.equals === 'function' || typeof b.equals === 'function') {
|
---|
50 | return typeof a.equals === 'function' && a.equals(b) && typeof b.equals === 'function' && b.equals(a);
|
---|
51 | }
|
---|
52 |
|
---|
53 | switch (typeA) {
|
---|
54 | case 'Arguments':
|
---|
55 | case 'Array':
|
---|
56 | case 'Object':
|
---|
57 | if (typeof a.constructor === 'function' && _functionName(a.constructor) === 'Promise') {
|
---|
58 | return a === b;
|
---|
59 | }
|
---|
60 |
|
---|
61 | break;
|
---|
62 |
|
---|
63 | case 'Boolean':
|
---|
64 | case 'Number':
|
---|
65 | case 'String':
|
---|
66 | if (!(typeof a === typeof b && _objectIs(a.valueOf(), b.valueOf()))) {
|
---|
67 | return false;
|
---|
68 | }
|
---|
69 |
|
---|
70 | break;
|
---|
71 |
|
---|
72 | case 'Date':
|
---|
73 | if (!_objectIs(a.valueOf(), b.valueOf())) {
|
---|
74 | return false;
|
---|
75 | }
|
---|
76 |
|
---|
77 | break;
|
---|
78 |
|
---|
79 | case 'Error':
|
---|
80 | return a.name === b.name && a.message === b.message;
|
---|
81 |
|
---|
82 | case 'RegExp':
|
---|
83 | if (!(a.source === b.source && a.global === b.global && a.ignoreCase === b.ignoreCase && a.multiline === b.multiline && a.sticky === b.sticky && a.unicode === b.unicode)) {
|
---|
84 | return false;
|
---|
85 | }
|
---|
86 |
|
---|
87 | break;
|
---|
88 | }
|
---|
89 |
|
---|
90 | var idx = stackA.length - 1;
|
---|
91 |
|
---|
92 | while (idx >= 0) {
|
---|
93 | if (stackA[idx] === a) {
|
---|
94 | return stackB[idx] === b;
|
---|
95 | }
|
---|
96 |
|
---|
97 | idx -= 1;
|
---|
98 | }
|
---|
99 |
|
---|
100 | switch (typeA) {
|
---|
101 | case 'Map':
|
---|
102 | if (a.size !== b.size) {
|
---|
103 | return false;
|
---|
104 | }
|
---|
105 |
|
---|
106 | return _uniqContentEquals(a.entries(), b.entries(), stackA.concat([a]), stackB.concat([b]));
|
---|
107 |
|
---|
108 | case 'Set':
|
---|
109 | if (a.size !== b.size) {
|
---|
110 | return false;
|
---|
111 | }
|
---|
112 |
|
---|
113 | return _uniqContentEquals(a.values(), b.values(), stackA.concat([a]), stackB.concat([b]));
|
---|
114 |
|
---|
115 | case 'Arguments':
|
---|
116 | case 'Array':
|
---|
117 | case 'Object':
|
---|
118 | case 'Boolean':
|
---|
119 | case 'Number':
|
---|
120 | case 'String':
|
---|
121 | case 'Date':
|
---|
122 | case 'Error':
|
---|
123 | case 'RegExp':
|
---|
124 | case 'Int8Array':
|
---|
125 | case 'Uint8Array':
|
---|
126 | case 'Uint8ClampedArray':
|
---|
127 | case 'Int16Array':
|
---|
128 | case 'Uint16Array':
|
---|
129 | case 'Int32Array':
|
---|
130 | case 'Uint32Array':
|
---|
131 | case 'Float32Array':
|
---|
132 | case 'Float64Array':
|
---|
133 | case 'ArrayBuffer':
|
---|
134 | break;
|
---|
135 |
|
---|
136 | default:
|
---|
137 | // Values of other types are only equal if identical.
|
---|
138 | return false;
|
---|
139 | }
|
---|
140 |
|
---|
141 | var keysA = keys(a);
|
---|
142 |
|
---|
143 | if (keysA.length !== keys(b).length) {
|
---|
144 | return false;
|
---|
145 | }
|
---|
146 |
|
---|
147 | var extendedStackA = stackA.concat([a]);
|
---|
148 | var extendedStackB = stackB.concat([b]);
|
---|
149 | idx = keysA.length - 1;
|
---|
150 |
|
---|
151 | while (idx >= 0) {
|
---|
152 | var key = keysA[idx];
|
---|
153 |
|
---|
154 | if (!(_has(key, b) && _equals(b[key], a[key], extendedStackA, extendedStackB))) {
|
---|
155 | return false;
|
---|
156 | }
|
---|
157 |
|
---|
158 | idx -= 1;
|
---|
159 | }
|
---|
160 |
|
---|
161 | return true;
|
---|
162 | } |
---|