Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
khoidt committed Nov 18, 2024
1 parent 6df6bb1 commit bda620d
Showing 1 changed file with 32 additions and 11 deletions.
43 changes: 32 additions & 11 deletions src/fragmentarium/domain/archaeology.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,41 @@ export class PartialDate {
}

toLocaleString(locale = 'default'): string {
const options: Intl.DateTimeFormatOptions = this.day
? { year: 'numeric', month: '2-digit', day: '2-digit' }
: this.month
? { year: 'numeric', month: '2-digit' }
: { year: 'numeric' }

if (this.year >= 0) {
const date = new Date(this.year, (this.month || 1) - 1, this.day || 1)
return new Intl.DateTimeFormat(locale, options).format(date)
} else {
return `${Math.abs(this.year)} BCE`
if (this.isBCE()) {
return this.formatBCE()
}
return this.formatCE(locale)
}

private isBCE(): boolean {
return this.year < 0
}

private formatBCE(): string {
return `${Math.abs(this.year)} BCE`
}

private formatCE(locale: string): string {
const options = this.getDateFormatOptions()
const date = this.createDate()
return new Intl.DateTimeFormat(locale, options).format(date)
}

private getDateFormatOptions(): Intl.DateTimeFormatOptions {
if (this.day) {
return { year: 'numeric', month: '2-digit', day: '2-digit' }
}
if (this.month) {
return { year: 'numeric', month: '2-digit' }
}
return { year: 'numeric' }
}

private createDate(): Date {
return new Date(this.year, (this.month || 1) - 1, this.day || 1)
}
}

export type DateRange = {
start: PartialDate
end?: PartialDate | null
Expand Down

0 comments on commit bda620d

Please sign in to comment.