Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

isEqual.js 5.3KB

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