equalObjects.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import keys from '../object/keys';
  2. /** Used for native method references. */
  3. var objectProto = Object.prototype;
  4. /** Used to check objects for own properties. */
  5. var hasOwnProperty = objectProto.hasOwnProperty;
  6. /**
  7. * A specialized version of `baseIsEqualDeep` for objects with support for
  8. * partial deep comparisons.
  9. *
  10. * @private
  11. * @param {Object} object The object to compare.
  12. * @param {Object} other The other object to compare.
  13. * @param {Function} equalFunc The function to determine equivalents of values.
  14. * @param {Function} [customizer] The function to customize comparing values.
  15. * @param {boolean} [isLoose] Specify performing partial comparisons.
  16. * @param {Array} [stackA] Tracks traversed `value` objects.
  17. * @param {Array} [stackB] Tracks traversed `other` objects.
  18. * @returns {boolean} Returns `true` if the objects are equivalent, else `false`.
  19. */
  20. function equalObjects(object, other, equalFunc, customizer, isLoose, stackA, stackB) {
  21. var objProps = keys(object),
  22. objLength = objProps.length,
  23. othProps = keys(other),
  24. othLength = othProps.length;
  25. if (objLength != othLength && !isLoose) {
  26. return false;
  27. }
  28. var index = objLength;
  29. while (index--) {
  30. var key = objProps[index];
  31. if (!(isLoose ? key in other : hasOwnProperty.call(other, key))) {
  32. return false;
  33. }
  34. }
  35. var skipCtor = isLoose;
  36. while (++index < objLength) {
  37. key = objProps[index];
  38. var objValue = object[key],
  39. othValue = other[key],
  40. result = customizer ? customizer(isLoose ? othValue : objValue, isLoose? objValue : othValue, key) : undefined;
  41. // Recursively compare objects (susceptible to call stack limits).
  42. if (!(result === undefined ? equalFunc(objValue, othValue, customizer, isLoose, stackA, stackB) : result)) {
  43. return false;
  44. }
  45. skipCtor || (skipCtor = key == 'constructor');
  46. }
  47. if (!skipCtor) {
  48. var objCtor = object.constructor,
  49. othCtor = other.constructor;
  50. // Non `Object` object instances with different constructors are not equal.
  51. if (objCtor != othCtor &&
  52. ('constructor' in object && 'constructor' in other) &&
  53. !(typeof objCtor == 'function' && objCtor instanceof objCtor &&
  54. typeof othCtor == 'function' && othCtor instanceof othCtor)) {
  55. return false;
  56. }
  57. }
  58. return true;
  59. }
  60. export default equalObjects;