_equalArrays.js 2.4 KB

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