Skip to content

Instantly share code, notes, and snippets.


epelz/user_helpers.ts Secret

Last active May 27, 2016
What would you like to do?
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