@signaldb/core
Version:
SignalDB is a client-side database that provides a simple MongoDB-like interface to the data with first-class typescript support to achieve an optimistic UI. Data persistence can be achieved by using storage providers that store the data through a JSON in
34 lines (33 loc) • 1.16 kB
JavaScript
//#region src/utils/isEqual.ts
/**
* Compares two values for deep equality.
* @param a - The first value to compare.
* @param b - The second value to compare.
* @returns - Returns `true` if the two values are deeply equal, otherwise `false`.
* @example
* isEqual({ a: 1 }, { a: 1 }); // true
* isEqual([1, 2], [1, 2]); // true
* isEqual(new Date(0), new Date(0)); // true
* isEqual(/abc/, /abc/); // true
* isEqual({ a: 1 }, { a: 2 }); // false
* isEqual(null, null); // true
*/
function isEqual(a, b) {
if (Object.is(a, b)) return true;
if (a instanceof RegExp && b instanceof RegExp) return a.toString() === b.toString();
if (a instanceof Date && b instanceof Date) return a.getTime() === b.getTime();
if (typeof a !== "object") return false;
if (typeof b !== "object") return false;
if (a === null) return false;
if (b === null) return false;
const aKeys = Object.keys(a);
const bKeys = Object.keys(b);
if (aKeys.length !== bKeys.length) return false;
for (const key of aKeys) {
if (!bKeys.includes(key)) return false;
if (!isEqual(a[key], b[key])) return false;
}
return true;
}
//#endregion
export { isEqual as default };