UNPKG

2.6 kBJavaScriptView Raw
1var SetCache = require('./_SetCache'),
2 arraySome = require('./_arraySome'),
3 cacheHas = require('./_cacheHas');
4
5/** Used to compose bitmasks for value comparisons. */
6var COMPARE_PARTIAL_FLAG = 1,
7 COMPARE_UNORDERED_FLAG = 2;
8
9/**
10 * A specialized version of `baseIsEqualDeep` for arrays with support for
11 * partial deep comparisons.
12 *
13 * @private
14 * @param {Array} array The array to compare.
15 * @param {Array} other The other array to compare.
16 * @param {number} bitmask The bitmask flags. See `baseIsEqual` for more details.
17 * @param {Function} customizer The function to customize comparisons.
18 * @param {Function} equalFunc The function to determine equivalents of values.
19 * @param {Object} stack Tracks traversed `array` and `other` objects.
20 * @returns {boolean} Returns `true` if the arrays are equivalent, else `false`.
21 */
22function equalArrays(array, other, bitmask, customizer, equalFunc, stack) {
23 var isPartial = bitmask & COMPARE_PARTIAL_FLAG,
24 arrLength = array.length,
25 othLength = other.length;
26
27 if (arrLength != othLength && !(isPartial && othLength > arrLength)) {
28 return false;
29 }
30 // Assume cyclic values are equal.
31 var stacked = stack.get(array);
32 if (stacked && stack.get(other)) {
33 return stacked == other;
34 }
35 var index = -1,
36 result = true,
37 seen = (bitmask & COMPARE_UNORDERED_FLAG) ? new SetCache : undefined;
38
39 stack.set(array, other);
40 stack.set(other, array);
41
42 // Ignore non-index properties.
43 while (++index < arrLength) {
44 var arrValue = array[index],
45 othValue = other[index];
46
47 if (customizer) {
48 var compared = isPartial
49 ? customizer(othValue, arrValue, index, other, array, stack)
50 : customizer(arrValue, othValue, index, array, other, stack);
51 }
52 if (compared !== undefined) {
53 if (compared) {
54 continue;
55 }
56 result = false;
57 break;
58 }
59 // Recursively compare arrays (susceptible to call stack limits).
60 if (seen) {
61 if (!arraySome(other, function(othValue, othIndex) {
62 if (!cacheHas(seen, othIndex) &&
63 (arrValue === othValue || equalFunc(arrValue, othValue, bitmask, customizer, stack))) {
64 return seen.push(othIndex);
65 }
66 })) {
67 result = false;
68 break;
69 }
70 } else if (!(
71 arrValue === othValue ||
72 equalFunc(arrValue, othValue, bitmask, customizer, stack)
73 )) {
74 result = false;
75 break;
76 }
77 }
78 stack['delete'](array);
79 stack['delete'](other);
80 return result;
81}
82
83module.exports = equalArrays;