UNPKG

3.83 kBJavaScriptView Raw
1import Symbol from './_Symbol';
2import Uint8Array from './_Uint8Array';
3import equalArrays from './_equalArrays';
4import mapToArray from './_mapToArray';
5import setToArray from './_setToArray';
6
7/** Used to compose bitmasks for comparison styles. */
8var UNORDERED_COMPARE_FLAG = 1,
9 PARTIAL_COMPARE_FLAG = 2;
10
11/** `Object#toString` result references. */
12var boolTag = '[object Boolean]',
13 dateTag = '[object Date]',
14 errorTag = '[object Error]',
15 mapTag = '[object Map]',
16 numberTag = '[object Number]',
17 regexpTag = '[object RegExp]',
18 setTag = '[object Set]',
19 stringTag = '[object String]',
20 symbolTag = '[object Symbol]';
21
22var arrayBufferTag = '[object ArrayBuffer]',
23 dataViewTag = '[object DataView]';
24
25/** Used to convert symbols to primitives and strings. */
26var symbolProto = Symbol ? Symbol.prototype : undefined,
27 symbolValueOf = symbolProto ? symbolProto.valueOf : undefined;
28
29/**
30 * A specialized version of `baseIsEqualDeep` for comparing objects of
31 * the same `toStringTag`.
32 *
33 * **Note:** This function only supports comparing values with tags of
34 * `Boolean`, `Date`, `Error`, `Number`, `RegExp`, or `String`.
35 *
36 * @private
37 * @param {Object} object The object to compare.
38 * @param {Object} other The other object to compare.
39 * @param {string} tag The `toStringTag` of the objects to compare.
40 * @param {Function} equalFunc The function to determine equivalents of values.
41 * @param {Function} customizer The function to customize comparisons.
42 * @param {number} bitmask The bitmask of comparison flags. See `baseIsEqual`
43 * for more details.
44 * @param {Object} stack Tracks traversed `object` and `other` objects.
45 * @returns {boolean} Returns `true` if the objects are equivalent, else `false`.
46 */
47function equalByTag(object, other, tag, equalFunc, customizer, bitmask, stack) {
48 switch (tag) {
49 case dataViewTag:
50 if ((object.byteLength != other.byteLength) ||
51 (object.byteOffset != other.byteOffset)) {
52 return false;
53 }
54 object = object.buffer;
55 other = other.buffer;
56
57 case arrayBufferTag:
58 if ((object.byteLength != other.byteLength) ||
59 !equalFunc(new Uint8Array(object), new Uint8Array(other))) {
60 return false;
61 }
62 return true;
63
64 case boolTag:
65 case dateTag:
66 // Coerce dates and booleans to numbers, dates to milliseconds and
67 // booleans to `1` or `0` treating invalid dates coerced to `NaN` as
68 // not equal.
69 return +object == +other;
70
71 case errorTag:
72 return object.name == other.name && object.message == other.message;
73
74 case numberTag:
75 // Treat `NaN` vs. `NaN` as equal.
76 return (object != +object) ? other != +other : object == +other;
77
78 case regexpTag:
79 case stringTag:
80 // Coerce regexes to strings and treat strings, primitives and objects,
81 // as equal. See http://www.ecma-international.org/ecma-262/6.0/#sec-regexp.prototype.tostring
82 // for more details.
83 return object == (other + '');
84
85 case mapTag:
86 var convert = mapToArray;
87
88 case setTag:
89 var isPartial = bitmask & PARTIAL_COMPARE_FLAG;
90 convert || (convert = setToArray);
91
92 if (object.size != other.size && !isPartial) {
93 return false;
94 }
95 // Assume cyclic values are equal.
96 var stacked = stack.get(object);
97 if (stacked) {
98 return stacked == other;
99 }
100 bitmask |= UNORDERED_COMPARE_FLAG;
101 stack.set(object, other);
102
103 // Recursively compare objects (susceptible to call stack limits).
104 return equalArrays(convert(object), convert(other), equalFunc, customizer, bitmask, stack);
105
106 case symbolTag:
107 if (symbolValueOf) {
108 return symbolValueOf.call(object) == symbolValueOf.call(other);
109 }
110 }
111 return false;
112}
113
114export default equalByTag;