froebel
Version:
TypeScript utility library
31 lines (25 loc) • 960 B
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = equal;
/**
* 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
*/
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]));
}
module.exports = Object.assign(exports.default || {}, exports);