mirror of
https://github.com/Start9Labs/start-os.git
synced 2026-03-26 10:21:52 +00:00
35 lines
1.2 KiB
TypeScript
35 lines
1.2 KiB
TypeScript
/**
|
|
* 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 recursively.
|
|
*
|
|
* @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
|
|
* ```
|
|
*/
|
|
export function deepEqual(...args: unknown[]) {
|
|
const objects = args.filter(
|
|
(x): x is object => 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
|
|
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] as any)[key], (x as any)[key])) return false
|
|
}
|
|
}
|
|
return true
|
|
}
|