123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- define(['./_setup', './isFunction', './_has', './keys', './underscore', './_getByteLength', './isTypedArray'], function (_setup, isFunction, _has, keys, underscore, _getByteLength, isTypedArray) {
-
- // Internal recursive comparison function for `_.isEqual`.
- function eq(a, b, aStack, bStack) {
- // Identical objects are equal. `0 === -0`, but they aren't identical.
- // See the [Harmony `egal` proposal](https://wiki.ecmascript.org/doku.php?id=harmony:egal).
- if (a === b) return a !== 0 || 1 / a === 1 / b;
- // `null` or `undefined` only equal to itself (strict comparison).
- if (a == null || b == null) return false;
- // `NaN`s are equivalent, but non-reflexive.
- if (a !== a) return b !== b;
- // Exhaust primitive checks
- var type = typeof a;
- if (type !== 'function' && type !== 'object' && typeof b != 'object') return false;
- return deepEq(a, b, aStack, bStack);
- }
-
- // Internal recursive comparison function for `_.isEqual`.
- function deepEq(a, b, aStack, bStack) {
- // Unwrap any wrapped objects.
- if (a instanceof underscore) a = a._wrapped;
- if (b instanceof underscore) b = b._wrapped;
- // Compare `[[Class]]` names.
- var className = _setup.toString.call(a);
- if (className !== _setup.toString.call(b)) return false;
- switch (className) {
- // These types are compared by value.
- case '[object RegExp]':
- // RegExps are coerced to strings for comparison (Note: '' + /a/i === '/a/i')
- case '[object String]':
- // Primitives and their corresponding object wrappers are equivalent; thus, `"5"` is
- // equivalent to `new String("5")`.
- return '' + a === '' + b;
- case '[object Number]':
- // `NaN`s are equivalent, but non-reflexive.
- // Object(NaN) is equivalent to NaN.
- if (+a !== +a) return +b !== +b;
- // An `egal` comparison is performed for other numeric values.
- return +a === 0 ? 1 / +a === 1 / b : +a === +b;
- case '[object Date]':
- case '[object Boolean]':
- // Coerce dates and booleans to numeric primitive values. Dates are compared by their
- // millisecond representations. Note that invalid dates with millisecond representations
- // of `NaN` are not equivalent.
- return +a === +b;
- case '[object Symbol]':
- return _setup.SymbolProto.valueOf.call(a) === _setup.SymbolProto.valueOf.call(b);
- case '[object ArrayBuffer]':
- // Coerce to `DataView` so we can fall through to the next case.
- return deepEq(new DataView(a), new DataView(b), aStack, bStack);
- case '[object DataView]':
- var byteLength = _getByteLength(a);
- if (byteLength !== _getByteLength(b)) {
- return false;
- }
- while (byteLength--) {
- if (a.getUint8(byteLength) !== b.getUint8(byteLength)) {
- return false;
- }
- }
- return true;
- }
-
- if (isTypedArray(a)) {
- // Coerce typed arrays to `DataView`.
- return deepEq(new DataView(a.buffer), new DataView(b.buffer), aStack, bStack);
- }
-
- var areArrays = className === '[object Array]';
- if (!areArrays) {
- if (typeof a != 'object' || typeof b != 'object') return false;
-
- // Objects with different constructors are not equivalent, but `Object`s or `Array`s
- // from different frames are.
- var aCtor = a.constructor, bCtor = b.constructor;
- if (aCtor !== bCtor && !(isFunction(aCtor) && aCtor instanceof aCtor &&
- isFunction(bCtor) && bCtor instanceof bCtor)
- && ('constructor' in a && 'constructor' in b)) {
- return false;
- }
- }
- // Assume equality for cyclic structures. The algorithm for detecting cyclic
- // structures is adapted from ES 5.1 section 15.12.3, abstract operation `JO`.
-
- // Initializing stack of traversed objects.
- // It's done here since we only need them for objects and arrays comparison.
- aStack = aStack || [];
- bStack = bStack || [];
- var length = aStack.length;
- while (length--) {
- // Linear search. Performance is inversely proportional to the number of
- // unique nested structures.
- if (aStack[length] === a) return bStack[length] === b;
- }
-
- // Add the first object to the stack of traversed objects.
- aStack.push(a);
- bStack.push(b);
-
- // Recursively compare objects and arrays.
- if (areArrays) {
- // Compare array lengths to determine if a deep comparison is necessary.
- length = a.length;
- if (length !== b.length) return false;
- // Deep compare the contents, ignoring non-numeric properties.
- while (length--) {
- if (!eq(a[length], b[length], aStack, bStack)) return false;
- }
- } else {
- // Deep compare objects.
- var _keys = keys(a), key;
- length = _keys.length;
- // Ensure that both objects contain the same number of properties before comparing deep equality.
- if (keys(b).length !== length) return false;
- while (length--) {
- // Deep compare each member
- key = _keys[length];
- if (!(_has(b, key) && eq(a[key], b[key], aStack, bStack))) return false;
- }
- }
- // Remove the first object from the stack of traversed objects.
- aStack.pop();
- bStack.pop();
- return true;
- }
-
- // Perform a deep comparison to check if two objects are equal.
- function isEqual(a, b) {
- return eq(a, b);
- }
-
- return isEqual;
-
- });
|