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

isEqual.js 5.2KB

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