UNPKG

froebel

Version:
22 lines (19 loc) 804 B
/** * Checks if `a` and `b` are structurally equal using the following algorithm: * * - primitives are compared by value * - functions are compared by reference * - objects (including arrays) are checked to have the same properties and * their values are compared recursively using the same algorithm */ export default function equal(a, b) { if (typeof a !== "object" && typeof b !== "object") return a === b; if (typeof a === "object" !== (typeof b === "object")) return false; if (a === b) return true; // null or ref equality if (a === null || b === null) return false; if (Array.isArray(a) !== Array.isArray(b)) return false; if (Object.keys(a).length !== Object.keys(b).length) { return false; } return Object.entries(a).every(([k, v]) => k in b && equal(v, b[k])); }