Last active
October 21, 2020 03:56
-
-
Save talesmgodois/5e9a36475600bd8660181664005d9403 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
class WeekDays { | |
private static map: Map<number, WeekDays> = new Map(); | |
public static readonly SUNDAY = WeekDays.build(0, 'SUNDAY'); | |
public static readonly MONDAY = WeekDays.build(1, 'MONDAY'); | |
public static readonly TUESDAY = WeekDays.build(2, 'TUESDAY'); | |
public static readonly WEDNESDAY = WeekDays.build(3, 'WEDNESDAY'); | |
public static readonly THURSDAY = WeekDays.build(4, 'THURSDAY'); | |
public static readonly FRIDAY = WeekDays.build(5, 'FRIDAY'); | |
public static readonly SATURDAY = WeekDays.build(6, 'SATURDAY'); | |
private constructor ( | |
public readonly code: number, | |
public readonly key: string | |
) { } | |
private weekends(): Array<WeekDays> { | |
return [WeekDays.SATURDAY, WeekDays.SUNDAY] | |
} | |
public equals(weekDay: WeekDays) { | |
return this.code === weekDay.code; | |
} | |
public isWeekday(): boolean { | |
return !this.isWeekend(); | |
} | |
public isWeekend(): boolean { | |
return this.weekends().some((weekend: WeekDays) => weekend.equals(this)); | |
} | |
public static build(code: number, key: string) { | |
const enumerator = this.map.get(code); | |
if (enumerator) { | |
throw new Error('Code is not available'); | |
} else { | |
const weekDay = new WeekDays(code, key); | |
this.map.set(code, weekDay); | |
return weekDay; | |
} | |
} | |
} |
This file contains hidden or 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
class WeekDays { | |
private static map: Map<number, WeekDays> = new Map(); | |
public static readonly SUNDAY = WeekDays.build(0, 'SUNDAY'); | |
public static readonly MONDAY = WeekDays.build(1, 'MONDAY'); | |
public static readonly TUESDAY = WeekDays.build(2, 'TUESDAY'); | |
public static readonly WEDNESDAY = WeekDays.build(3, 'WEDNESDAY'); | |
public static readonly THURSDAY = WeekDays.build(4, 'THURSDAY'); | |
public static readonly FRIDAY = WeekDays.build(5, 'FRIDAY'); | |
public static readonly SATURDAY = WeekDays.build(6, 'SATURDAY'); | |
private constructor ( | |
public readonly code: number, | |
public readonly key: string | |
) { } | |
private weekends(): Array<WeekDays> { | |
return [WeekDays.SATURDAY, WeekDays.SUNDAY] | |
} | |
public equals(weekDay: WeekDays) { | |
return this.code === weekDay.code; | |
} | |
public isWeekday(): boolean { | |
return !this.isWeekend(); | |
} | |
public isWeekend(): boolean { | |
return this.weekends().some((weekend: WeekDays) => weekend.equals(this)); | |
} | |
public static getEnumByCode(code: number) { | |
return this.map.get(code); | |
} | |
public static build(code: number, key: string) { | |
const enumerator = this.map.get(code); | |
if (enumerator) { | |
throw new Error('Code is not available'); | |
} else { | |
const weekDay = new WeekDays(code, key); | |
this.map.set(code, weekDay); | |
return weekDay; | |
} | |
} | |
} | |
// console.log(WeekDays.FRIDAY.isWeekend()) | |
// console.log(WeekDays.FRIDAY.isWeekday()) | |
console.log(WeekDays.FRIDAY.equals(WeekDays.FRIDAY)) | |
// console.log(WeekDays.FRIDAY.equals({code: 5, key: 'asdf'})) | |
console.log(WeekDays.getEnumByCode(0)) | |
console.log(WeekDays.getEnumByCode(6)) | |
console.log(WeekDays.getEnumByCode(10)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment