equalArrays.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. var arraySome = require('./arraySome');
  2. /**
  3. * A specialized version of `baseIsEqualDeep` for arrays with support for
  4. * partial deep comparisons.
  5. *
  6. * @private
  7. * @param {Array} array The array to compare.
  8. * @param {Array} other The other array to compare.
  9. * @param {Function} equalFunc The function to determine equivalents of values.
  10. * @param {Function} [customizer] The function to customize comparing arrays.
  11. * @param {boolean} [isLoose] Specify performing partial comparisons.
  12. * @param {Array} [stackA] Tracks traversed `value` objects.
  13. * @param {Array} [stackB] Tracks traversed `other` objects.
  14. * @returns {boolean} Returns `true` if the arrays are equivalent, else `false`.
  15. */
  16. function equalArrays(array, other, equalFunc, customizer, isLoose, stackA, stackB) {
  17. var index = -1,
  18. arrLength = array.length,
  19. othLength = other.length;
  20. if (arrLength != othLength && !(isLoose && othLength > arrLength)) {
  21. return false;
  22. }
  23. // Ignore non-index properties.
  24. while (++index < arrLength) {
  25. var arrValue = array[index],
  26. othValue = other[index],
  27. result = customizer ? customizer(isLoose ? othValue : arrValue, isLoose ? arrValue : othValue, index) : undefined;
  28. if (result !== undefined) {
  29. if (result) {
  30. continue;
  31. }
  32. return false;
  33. }
  34. // Recursively compare arrays (susceptible to call stack limits).
  35. if (isLoose) {
  36. if (!arraySome(other, function(othValue) {
  37. return arrValue === othValue || equalFunc(arrValue, othValue, customizer, isLoose, stackA, stackB);
  38. })) {
  39. return false;
  40. }
  41. } else if (!(arrValue === othValue || equalFunc(arrValue, othValue, customizer, isLoose, stackA, stackB))) {
  42. return false;
  43. }
  44. }
  45. return true;
  46. }
  47. module.exports = equalArrays;