Created
October 29, 2020 09:48
-
-
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
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
/** | |
* 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