Skip to content

Instantly share code, notes, and snippets.

@Gummiees
Created October 29, 2020 09:48
Show Gist options
  • Save Gummiees/223d8e59cd31e2e7c098892434a00d2d to your computer and use it in GitHub Desktop.
Save Gummiees/223d8e59cd31e2e7c098892434a00d2d to your computer and use it in GitHub Desktop.
Compares two same type objects to check if their properties and values are equals
/**
* Compares two same type objects to check if their properties and values are equals.
*
* Attention: It is not recursively.
* @param original Original element to have as a base.
* @param compareTo element to compare to with the original one.
* @returns The result from comparing both.
*/
equals<T>(original: T, compareTo: T): boolean {
if (!original && !compareTo) {
return true;
} else if (!original || !compareTo) {
return false;
}
const keysOriginal: any[] = Object.keys(original);
const keysCompareTo: any[] = Object.keys(compareTo);
keysOriginal.forEach((keyOriginal, i) => {
if (keyOriginal !== keysCompareTo[i] || original[keyOriginal] !== compareTo[keyOriginal]) {
return false;
}
});
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment