_baseIsEqualDeep.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. var Stack = require('./_Stack'),
  2. equalArrays = require('./_equalArrays'),
  3. equalByTag = require('./_equalByTag'),
  4. equalObjects = require('./_equalObjects'),
  5. getTag = require('./_getTag'),
  6. isArray = require('./isArray'),
  7. isHostObject = require('./_isHostObject'),
  8. isTypedArray = require('./isTypedArray');
  9. /** Used to compose bitmasks for comparison styles. */
  10. var PARTIAL_COMPARE_FLAG = 2;
  11. /** `Object#toString` result references. */
  12. var argsTag = '[object Arguments]',
  13. arrayTag = '[object Array]',
  14. objectTag = '[object Object]';
  15. /** Used for built-in method references. */
  16. var objectProto = Object.prototype;
  17. /** Used to check objects for own properties. */
  18. var hasOwnProperty = objectProto.hasOwnProperty;
  19. /**
  20. * A specialized version of `baseIsEqual` for arrays and objects which performs
  21. * deep comparisons and tracks traversed objects enabling objects with circular
  22. * references to be compared.
  23. *
  24. * @private
  25. * @param {Object} object The object to compare.
  26. * @param {Object} other The other object to compare.
  27. * @param {Function} equalFunc The function to determine equivalents of values.
  28. * @param {Function} [customizer] The function to customize comparisons.
  29. * @param {number} [bitmask] The bitmask of comparison flags. See `baseIsEqual`
  30. * for more details.
  31. * @param {Object} [stack] Tracks traversed `object` and `other` objects.
  32. * @returns {boolean} Returns `true` if the objects are equivalent, else `false`.
  33. */
  34. function baseIsEqualDeep(object, other, equalFunc, customizer, bitmask, stack) {
  35. var objIsArr = isArray(object),
  36. othIsArr = isArray(other),
  37. objTag = arrayTag,
  38. othTag = arrayTag;
  39. if (!objIsArr) {
  40. objTag = getTag(object);
  41. objTag = objTag == argsTag ? objectTag : objTag;
  42. }
  43. if (!othIsArr) {
  44. othTag = getTag(other);
  45. othTag = othTag == argsTag ? objectTag : othTag;
  46. }
  47. var objIsObj = objTag == objectTag && !isHostObject(object),
  48. othIsObj = othTag == objectTag && !isHostObject(other),
  49. isSameTag = objTag == othTag;
  50. if (isSameTag && !objIsObj) {
  51. stack || (stack = new Stack);
  52. return (objIsArr || isTypedArray(object))
  53. ? equalArrays(object, other, equalFunc, customizer, bitmask, stack)
  54. : equalByTag(object, other, objTag, equalFunc, customizer, bitmask, stack);
  55. }
  56. if (!(bitmask & PARTIAL_COMPARE_FLAG)) {
  57. var objIsWrapped = objIsObj && hasOwnProperty.call(object, '__wrapped__'),
  58. othIsWrapped = othIsObj && hasOwnProperty.call(other, '__wrapped__');
  59. if (objIsWrapped || othIsWrapped) {
  60. var objUnwrapped = objIsWrapped ? object.value() : object,
  61. othUnwrapped = othIsWrapped ? other.value() : other;
  62. stack || (stack = new Stack);
  63. return equalFunc(objUnwrapped, othUnwrapped, customizer, bitmask, stack);
  64. }
  65. }
  66. if (!isSameTag) {
  67. return false;
  68. }
  69. stack || (stack = new Stack);
  70. return equalObjects(object, other, equalFunc, customizer, bitmask, stack);
  71. }
  72. module.exports = baseIsEqualDeep;