-
-
Save epelz/c00e020fbaca0d5d99dc to your computer and use it in GitHub Desktop.
Example: Data model helper
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
export function isOnVacation(attr: { | |
// We pass in the current time, because otherwise this function would rely | |
// on the Date global. Instead, the calling component will use a chronometer | |
// service which is declaratively passed-in to it. | |
nowTime: number; | |
// We take advantage of TypeScript's structural typing, so this method can | |
// take any compatible User model. This is preferable to relying on a specific | |
// User interface: each calling component has a subset of the User object graph | |
// that it depends on, so this makes isOnVacation usable in many components. | |
user: { | |
vacationStartTime(): number; | |
vacationEndTime(): number | |
}; | |
}): boolean { | |
var startTime = attr.user.vacationStartTime(); | |
var endTime = attr.user.vacationEndTime(); | |
var wasOnVacation = startTime !== 0 && startTime <= attr.nowTime; | |
var stillOnVacation = endTime === 0 || endTime >= attr.nowTime; | |
return wasOnVacation && stillOnVacation; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment