Skip to content

Instantly share code, notes, and snippets.

@themgoncalves
Last active May 29, 2021 10:41
Show Gist options
  • Save themgoncalves/8dfc8c987c74365a0d87ff9f421d606b to your computer and use it in GitHub Desktop.
Save themgoncalves/8dfc8c987c74365a0d87ff9f421d606b to your computer and use it in GitHub Desktop.
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