source: frontend/node_modules/underscore/modules/isEqual.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 11 days ago

Fix frontend appearance

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