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

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

Fix frontend appearance

  • Property mode set to 100644
File size: 5.4 KB
Line 
1var underscore = require('./underscore.js');
2var _setup = require('./_setup.js');
3var _getByteLength = require('./_getByteLength.js');
4var isTypedArray = require('./isTypedArray.js');
5var isFunction = require('./isFunction.js');
6var _stringTagBug = require('./_stringTagBug.js');
7var isDataView = require('./isDataView.js');
8var keys = require('./keys.js');
9var _has = require('./_has.js');
10var _toBufferView = require('./_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 underscore) a = a._wrapped;
34 if (b instanceof underscore) b = b._wrapped;
35 // Compare `[[Class]]` names.
36 var className = _setup.toString.call(a);
37 if (className !== _setup.toString.call(b)) return false;
38 // Work around a bug in IE 10 - Edge 13.
39 if (_stringTagBug.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 _setup.SymbolProto.valueOf.call(a) === _setup.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.
136function isEqual(a, b) {
137 return eq(a, b);
138}
139
140module.exports = isEqual;
Note: See TracBrowser for help on using the repository browser.