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