No Description

isEqual.js 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. define(['./underscore', './_setup', './_getByteLength', './isTypedArray', './isFunction', './_stringTagBug', './isDataView', './keys', './_has', './_toBufferView'], function (underscore, _setup, _getByteLength, isTypedArray, isFunction, _stringTagBug, isDataView, keys, _has, _toBufferView) {
  2. // We use this string twice, so give it a name for minification.
  3. var tagDataView = '[object DataView]';
  4. // Internal recursive comparison function for `_.isEqual`.
  5. function eq(a, b, aStack, bStack) {
  6. // Identical objects are equal. `0 === -0`, but they aren't identical.
  7. // See the [Harmony `egal` proposal](https://wiki.ecmascript.org/doku.php?id=harmony:egal).
  8. if (a === b) return a !== 0 || 1 / a === 1 / b;
  9. // `null` or `undefined` only equal to itself (strict comparison).
  10. if (a == null || b == null) return false;
  11. // `NaN`s are equivalent, but non-reflexive.
  12. if (a !== a) return b !== b;
  13. // Exhaust primitive checks
  14. var type = typeof a;
  15. if (type !== 'function' && type !== 'object' && typeof b != 'object') return false;
  16. return deepEq(a, b, aStack, bStack);
  17. }
  18. // Internal recursive comparison function for `_.isEqual`.
  19. function deepEq(a, b, aStack, bStack) {
  20. // Unwrap any wrapped objects.
  21. if (a instanceof underscore) a = a._wrapped;
  22. if (b instanceof underscore) b = b._wrapped;
  23. // Compare `[[Class]]` names.
  24. var className = _setup.toString.call(a);
  25. if (className !== _setup.toString.call(b)) return false;
  26. // Work around a bug in IE 10 - Edge 13.
  27. if (_stringTagBug.hasStringTagBug && className == '[object Object]' && isDataView(a)) {
  28. if (!isDataView(b)) return false;
  29. className = tagDataView;
  30. }
  31. switch (className) {
  32. // These types are compared by value.
  33. case '[object RegExp]':
  34. // RegExps are coerced to strings for comparison (Note: '' + /a/i === '/a/i')
  35. case '[object String]':
  36. // Primitives and their corresponding object wrappers are equivalent; thus, `"5"` is
  37. // equivalent to `new String("5")`.
  38. return '' + a === '' + b;
  39. case '[object Number]':
  40. // `NaN`s are equivalent, but non-reflexive.
  41. // Object(NaN) is equivalent to NaN.
  42. if (+a !== +a) return +b !== +b;
  43. // An `egal` comparison is performed for other numeric values.
  44. return +a === 0 ? 1 / +a === 1 / b : +a === +b;
  45. case '[object Date]':
  46. case '[object Boolean]':
  47. // Coerce dates and booleans to numeric primitive values. Dates are compared by their
  48. // millisecond representations. Note that invalid dates with millisecond representations
  49. // of `NaN` are not equivalent.
  50. return +a === +b;
  51. case '[object Symbol]':
  52. return _setup.SymbolProto.valueOf.call(a) === _setup.SymbolProto.valueOf.call(b);
  53. case '[object ArrayBuffer]':
  54. case tagDataView:
  55. // Coerce to typed array so we can fall through.
  56. return deepEq(_toBufferView(a), _toBufferView(b), aStack, bStack);
  57. }
  58. var areArrays = className === '[object Array]';
  59. if (!areArrays && isTypedArray(a)) {
  60. var byteLength = _getByteLength(a);
  61. if (byteLength !== _getByteLength(b)) return false;
  62. if (a.buffer === b.buffer && a.byteOffset === b.byteOffset) return true;
  63. areArrays = true;
  64. }
  65. if (!areArrays) {
  66. if (typeof a != 'object' || typeof b != 'object') return false;
  67. // Objects with different constructors are not equivalent, but `Object`s or `Array`s
  68. // from different frames are.
  69. var aCtor = a.constructor, bCtor = b.constructor;
  70. if (aCtor !== bCtor && !(isFunction(aCtor) && aCtor instanceof aCtor &&
  71. isFunction(bCtor) && bCtor instanceof bCtor)
  72. && ('constructor' in a && 'constructor' in b)) {
  73. return false;
  74. }
  75. }
  76. // Assume equality for cyclic structures. The algorithm for detecting cyclic
  77. // structures is adapted from ES 5.1 section 15.12.3, abstract operation `JO`.
  78. // Initializing stack of traversed objects.
  79. // It's done here since we only need them for objects and arrays comparison.
  80. aStack = aStack || [];
  81. bStack = bStack || [];
  82. var length = aStack.length;
  83. while (length--) {
  84. // Linear search. Performance is inversely proportional to the number of
  85. // unique nested structures.
  86. if (aStack[length] === a) return bStack[length] === b;
  87. }
  88. // Add the first object to the stack of traversed objects.
  89. aStack.push(a);
  90. bStack.push(b);
  91. // Recursively compare objects and arrays.
  92. if (areArrays) {
  93. // Compare array lengths to determine if a deep comparison is necessary.
  94. length = a.length;
  95. if (length !== b.length) return false;
  96. // Deep compare the contents, ignoring non-numeric properties.
  97. while (length--) {
  98. if (!eq(a[length], b[length], aStack, bStack)) return false;
  99. }
  100. } else {
  101. // Deep compare objects.
  102. var _keys = keys(a), key;
  103. length = _keys.length;
  104. // Ensure that both objects contain the same number of properties before comparing deep equality.
  105. if (keys(b).length !== length) return false;
  106. while (length--) {
  107. // Deep compare each member
  108. key = _keys[length];
  109. if (!(_has(b, key) && eq(a[key], b[key], aStack, bStack))) return false;
  110. }
  111. }
  112. // Remove the first object from the stack of traversed objects.
  113. aStack.pop();
  114. bStack.pop();
  115. return true;
  116. }
  117. // Perform a deep comparison to check if two objects are equal.
  118. function isEqual(a, b) {
  119. return eq(a, b);
  120. }
  121. return isEqual;
  122. });