Last active
May 28, 2025 14:55
-
-
Save kirpalmakanga/a92bb22091aa59433fcbce11e11653a0 to your computer and use it in GitHub Desktop.
isEqual
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function isEqual(a: unknown, b: unknown): boolean { | |
if (a === b) return true; | |
if (a == null || b == null) return a === b; | |
if (typeof a !== typeof b) return false; | |
if (Array.isArray(a) && Array.isArray(b)) { | |
if (a.length !== b.length) return false; | |
return a.every((item, index) => isEqual(item, b[index])); | |
} | |
if (typeof a === 'object' && typeof b === 'object') { | |
const keysA = Object.keys(a) as (keyof typeof a)[]; | |
const keysB = Object.keys(b) as (keyof typeof b)[]; | |
if (keysA.length !== keysB.length) return false; | |
return keysA.every((key) => key in b && isEqual(a[key], b[key])); | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment