44 lines
1.2 KiB
TypeScript
44 lines
1.2 KiB
TypeScript
import type { ValueObject } from '@core/shared/domain/ValueObject';
|
|
import { RacingDomainValidationError } from '../errors/RacingDomainError';
|
|
import type { Weekday } from '../types/Weekday';
|
|
import { weekdayToIndex } from '../types/Weekday';
|
|
|
|
export interface WeekdaySetProps {
|
|
days: Weekday[];
|
|
}
|
|
|
|
export class WeekdaySet implements ValueObject<WeekdaySetProps> {
|
|
private readonly days: Weekday[];
|
|
|
|
static fromArray(days: Weekday[]): WeekdaySet {
|
|
return new WeekdaySet(days);
|
|
}
|
|
|
|
constructor(days: Weekday[]) {
|
|
if (!Array.isArray(days) || days.length === 0) {
|
|
throw new RacingDomainValidationError('WeekdaySet requires at least one weekday');
|
|
}
|
|
|
|
const unique = Array.from(new Set(days));
|
|
this.days = unique.sort((a, b) => weekdayToIndex(a) - weekdayToIndex(b));
|
|
}
|
|
|
|
get props(): WeekdaySetProps {
|
|
return { days: [...this.days] };
|
|
}
|
|
|
|
getAll(): Weekday[] {
|
|
return [...this.days];
|
|
}
|
|
|
|
includes(day: Weekday): boolean {
|
|
return this.days.includes(day);
|
|
}
|
|
|
|
equals(other: ValueObject<WeekdaySetProps>): boolean {
|
|
const a = this.props.days;
|
|
const b = other.props.days;
|
|
if (a.length !== b.length) return false;
|
|
return a.every((day, index) => day === b[index]);
|
|
}
|
|
} |