Skip to content

Instantly share code, notes, and snippets.

@epelz
Last active May 27, 2016 10:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save epelz/c00e020fbaca0d5d99dc to your computer and use it in GitHub Desktop.
Save epelz/c00e020fbaca0d5d99dc to your computer and use it in GitHub Desktop.
Example: Data model helper
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