You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

comparisons.js 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. // Currently in sync with Node.js lib/internal/util/comparisons.js
  2. // https://github.com/nodejs/node/commit/112cc7c27551254aa2b17098fb774867f05ed0d9
  3. 'use strict';
  4. function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }
  5. function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); }
  6. function _iterableToArrayLimit(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
  7. function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
  8. function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
  9. var regexFlagsSupported = /a/g.flags !== undefined;
  10. var arrayFromSet = function arrayFromSet(set) {
  11. var array = [];
  12. set.forEach(function (value) {
  13. return array.push(value);
  14. });
  15. return array;
  16. };
  17. var arrayFromMap = function arrayFromMap(map) {
  18. var array = [];
  19. map.forEach(function (value, key) {
  20. return array.push([key, value]);
  21. });
  22. return array;
  23. };
  24. var objectIs = Object.is ? Object.is : require('object-is');
  25. var objectGetOwnPropertySymbols = Object.getOwnPropertySymbols ? Object.getOwnPropertySymbols : function () {
  26. return [];
  27. };
  28. var numberIsNaN = Number.isNaN ? Number.isNaN : require('is-nan');
  29. function uncurryThis(f) {
  30. return f.call.bind(f);
  31. }
  32. var hasOwnProperty = uncurryThis(Object.prototype.hasOwnProperty);
  33. var propertyIsEnumerable = uncurryThis(Object.prototype.propertyIsEnumerable);
  34. var objectToString = uncurryThis(Object.prototype.toString);
  35. var _require$types = require('util/').types,
  36. isAnyArrayBuffer = _require$types.isAnyArrayBuffer,
  37. isArrayBufferView = _require$types.isArrayBufferView,
  38. isDate = _require$types.isDate,
  39. isMap = _require$types.isMap,
  40. isRegExp = _require$types.isRegExp,
  41. isSet = _require$types.isSet,
  42. isNativeError = _require$types.isNativeError,
  43. isBoxedPrimitive = _require$types.isBoxedPrimitive,
  44. isNumberObject = _require$types.isNumberObject,
  45. isStringObject = _require$types.isStringObject,
  46. isBooleanObject = _require$types.isBooleanObject,
  47. isBigIntObject = _require$types.isBigIntObject,
  48. isSymbolObject = _require$types.isSymbolObject,
  49. isFloat32Array = _require$types.isFloat32Array,
  50. isFloat64Array = _require$types.isFloat64Array;
  51. function isNonIndex(key) {
  52. if (key.length === 0 || key.length > 10) return true;
  53. for (var i = 0; i < key.length; i++) {
  54. var code = key.charCodeAt(i);
  55. if (code < 48 || code > 57) return true;
  56. } // The maximum size for an array is 2 ** 32 -1.
  57. return key.length === 10 && key >= Math.pow(2, 32);
  58. }
  59. function getOwnNonIndexProperties(value) {
  60. return Object.keys(value).filter(isNonIndex).concat(objectGetOwnPropertySymbols(value).filter(Object.prototype.propertyIsEnumerable.bind(value)));
  61. } // Taken from https://github.com/feross/buffer/blob/680e9e5e488f22aac27599a57dc844a6315928dd/index.js
  62. // original notice:
  63. /*!
  64. * The buffer module from node.js, for the browser.
  65. *
  66. * @author Feross Aboukhadijeh <feross@feross.org> <http://feross.org>
  67. * @license MIT
  68. */
  69. function compare(a, b) {
  70. if (a === b) {
  71. return 0;
  72. }
  73. var x = a.length;
  74. var y = b.length;
  75. for (var i = 0, len = Math.min(x, y); i < len; ++i) {
  76. if (a[i] !== b[i]) {
  77. x = a[i];
  78. y = b[i];
  79. break;
  80. }
  81. }
  82. if (x < y) {
  83. return -1;
  84. }
  85. if (y < x) {
  86. return 1;
  87. }
  88. return 0;
  89. }
  90. var ONLY_ENUMERABLE = undefined;
  91. var kStrict = true;
  92. var kLoose = false;
  93. var kNoIterator = 0;
  94. var kIsArray = 1;
  95. var kIsSet = 2;
  96. var kIsMap = 3; // Check if they have the same source and flags
  97. function areSimilarRegExps(a, b) {
  98. return regexFlagsSupported ? a.source === b.source && a.flags === b.flags : RegExp.prototype.toString.call(a) === RegExp.prototype.toString.call(b);
  99. }
  100. function areSimilarFloatArrays(a, b) {
  101. if (a.byteLength !== b.byteLength) {
  102. return false;
  103. }
  104. for (var offset = 0; offset < a.byteLength; offset++) {
  105. if (a[offset] !== b[offset]) {
  106. return false;
  107. }
  108. }
  109. return true;
  110. }
  111. function areSimilarTypedArrays(a, b) {
  112. if (a.byteLength !== b.byteLength) {
  113. return false;
  114. }
  115. return compare(new Uint8Array(a.buffer, a.byteOffset, a.byteLength), new Uint8Array(b.buffer, b.byteOffset, b.byteLength)) === 0;
  116. }
  117. function areEqualArrayBuffers(buf1, buf2) {
  118. return buf1.byteLength === buf2.byteLength && compare(new Uint8Array(buf1), new Uint8Array(buf2)) === 0;
  119. }
  120. function isEqualBoxedPrimitive(val1, val2) {
  121. if (isNumberObject(val1)) {
  122. return isNumberObject(val2) && objectIs(Number.prototype.valueOf.call(val1), Number.prototype.valueOf.call(val2));
  123. }
  124. if (isStringObject(val1)) {
  125. return isStringObject(val2) && String.prototype.valueOf.call(val1) === String.prototype.valueOf.call(val2);
  126. }
  127. if (isBooleanObject(val1)) {
  128. return isBooleanObject(val2) && Boolean.prototype.valueOf.call(val1) === Boolean.prototype.valueOf.call(val2);
  129. }
  130. if (isBigIntObject(val1)) {
  131. return isBigIntObject(val2) && BigInt.prototype.valueOf.call(val1) === BigInt.prototype.valueOf.call(val2);
  132. }
  133. return isSymbolObject(val2) && Symbol.prototype.valueOf.call(val1) === Symbol.prototype.valueOf.call(val2);
  134. } // Notes: Type tags are historical [[Class]] properties that can be set by
  135. // FunctionTemplate::SetClassName() in C++ or Symbol.toStringTag in JS
  136. // and retrieved using Object.prototype.toString.call(obj) in JS
  137. // See https://tc39.github.io/ecma262/#sec-object.prototype.tostring
  138. // for a list of tags pre-defined in the spec.
  139. // There are some unspecified tags in the wild too (e.g. typed array tags).
  140. // Since tags can be altered, they only serve fast failures
  141. //
  142. // Typed arrays and buffers are checked by comparing the content in their
  143. // underlying ArrayBuffer. This optimization requires that it's
  144. // reasonable to interpret their underlying memory in the same way,
  145. // which is checked by comparing their type tags.
  146. // (e.g. a Uint8Array and a Uint16Array with the same memory content
  147. // could still be different because they will be interpreted differently).
  148. //
  149. // For strict comparison, objects should have
  150. // a) The same built-in type tags
  151. // b) The same prototypes.
  152. function innerDeepEqual(val1, val2, strict, memos) {
  153. // All identical values are equivalent, as determined by ===.
  154. if (val1 === val2) {
  155. if (val1 !== 0) return true;
  156. return strict ? objectIs(val1, val2) : true;
  157. } // Check more closely if val1 and val2 are equal.
  158. if (strict) {
  159. if (_typeof(val1) !== 'object') {
  160. return typeof val1 === 'number' && numberIsNaN(val1) && numberIsNaN(val2);
  161. }
  162. if (_typeof(val2) !== 'object' || val1 === null || val2 === null) {
  163. return false;
  164. }
  165. if (Object.getPrototypeOf(val1) !== Object.getPrototypeOf(val2)) {
  166. return false;
  167. }
  168. } else {
  169. if (val1 === null || _typeof(val1) !== 'object') {
  170. if (val2 === null || _typeof(val2) !== 'object') {
  171. // eslint-disable-next-line eqeqeq
  172. return val1 == val2;
  173. }
  174. return false;
  175. }
  176. if (val2 === null || _typeof(val2) !== 'object') {
  177. return false;
  178. }
  179. }
  180. var val1Tag = objectToString(val1);
  181. var val2Tag = objectToString(val2);
  182. if (val1Tag !== val2Tag) {
  183. return false;
  184. }
  185. if (Array.isArray(val1)) {
  186. // Check for sparse arrays and general fast path
  187. if (val1.length !== val2.length) {
  188. return false;
  189. }
  190. var keys1 = getOwnNonIndexProperties(val1, ONLY_ENUMERABLE);
  191. var keys2 = getOwnNonIndexProperties(val2, ONLY_ENUMERABLE);
  192. if (keys1.length !== keys2.length) {
  193. return false;
  194. }
  195. return keyCheck(val1, val2, strict, memos, kIsArray, keys1);
  196. } // [browserify] This triggers on certain types in IE (Map/Set) so we don't
  197. // wan't to early return out of the rest of the checks. However we can check
  198. // if the second value is one of these values and the first isn't.
  199. if (val1Tag === '[object Object]') {
  200. // return keyCheck(val1, val2, strict, memos, kNoIterator);
  201. if (!isMap(val1) && isMap(val2) || !isSet(val1) && isSet(val2)) {
  202. return false;
  203. }
  204. }
  205. if (isDate(val1)) {
  206. if (!isDate(val2) || Date.prototype.getTime.call(val1) !== Date.prototype.getTime.call(val2)) {
  207. return false;
  208. }
  209. } else if (isRegExp(val1)) {
  210. if (!isRegExp(val2) || !areSimilarRegExps(val1, val2)) {
  211. return false;
  212. }
  213. } else if (isNativeError(val1) || val1 instanceof Error) {
  214. // Do not compare the stack as it might differ even though the error itself
  215. // is otherwise identical.
  216. if (val1.message !== val2.message || val1.name !== val2.name) {
  217. return false;
  218. }
  219. } else if (isArrayBufferView(val1)) {
  220. if (!strict && (isFloat32Array(val1) || isFloat64Array(val1))) {
  221. if (!areSimilarFloatArrays(val1, val2)) {
  222. return false;
  223. }
  224. } else if (!areSimilarTypedArrays(val1, val2)) {
  225. return false;
  226. } // Buffer.compare returns true, so val1.length === val2.length. If they both
  227. // only contain numeric keys, we don't need to exam further than checking
  228. // the symbols.
  229. var _keys = getOwnNonIndexProperties(val1, ONLY_ENUMERABLE);
  230. var _keys2 = getOwnNonIndexProperties(val2, ONLY_ENUMERABLE);
  231. if (_keys.length !== _keys2.length) {
  232. return false;
  233. }
  234. return keyCheck(val1, val2, strict, memos, kNoIterator, _keys);
  235. } else if (isSet(val1)) {
  236. if (!isSet(val2) || val1.size !== val2.size) {
  237. return false;
  238. }
  239. return keyCheck(val1, val2, strict, memos, kIsSet);
  240. } else if (isMap(val1)) {
  241. if (!isMap(val2) || val1.size !== val2.size) {
  242. return false;
  243. }
  244. return keyCheck(val1, val2, strict, memos, kIsMap);
  245. } else if (isAnyArrayBuffer(val1)) {
  246. if (!areEqualArrayBuffers(val1, val2)) {
  247. return false;
  248. }
  249. } else if (isBoxedPrimitive(val1) && !isEqualBoxedPrimitive(val1, val2)) {
  250. return false;
  251. }
  252. return keyCheck(val1, val2, strict, memos, kNoIterator);
  253. }
  254. function getEnumerables(val, keys) {
  255. return keys.filter(function (k) {
  256. return propertyIsEnumerable(val, k);
  257. });
  258. }
  259. function keyCheck(val1, val2, strict, memos, iterationType, aKeys) {
  260. // For all remaining Object pairs, including Array, objects and Maps,
  261. // equivalence is determined by having:
  262. // a) The same number of owned enumerable properties
  263. // b) The same set of keys/indexes (although not necessarily the same order)
  264. // c) Equivalent values for every corresponding key/index
  265. // d) For Sets and Maps, equal contents
  266. // Note: this accounts for both named and indexed properties on Arrays.
  267. if (arguments.length === 5) {
  268. aKeys = Object.keys(val1);
  269. var bKeys = Object.keys(val2); // The pair must have the same number of owned properties.
  270. if (aKeys.length !== bKeys.length) {
  271. return false;
  272. }
  273. } // Cheap key test
  274. var i = 0;
  275. for (; i < aKeys.length; i++) {
  276. if (!hasOwnProperty(val2, aKeys[i])) {
  277. return false;
  278. }
  279. }
  280. if (strict && arguments.length === 5) {
  281. var symbolKeysA = objectGetOwnPropertySymbols(val1);
  282. if (symbolKeysA.length !== 0) {
  283. var count = 0;
  284. for (i = 0; i < symbolKeysA.length; i++) {
  285. var key = symbolKeysA[i];
  286. if (propertyIsEnumerable(val1, key)) {
  287. if (!propertyIsEnumerable(val2, key)) {
  288. return false;
  289. }
  290. aKeys.push(key);
  291. count++;
  292. } else if (propertyIsEnumerable(val2, key)) {
  293. return false;
  294. }
  295. }
  296. var symbolKeysB = objectGetOwnPropertySymbols(val2);
  297. if (symbolKeysA.length !== symbolKeysB.length && getEnumerables(val2, symbolKeysB).length !== count) {
  298. return false;
  299. }
  300. } else {
  301. var _symbolKeysB = objectGetOwnPropertySymbols(val2);
  302. if (_symbolKeysB.length !== 0 && getEnumerables(val2, _symbolKeysB).length !== 0) {
  303. return false;
  304. }
  305. }
  306. }
  307. if (aKeys.length === 0 && (iterationType === kNoIterator || iterationType === kIsArray && val1.length === 0 || val1.size === 0)) {
  308. return true;
  309. } // Use memos to handle cycles.
  310. if (memos === undefined) {
  311. memos = {
  312. val1: new Map(),
  313. val2: new Map(),
  314. position: 0
  315. };
  316. } else {
  317. // We prevent up to two map.has(x) calls by directly retrieving the value
  318. // and checking for undefined. The map can only contain numbers, so it is
  319. // safe to check for undefined only.
  320. var val2MemoA = memos.val1.get(val1);
  321. if (val2MemoA !== undefined) {
  322. var val2MemoB = memos.val2.get(val2);
  323. if (val2MemoB !== undefined) {
  324. return val2MemoA === val2MemoB;
  325. }
  326. }
  327. memos.position++;
  328. }
  329. memos.val1.set(val1, memos.position);
  330. memos.val2.set(val2, memos.position);
  331. var areEq = objEquiv(val1, val2, strict, aKeys, memos, iterationType);
  332. memos.val1.delete(val1);
  333. memos.val2.delete(val2);
  334. return areEq;
  335. }
  336. function setHasEqualElement(set, val1, strict, memo) {
  337. // Go looking.
  338. var setValues = arrayFromSet(set);
  339. for (var i = 0; i < setValues.length; i++) {
  340. var val2 = setValues[i];
  341. if (innerDeepEqual(val1, val2, strict, memo)) {
  342. // Remove the matching element to make sure we do not check that again.
  343. set.delete(val2);
  344. return true;
  345. }
  346. }
  347. return false;
  348. } // See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness#Loose_equality_using
  349. // Sadly it is not possible to detect corresponding values properly in case the
  350. // type is a string, number, bigint or boolean. The reason is that those values
  351. // can match lots of different string values (e.g., 1n == '+00001').
  352. function findLooseMatchingPrimitives(prim) {
  353. switch (_typeof(prim)) {
  354. case 'undefined':
  355. return null;
  356. case 'object':
  357. // Only pass in null as object!
  358. return undefined;
  359. case 'symbol':
  360. return false;
  361. case 'string':
  362. prim = +prim;
  363. // Loose equal entries exist only if the string is possible to convert to
  364. // a regular number and not NaN.
  365. // Fall through
  366. case 'number':
  367. if (numberIsNaN(prim)) {
  368. return false;
  369. }
  370. }
  371. return true;
  372. }
  373. function setMightHaveLoosePrim(a, b, prim) {
  374. var altValue = findLooseMatchingPrimitives(prim);
  375. if (altValue != null) return altValue;
  376. return b.has(altValue) && !a.has(altValue);
  377. }
  378. function mapMightHaveLoosePrim(a, b, prim, item, memo) {
  379. var altValue = findLooseMatchingPrimitives(prim);
  380. if (altValue != null) {
  381. return altValue;
  382. }
  383. var curB = b.get(altValue);
  384. if (curB === undefined && !b.has(altValue) || !innerDeepEqual(item, curB, false, memo)) {
  385. return false;
  386. }
  387. return !a.has(altValue) && innerDeepEqual(item, curB, false, memo);
  388. }
  389. function setEquiv(a, b, strict, memo) {
  390. // This is a lazily initiated Set of entries which have to be compared
  391. // pairwise.
  392. var set = null;
  393. var aValues = arrayFromSet(a);
  394. for (var i = 0; i < aValues.length; i++) {
  395. var val = aValues[i]; // Note: Checking for the objects first improves the performance for object
  396. // heavy sets but it is a minor slow down for primitives. As they are fast
  397. // to check this improves the worst case scenario instead.
  398. if (_typeof(val) === 'object' && val !== null) {
  399. if (set === null) {
  400. set = new Set();
  401. } // If the specified value doesn't exist in the second set its an not null
  402. // object (or non strict only: a not matching primitive) we'll need to go
  403. // hunting for something thats deep-(strict-)equal to it. To make this
  404. // O(n log n) complexity we have to copy these values in a new set first.
  405. set.add(val);
  406. } else if (!b.has(val)) {
  407. if (strict) return false; // Fast path to detect missing string, symbol, undefined and null values.
  408. if (!setMightHaveLoosePrim(a, b, val)) {
  409. return false;
  410. }
  411. if (set === null) {
  412. set = new Set();
  413. }
  414. set.add(val);
  415. }
  416. }
  417. if (set !== null) {
  418. var bValues = arrayFromSet(b);
  419. for (var _i = 0; _i < bValues.length; _i++) {
  420. var _val = bValues[_i]; // We have to check if a primitive value is already
  421. // matching and only if it's not, go hunting for it.
  422. if (_typeof(_val) === 'object' && _val !== null) {
  423. if (!setHasEqualElement(set, _val, strict, memo)) return false;
  424. } else if (!strict && !a.has(_val) && !setHasEqualElement(set, _val, strict, memo)) {
  425. return false;
  426. }
  427. }
  428. return set.size === 0;
  429. }
  430. return true;
  431. }
  432. function mapHasEqualEntry(set, map, key1, item1, strict, memo) {
  433. // To be able to handle cases like:
  434. // Map([[{}, 'a'], [{}, 'b']]) vs Map([[{}, 'b'], [{}, 'a']])
  435. // ... we need to consider *all* matching keys, not just the first we find.
  436. var setValues = arrayFromSet(set);
  437. for (var i = 0; i < setValues.length; i++) {
  438. var key2 = setValues[i];
  439. if (innerDeepEqual(key1, key2, strict, memo) && innerDeepEqual(item1, map.get(key2), strict, memo)) {
  440. set.delete(key2);
  441. return true;
  442. }
  443. }
  444. return false;
  445. }
  446. function mapEquiv(a, b, strict, memo) {
  447. var set = null;
  448. var aEntries = arrayFromMap(a);
  449. for (var i = 0; i < aEntries.length; i++) {
  450. var _aEntries$i = _slicedToArray(aEntries[i], 2),
  451. key = _aEntries$i[0],
  452. item1 = _aEntries$i[1];
  453. if (_typeof(key) === 'object' && key !== null) {
  454. if (set === null) {
  455. set = new Set();
  456. }
  457. set.add(key);
  458. } else {
  459. // By directly retrieving the value we prevent another b.has(key) check in
  460. // almost all possible cases.
  461. var item2 = b.get(key);
  462. if (item2 === undefined && !b.has(key) || !innerDeepEqual(item1, item2, strict, memo)) {
  463. if (strict) return false; // Fast path to detect missing string, symbol, undefined and null
  464. // keys.
  465. if (!mapMightHaveLoosePrim(a, b, key, item1, memo)) return false;
  466. if (set === null) {
  467. set = new Set();
  468. }
  469. set.add(key);
  470. }
  471. }
  472. }
  473. if (set !== null) {
  474. var bEntries = arrayFromMap(b);
  475. for (var _i2 = 0; _i2 < bEntries.length; _i2++) {
  476. var _bEntries$_i = _slicedToArray(bEntries[_i2], 2),
  477. key = _bEntries$_i[0],
  478. item = _bEntries$_i[1];
  479. if (_typeof(key) === 'object' && key !== null) {
  480. if (!mapHasEqualEntry(set, a, key, item, strict, memo)) return false;
  481. } else if (!strict && (!a.has(key) || !innerDeepEqual(a.get(key), item, false, memo)) && !mapHasEqualEntry(set, a, key, item, false, memo)) {
  482. return false;
  483. }
  484. }
  485. return set.size === 0;
  486. }
  487. return true;
  488. }
  489. function objEquiv(a, b, strict, keys, memos, iterationType) {
  490. // Sets and maps don't have their entries accessible via normal object
  491. // properties.
  492. var i = 0;
  493. if (iterationType === kIsSet) {
  494. if (!setEquiv(a, b, strict, memos)) {
  495. return false;
  496. }
  497. } else if (iterationType === kIsMap) {
  498. if (!mapEquiv(a, b, strict, memos)) {
  499. return false;
  500. }
  501. } else if (iterationType === kIsArray) {
  502. for (; i < a.length; i++) {
  503. if (hasOwnProperty(a, i)) {
  504. if (!hasOwnProperty(b, i) || !innerDeepEqual(a[i], b[i], strict, memos)) {
  505. return false;
  506. }
  507. } else if (hasOwnProperty(b, i)) {
  508. return false;
  509. } else {
  510. // Array is sparse.
  511. var keysA = Object.keys(a);
  512. for (; i < keysA.length; i++) {
  513. var key = keysA[i];
  514. if (!hasOwnProperty(b, key) || !innerDeepEqual(a[key], b[key], strict, memos)) {
  515. return false;
  516. }
  517. }
  518. if (keysA.length !== Object.keys(b).length) {
  519. return false;
  520. }
  521. return true;
  522. }
  523. }
  524. } // The pair must have equivalent values for every corresponding key.
  525. // Possibly expensive deep test:
  526. for (i = 0; i < keys.length; i++) {
  527. var _key = keys[i];
  528. if (!innerDeepEqual(a[_key], b[_key], strict, memos)) {
  529. return false;
  530. }
  531. }
  532. return true;
  533. }
  534. function isDeepEqual(val1, val2) {
  535. return innerDeepEqual(val1, val2, kLoose);
  536. }
  537. function isDeepStrictEqual(val1, val2) {
  538. return innerDeepEqual(val1, val2, kStrict);
  539. }
  540. module.exports = {
  541. isDeepEqual: isDeepEqual,
  542. isDeepStrictEqual: isDeepStrictEqual
  543. };