refactor
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
import { RacingDomainValidationError } from '../errors/RacingDomainError';
|
||||
import { ALL_WEEKDAYS } from '../types/Weekday';
|
||||
import type { Weekday } from '../types/Weekday';
|
||||
import type { IValueObject } from '@core/shared/domain';
|
||||
|
||||
@@ -9,20 +11,43 @@ export interface MonthlyRecurrencePatternProps {
|
||||
export class MonthlyRecurrencePattern implements IValueObject<MonthlyRecurrencePatternProps> {
|
||||
readonly ordinal: 1 | 2 | 3 | 4;
|
||||
readonly weekday: Weekday;
|
||||
|
||||
constructor(ordinal: 1 | 2 | 3 | 4, weekday: Weekday);
|
||||
constructor(props: MonthlyRecurrencePatternProps);
|
||||
constructor(
|
||||
ordinalOrProps: 1 | 2 | 3 | 4 | MonthlyRecurrencePatternProps,
|
||||
weekday?: Weekday,
|
||||
) {
|
||||
if (typeof ordinalOrProps === 'object') {
|
||||
this.ordinal = ordinalOrProps.ordinal;
|
||||
this.weekday = ordinalOrProps.weekday;
|
||||
} else {
|
||||
this.ordinal = ordinalOrProps;
|
||||
this.weekday = weekday as Weekday;
|
||||
|
||||
private constructor(ordinal: 1 | 2 | 3 | 4, weekday: Weekday) {
|
||||
this.ordinal = ordinal;
|
||||
this.weekday = weekday;
|
||||
}
|
||||
|
||||
static create(ordinal: 1 | 2 | 3 | 4, weekday: Weekday): MonthlyRecurrencePattern {
|
||||
if (!ordinal || ordinal < 1 || ordinal > 4) {
|
||||
throw new RacingDomainValidationError('MonthlyRecurrencePattern ordinal must be between 1 and 4');
|
||||
}
|
||||
if (!weekday || !ALL_WEEKDAYS.includes(weekday)) {
|
||||
throw new RacingDomainValidationError('MonthlyRecurrencePattern weekday must be a valid weekday');
|
||||
}
|
||||
return new MonthlyRecurrencePattern(ordinal, weekday);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the ordinal suffix (1st, 2nd, 3rd, 4th)
|
||||
*/
|
||||
getOrdinalSuffix(): string {
|
||||
switch (this.ordinal) {
|
||||
case 1:
|
||||
return '1st';
|
||||
case 2:
|
||||
return '2nd';
|
||||
case 3:
|
||||
return '3rd';
|
||||
case 4:
|
||||
return '4th';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get description of the pattern
|
||||
*/
|
||||
getDescription(): string {
|
||||
return `${this.getOrdinalSuffix()} ${this.weekday} of the month`;
|
||||
}
|
||||
|
||||
get props(): MonthlyRecurrencePatternProps {
|
||||
|
||||
Reference in New Issue
Block a user