Last active
May 29, 2021 10:41
-
-
Save themgoncalves/8dfc8c987c74365a0d87ff9f421d606b to your computer and use it in GitHub Desktop.
This file contains 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
const deepCompare = (source: unknown | unknown[], target: unknown | unknown[]): boolean => { | |
if (typeOf(source) !== typeOf(target)) { | |
return false; | |
} | |
if (typeOf(source) === 'array') { | |
if ((source as unknown[]).length !== (target as unknown[]).length) { | |
return false; | |
} | |
return (source as unknown[]).every((entry, index) => deepCompare(entry, (target as unknown[])[index])) | |
} else if (typeOf(source) === 'object') { | |
if (Object.keys(source).length !== Object.keys(target).length) { | |
return false; | |
} | |
return Object.keys(source).every((key) => deepCompare((source as Record<string, unknown>)[key], (target as Record<string, unknown>)[key])); | |
} else if (typeOf(source) === 'date') { | |
return source.getTime() === target.getTime(); | |
} | |
return source === target; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment