48 lines
1.7 KiB
JavaScript
48 lines
1.7 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.deepEqual = deepEqual;
|
|
/**
|
|
* Performs a deep structural equality check across all provided arguments.
|
|
* Returns true only if every argument is deeply equal to every other argument.
|
|
* Handles primitives, arrays, and plain objects (JSON-like) recursively.
|
|
*
|
|
* Non-plain objects (Set, Map, Date, etc.) are compared by reference only,
|
|
* since Object.keys() does not enumerate their contents.
|
|
*
|
|
* @param args - Two or more values to compare for deep equality
|
|
* @returns True if all arguments are deeply equal
|
|
*
|
|
* @example
|
|
* ```ts
|
|
* deepEqual({ a: 1 }, { a: 1 }) // true
|
|
* deepEqual([1, 2], [1, 2], [1, 2]) // true
|
|
* deepEqual({ a: 1 }, { a: 2 }) // false
|
|
* ```
|
|
*/
|
|
function deepEqual(...args) {
|
|
const objects = args.filter((x) => typeof x === 'object' && x !== null);
|
|
if (objects.length === 0) {
|
|
for (const x of args)
|
|
if (x !== args[0])
|
|
return false;
|
|
return true;
|
|
}
|
|
if (objects.length !== args.length)
|
|
return false;
|
|
if (objects.some(Array.isArray) && !objects.every(Array.isArray))
|
|
return false;
|
|
if (objects.some((x) => !Array.isArray(x) && Object.getPrototypeOf(x) !== Object.prototype)) {
|
|
return (objects.reduce((a, b) => (a === b ? a : null), objects[0]) !== null);
|
|
}
|
|
const allKeys = new Set(objects.flatMap((x) => Object.keys(x)));
|
|
for (const key of allKeys) {
|
|
for (const x of objects) {
|
|
if (!(key in x))
|
|
return false;
|
|
if (!deepEqual(objects[0][key], x[key]))
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
//# sourceMappingURL=deepEqual.js.map
|