UNPKG

2.98 kBJavaScriptView Raw
1import Stack from './_Stack';
2import equalArrays from './_equalArrays';
3import equalByTag from './_equalByTag';
4import equalObjects from './_equalObjects';
5import getTag from './_getTag';
6import isArray from './isArray';
7import isHostObject from './_isHostObject';
8import isTypedArray from './isTypedArray';
9
10/** Used to compose bitmasks for comparison styles. */
11var PARTIAL_COMPARE_FLAG = 2;
12
13/** `Object#toString` result references. */
14var argsTag = '[object Arguments]',
15 arrayTag = '[object Array]',
16 objectTag = '[object Object]';
17
18/** Used for built-in method references. */
19var objectProto = Object.prototype;
20
21/** Used to check objects for own properties. */
22var hasOwnProperty = objectProto.hasOwnProperty;
23
24/**
25 * A specialized version of `baseIsEqual` for arrays and objects which performs
26 * deep comparisons and tracks traversed objects enabling objects with circular
27 * references to be compared.
28 *
29 * @private
30 * @param {Object} object The object to compare.
31 * @param {Object} other The other object to compare.
32 * @param {Function} equalFunc The function to determine equivalents of values.
33 * @param {Function} [customizer] The function to customize comparisons.
34 * @param {number} [bitmask] The bitmask of comparison flags. See `baseIsEqual`
35 * for more details.
36 * @param {Object} [stack] Tracks traversed `object` and `other` objects.
37 * @returns {boolean} Returns `true` if the objects are equivalent, else `false`.
38 */
39function baseIsEqualDeep(object, other, equalFunc, customizer, bitmask, stack) {
40 var objIsArr = isArray(object),
41 othIsArr = isArray(other),
42 objTag = arrayTag,
43 othTag = arrayTag;
44
45 if (!objIsArr) {
46 objTag = getTag(object);
47 objTag = objTag == argsTag ? objectTag : objTag;
48 }
49 if (!othIsArr) {
50 othTag = getTag(other);
51 othTag = othTag == argsTag ? objectTag : othTag;
52 }
53 var objIsObj = objTag == objectTag && !isHostObject(object),
54 othIsObj = othTag == objectTag && !isHostObject(other),
55 isSameTag = objTag == othTag;
56
57 if (isSameTag && !objIsObj) {
58 stack || (stack = new Stack);
59 return (objIsArr || isTypedArray(object))
60 ? equalArrays(object, other, equalFunc, customizer, bitmask, stack)
61 : equalByTag(object, other, objTag, equalFunc, customizer, bitmask, stack);
62 }
63 if (!(bitmask & PARTIAL_COMPARE_FLAG)) {
64 var objIsWrapped = objIsObj && hasOwnProperty.call(object, '__wrapped__'),
65 othIsWrapped = othIsObj && hasOwnProperty.call(other, '__wrapped__');
66
67 if (objIsWrapped || othIsWrapped) {
68 var objUnwrapped = objIsWrapped ? object.value() : object,
69 othUnwrapped = othIsWrapped ? other.value() : other;
70
71 stack || (stack = new Stack);
72 return equalFunc(objUnwrapped, othUnwrapped, customizer, bitmask, stack);
73 }
74 }
75 if (!isSameTag) {
76 return false;
77 }
78 stack || (stack = new Stack);
79 return equalObjects(object, other, equalFunc, customizer, bitmask, stack);
80}
81
82export default baseIsEqualDeep;