-
Notifications
You must be signed in to change notification settings - Fork 29
/
deep-equal.js
264 lines (235 loc) · 9.3 KB
/
deep-equal.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
"use strict";
var valueToString = require("@sinonjs/commons").valueToString;
var className = require("@sinonjs/commons").className;
var typeOf = require("@sinonjs/commons").typeOf;
var arrayProto = require("@sinonjs/commons").prototypes.array;
var objectProto = require("@sinonjs/commons").prototypes.object;
var mapForEach = require("@sinonjs/commons").prototypes.map.forEach;
var getClass = require("./get-class");
var identical = require("./identical");
var isArguments = require("./is-arguments");
var isDate = require("./is-date");
var isElement = require("./is-element");
var isMap = require("./is-map");
var isNaN = require("./is-nan");
var isObject = require("./is-object");
var isSet = require("./is-set");
var isSubset = require("./is-subset");
var concat = arrayProto.concat;
var every = arrayProto.every;
var push = arrayProto.push;
var getTime = Date.prototype.getTime;
var hasOwnProperty = objectProto.hasOwnProperty;
var indexOf = arrayProto.indexOf;
var keys = Object.keys;
var getOwnPropertySymbols = Object.getOwnPropertySymbols;
/**
* Deep equal comparison. Two values are "deep equal" when:
*
* - They are equal, according to samsam.identical
* - They are both date objects representing the same time
* - They are both arrays containing elements that are all deepEqual
* - They are objects with the same set of properties, and each property
* in ``actual`` is deepEqual to the corresponding property in ``expectation``
*
* Supports cyclic objects.
*
* @alias module:samsam.deepEqual
* @param {*} actual The object to examine
* @param {*} expectation The object actual is expected to be equal to
* @param {object} match A value to match on
* @returns {boolean} Returns true when actual and expectation are considered equal
*/
function deepEqualCyclic(actual, expectation, match) {
// used for cyclic comparison
// contain already visited objects
var actualObjects = [];
var expectationObjects = [];
// contain pathes (position in the object structure)
// of the already visited objects
// indexes same as in objects arrays
var actualPaths = [];
var expectationPaths = [];
// contains combinations of already compared objects
// in the manner: { "$1['ref']$2['ref']": true }
var compared = {};
// does the recursion for the deep equal check
// eslint-disable-next-line complexity
return (function deepEqual(
actualObj,
expectationObj,
actualPath,
expectationPath
) {
// If both are matchers they must be the same instance in order to be
// considered equal If we didn't do that we would end up running one
// matcher against the other
if (match && match.isMatcher(expectationObj)) {
if (match.isMatcher(actualObj)) {
return actualObj === expectationObj;
}
return expectationObj.test(actualObj);
}
var actualType = typeof actualObj;
var expectationType = typeof expectationObj;
if (
actualObj === expectationObj ||
isNaN(actualObj) ||
isNaN(expectationObj) ||
actualObj === null ||
expectationObj === null ||
actualObj === undefined ||
expectationObj === undefined ||
actualType !== "object" ||
expectationType !== "object"
) {
return identical(actualObj, expectationObj);
}
// Elements are only equal if identical(expected, actual)
if (isElement(actualObj) || isElement(expectationObj)) {
return false;
}
var isActualDate = isDate(actualObj);
var isExpectationDate = isDate(expectationObj);
if (isActualDate || isExpectationDate) {
if (
!isActualDate ||
!isExpectationDate ||
getTime.call(actualObj) !== getTime.call(expectationObj)
) {
return false;
}
}
if (actualObj instanceof RegExp && expectationObj instanceof RegExp) {
if (valueToString(actualObj) !== valueToString(expectationObj)) {
return false;
}
}
if (actualObj instanceof Promise && expectationObj instanceof Promise) {
return actualObj === expectationObj;
}
if (actualObj instanceof Error && expectationObj instanceof Error) {
return actualObj === expectationObj;
}
var actualClass = getClass(actualObj);
var expectationClass = getClass(expectationObj);
var actualKeys = keys(actualObj);
var expectationKeys = keys(expectationObj);
var actualName = className(actualObj);
var expectationName = className(expectationObj);
var expectationSymbols =
typeOf(getOwnPropertySymbols) === "function"
? getOwnPropertySymbols(expectationObj)
: /* istanbul ignore next: cannot collect coverage for engine that doesn't support Symbol */
[];
var expectationKeysAndSymbols = concat(
expectationKeys,
expectationSymbols
);
if (isArguments(actualObj) || isArguments(expectationObj)) {
if (actualObj.length !== expectationObj.length) {
return false;
}
} else {
if (
actualType !== expectationType ||
actualClass !== expectationClass ||
actualKeys.length !== expectationKeys.length ||
(actualName &&
expectationName &&
actualName !== expectationName)
) {
return false;
}
}
if (isSet(actualObj) || isSet(expectationObj)) {
if (
!isSet(actualObj) ||
!isSet(expectationObj) ||
actualObj.size !== expectationObj.size
) {
return false;
}
return isSubset(actualObj, expectationObj, deepEqual);
}
if (isMap(actualObj) || isMap(expectationObj)) {
if (
!isMap(actualObj) ||
!isMap(expectationObj) ||
actualObj.size !== expectationObj.size
) {
return false;
}
var mapsDeeplyEqual = true;
mapForEach(actualObj, function (value, key) {
mapsDeeplyEqual =
mapsDeeplyEqual &&
deepEqualCyclic(value, expectationObj.get(key));
});
return mapsDeeplyEqual;
}
return every(expectationKeysAndSymbols, function (key) {
if (!hasOwnProperty(actualObj, key)) {
return false;
}
var actualValue = actualObj[key];
var expectationValue = expectationObj[key];
var actualObject = isObject(actualValue);
var expectationObject = isObject(expectationValue);
// determines, if the objects were already visited
// (it's faster to check for isObject first, than to
// get -1 from getIndex for non objects)
var actualIndex = actualObject
? indexOf(actualObjects, actualValue)
: -1;
var expectationIndex = expectationObject
? indexOf(expectationObjects, expectationValue)
: -1;
// determines the new paths of the objects
// - for non cyclic objects the current path will be extended
// by current property name
// - for cyclic objects the stored path is taken
var newActualPath =
actualIndex !== -1
? actualPaths[actualIndex]
: actualPath + "[" + JSON.stringify(key) + "]";
var newExpectationPath =
expectationIndex !== -1
? expectationPaths[expectationIndex]
: expectationPath + "[" + JSON.stringify(key) + "]";
var combinedPath = newActualPath + newExpectationPath;
// stop recursion if current objects are already compared
if (compared[combinedPath]) {
return true;
}
// remember the current objects and their paths
if (actualIndex === -1 && actualObject) {
push(actualObjects, actualValue);
push(actualPaths, newActualPath);
}
if (expectationIndex === -1 && expectationObject) {
push(expectationObjects, expectationValue);
push(expectationPaths, newExpectationPath);
}
// remember that the current objects are already compared
if (actualObject && expectationObject) {
compared[combinedPath] = true;
}
// End of cyclic logic
// neither actualValue nor expectationValue is a cycle
// continue with next level
return deepEqual(
actualValue,
expectationValue,
newActualPath,
newExpectationPath
);
});
})(actual, expectation, "$1", "$2");
}
deepEqualCyclic.use = function (match) {
return function deepEqual(a, b) {
return deepEqualCyclic(a, b, match);
};
};
module.exports = deepEqualCyclic;