No Description

isEqual.js 5.4KB

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