Files
orca/src/shared/persisted-ui-equality.ts
T
2026-06-17 13:02:47 -07:00

47 lines
1.3 KiB
TypeScript

export function persistedUIValuesEqual(left: unknown, right: unknown): boolean {
if (Object.is(left, right)) {
return true
}
if (left instanceof Set || right instanceof Set) {
if (!(left instanceof Set) || !(right instanceof Set) || left.size !== right.size) {
return false
}
for (const value of left) {
if (!right.has(value)) {
return false
}
}
return true
}
if (Array.isArray(left) || Array.isArray(right)) {
if (!Array.isArray(left) || !Array.isArray(right) || left.length !== right.length) {
return false
}
return left.every((value, index) => persistedUIValuesEqual(value, right[index]))
}
if (left === null || right === null || typeof left !== 'object' || typeof right !== 'object') {
return false
}
const leftRecord = left as Record<string, unknown>
const rightRecord = right as Record<string, unknown>
const leftKeys = Object.keys(leftRecord)
const rightKeys = Object.keys(rightRecord)
if (leftKeys.length !== rightKeys.length) {
return false
}
for (const key of leftKeys) {
if (
!Object.prototype.hasOwnProperty.call(rightRecord, key) ||
!persistedUIValuesEqual(leftRecord[key], rightRecord[key])
) {
return false
}
}
return true
}