I'm currently working on a typescript-class with this method:
/**
* Gets the state of a package. The state is determined based on two timestamps stored as fields in the {@link
* Package}. As the current time equals or passes a timestamp, the state changes.
*
* @param currentTime The time to compare the timestamps with
* @param sourcePackage The package containing the two timestamps to compare the currentTime with
* @returns {CardState} The calculated state of the package
*/
static determineStateOfPackage(currentTime: Date, sourcePackage: Package): CardState {
if (sourcePackage.targetTime.getTime() <= currentTime.getTime()) {
return CardState.URGENT;
}
if (sourcePackage.warningTime.getTime() <= currentTime.getTime()) {
return CardState.WARNING;
}
return CardState.FRIENDLY;
}
It bugs me that the two ifs are repetitive but I can't really figure out how to improve this. How can I improve this code especially to avoid calling the two ifs with nearly the same content twice?