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