zhangnaisong
2023-08-05 24d66c8d82b628a06e93dbb1abfea2049b3d45ab
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
'use strict';
 
const Types = require('./types');
 
 
const internals = {
    mismatched: null
};
 
 
module.exports = function (obj, ref, options) {
 
    options = Object.assign({ prototype: true }, options);
 
    return !!internals.isDeepEqual(obj, ref, options, []);
};
 
 
internals.isDeepEqual = function (obj, ref, options, seen) {
 
    if (obj === ref) {                                                      // Copied from Deep-eql, copyright(c) 2013 Jake Luer, jake@alogicalparadox.com, MIT Licensed, https://github.com/chaijs/deep-eql
        return obj !== 0 || 1 / obj === 1 / ref;
    }
 
    const type = typeof obj;
 
    if (type !== typeof ref) {
        return false;
    }
 
    if (obj === null ||
        ref === null) {
 
        return false;
    }
 
    if (type === 'function') {
        if (!options.deepFunction ||
            obj.toString() !== ref.toString()) {
 
            return false;
        }
 
        // Continue as object
    }
    else if (type !== 'object') {
        return obj !== obj && ref !== ref;                                  // NaN
    }
 
    const instanceType = internals.getSharedType(obj, ref, !!options.prototype);
    switch (instanceType) {
        case Types.buffer:
            return Buffer && Buffer.prototype.equals.call(obj, ref);        // $lab:coverage:ignore$
        case Types.promise:
            return obj === ref;
        case Types.regex:
            return obj.toString() === ref.toString();
        case internals.mismatched:
            return false;
    }
 
    for (let i = seen.length - 1; i >= 0; --i) {
        if (seen[i].isSame(obj, ref)) {
            return true;                                                    // If previous comparison failed, it would have stopped execution
        }
    }
 
    seen.push(new internals.SeenEntry(obj, ref));
 
    try {
        return !!internals.isDeepEqualObj(instanceType, obj, ref, options, seen);
    }
    finally {
        seen.pop();
    }
};
 
 
internals.getSharedType = function (obj, ref, checkPrototype) {
 
    if (checkPrototype) {
        if (Object.getPrototypeOf(obj) !== Object.getPrototypeOf(ref)) {
            return internals.mismatched;
        }
 
        return Types.getInternalProto(obj);
    }
 
    const type = Types.getInternalProto(obj);
    if (type !== Types.getInternalProto(ref)) {
        return internals.mismatched;
    }
 
    return type;
};
 
 
internals.valueOf = function (obj) {
 
    const objValueOf = obj.valueOf;
    if (objValueOf === undefined) {
        return obj;
    }
 
    try {
        return objValueOf.call(obj);
    }
    catch (err) {
        return err;
    }
};
 
 
internals.hasOwnEnumerableProperty = function (obj, key) {
 
    return Object.prototype.propertyIsEnumerable.call(obj, key);
};
 
 
internals.isSetSimpleEqual = function (obj, ref) {
 
    for (const entry of obj) {
        if (!ref.has(entry)) {
            return false;
        }
    }
 
    return true;
};
 
 
internals.isDeepEqualObj = function (instanceType, obj, ref, options, seen) {
 
    const { isDeepEqual, valueOf, hasOwnEnumerableProperty } = internals;
    const { keys, getOwnPropertySymbols } = Object;
 
    if (instanceType === Types.array) {
        if (options.part) {
 
            // Check if any index match any other index
 
            for (const objValue of obj) {
                for (const refValue of ref) {
                    if (isDeepEqual(objValue, refValue, options, seen)) {
                        return true;
                    }
                }
            }
        }
        else {
            if (obj.length !== ref.length) {
                return false;
            }
 
            for (let i = 0; i < obj.length; ++i) {
                if (!isDeepEqual(obj[i], ref[i], options, seen)) {
                    return false;
                }
            }
 
            return true;
        }
    }
    else if (instanceType === Types.set) {
        if (obj.size !== ref.size) {
            return false;
        }
 
        if (!internals.isSetSimpleEqual(obj, ref)) {
 
            // Check for deep equality
 
            const ref2 = new Set(ref);
            for (const objEntry of obj) {
                if (ref2.delete(objEntry)) {
                    continue;
                }
 
                let found = false;
                for (const refEntry of ref2) {
                    if (isDeepEqual(objEntry, refEntry, options, seen)) {
                        ref2.delete(refEntry);
                        found = true;
                        break;
                    }
                }
 
                if (!found) {
                    return false;
                }
            }
        }
    }
    else if (instanceType === Types.map) {
        if (obj.size !== ref.size) {
            return false;
        }
 
        for (const [key, value] of obj) {
            if (value === undefined && !ref.has(key)) {
                return false;
            }
 
            if (!isDeepEqual(value, ref.get(key), options, seen)) {
                return false;
            }
        }
    }
    else if (instanceType === Types.error) {
 
        // Always check name and message
 
        if (obj.name !== ref.name ||
            obj.message !== ref.message) {
 
            return false;
        }
    }
 
    // Check .valueOf()
 
    const valueOfObj = valueOf(obj);
    const valueOfRef = valueOf(ref);
    if ((obj !== valueOfObj || ref !== valueOfRef) &&
        !isDeepEqual(valueOfObj, valueOfRef, options, seen)) {
 
        return false;
    }
 
    // Check properties
 
    const objKeys = keys(obj);
    if (!options.part &&
        objKeys.length !== keys(ref).length &&
        !options.skip) {
 
        return false;
    }
 
    let skipped = 0;
    for (const key of objKeys) {
        if (options.skip &&
            options.skip.includes(key)) {
 
            if (ref[key] === undefined) {
                ++skipped;
            }
 
            continue;
        }
 
        if (!hasOwnEnumerableProperty(ref, key)) {
            return false;
        }
 
        if (!isDeepEqual(obj[key], ref[key], options, seen)) {
            return false;
        }
    }
 
    if (!options.part &&
        objKeys.length - skipped !== keys(ref).length) {
 
        return false;
    }
 
    // Check symbols
 
    if (options.symbols !== false) {                                // Defaults to true
        const objSymbols = getOwnPropertySymbols(obj);
        const refSymbols = new Set(getOwnPropertySymbols(ref));
 
        for (const key of objSymbols) {
            if (!options.skip ||
                !options.skip.includes(key)) {
 
                if (hasOwnEnumerableProperty(obj, key)) {
                    if (!hasOwnEnumerableProperty(ref, key)) {
                        return false;
                    }
 
                    if (!isDeepEqual(obj[key], ref[key], options, seen)) {
                        return false;
                    }
                }
                else if (hasOwnEnumerableProperty(ref, key)) {
                    return false;
                }
            }
 
            refSymbols.delete(key);
        }
 
        for (const key of refSymbols) {
            if (hasOwnEnumerableProperty(ref, key)) {
                return false;
            }
        }
    }
 
    return true;
};
 
 
internals.SeenEntry = class {
 
    constructor(obj, ref) {
 
        this.obj = obj;
        this.ref = ref;
    }
 
    isSame(obj, ref) {
 
        return this.obj === obj && this.ref === ref;
    }
};