UNPKG

1.9 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4Object.defineProperty(exports, "__esModule", {
5 value: true
6});
7exports.default = void 0;
8var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
9var _warning = _interopRequireDefault(require("./warning"));
10/**
11 * Deeply compares two object literals.
12 * @param obj1 object 1
13 * @param obj2 object 2
14 * @param shallow shallow compare
15 * @returns
16 */
17function isEqual(obj1, obj2) {
18 var shallow = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
19 // https://github.com/mapbox/mapbox-gl-js/pull/5979/files#diff-fde7145050c47cc3a306856efd5f9c3016e86e859de9afbd02c879be5067e58f
20 var refSet = new Set();
21 function deepEqual(a, b) {
22 var level = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1;
23 var circular = refSet.has(a);
24 (0, _warning.default)(!circular, 'Warning: There may be circular references');
25 if (circular) {
26 return false;
27 }
28 if (a === b) {
29 return true;
30 }
31 if (shallow && level > 1) {
32 return false;
33 }
34 refSet.add(a);
35 var newLevel = level + 1;
36 if (Array.isArray(a)) {
37 if (!Array.isArray(b) || a.length !== b.length) {
38 return false;
39 }
40 for (var i = 0; i < a.length; i++) {
41 if (!deepEqual(a[i], b[i], newLevel)) {
42 return false;
43 }
44 }
45 return true;
46 }
47 if (a && b && (0, _typeof2.default)(a) === 'object' && (0, _typeof2.default)(b) === 'object') {
48 var keys = Object.keys(a);
49 if (keys.length !== Object.keys(b).length) {
50 return false;
51 }
52 return keys.every(function (key) {
53 return deepEqual(a[key], b[key], newLevel);
54 });
55 }
56 // other
57 return false;
58 }
59 return deepEqual(obj1, obj2);
60}
61var _default = exports.default = isEqual;
\No newline at end of file