Skip to content

Instantly share code, notes, and snippets.

@themgoncalves
Last active July 22, 2022 20:55
Show Gist options
  • Save themgoncalves/c2e58d2bff0960e094a00940ead74c5f to your computer and use it in GitHub Desktop.
Save themgoncalves/c2e58d2bff0960e094a00940ead74c5f to your computer and use it in GitHub Desktop.
const deepCompare = (source, target) => {
if (typeOf(source) !== typeOf(target)) {
return false;
}
if (typeOf(source) === 'array') {
if (source.length !== target.length) {
return false;
}
return source.every((entry, index) => deepCompare(entry, target[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[key], target[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