Fix `isDeepEqual` function

This commit is contained in:
Joshua Lochner 2023-05-02 05:20:07 +02:00
parent f67d9642be
commit 27c9d8d570
1 changed files with 22 additions and 0 deletions

View File

@ -1,7 +1,29 @@
/**
* Check if two arrays are the same:
* Adapted from https://stackoverflow.com/a/16436975/13989043
* @param {any[]} a
* @param {any[]} b
* @returns {boolean} True if the arrays are equal, false if not
*/
function arraysEqual(a, b) {
if (a === b) return true;
if (a == null || b == null) return false;
if (a.length !== b.length) return false;
for (let i = 0; i < a.length; ++i) {
if (a[i] !== b[i]) return false;
}
return true;
}
export function isDeepEqual(obj1, obj2, {
tol = 1e-3
} = {}) {
if (Array.isArray(obj1) && Array.isArray(obj2)) {
return arraysEqual(obj1, obj2);
}
// Get the keys of both objects
const obj1Keys = Object.keys(obj1);
const obj2Keys = Object.keys(obj2);