Skip to content

Commit

Permalink
add option to get label text from each preset
Browse files Browse the repository at this point in the history
  • Loading branch information
kiaking committed Dec 25, 2023
1 parent 2d91f8d commit e5a4159
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions lib/support/DateRange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,16 @@ export class DateRange {
getTo(): Day | null {
return this.preset.getTo()
}

getLabeltext(): string {
return this.preset.getLabeltext()
}
}

export abstract class DateRangePresetBase {
abstract getFrom(): Day | null
abstract getTo(): Day | null
abstract getLabeltext(): string

protected getYear(year: number | null): Day | null {
if (!year) {
Expand Down Expand Up @@ -84,6 +89,13 @@ export class DateFromTo extends DateRangePresetBase {
return this.to
}

getLabeltext(): string {
const f = this.getFrom()?.format('YYYY-MM-DD')
const t = this.getTo()?.format('YYYY-MM-DD')

return (f && t) ? `${f}${t}` : 'Error'
}

setFrom(from: Day | null): this {
this.from = from
return this
Expand Down Expand Up @@ -112,6 +124,10 @@ export class Year extends DateRangePresetBase {
return this.getYear(this.year)?.endOf('year') ?? null
}

getLabeltext(): string {
return this.getYear(this.year)?.format('YYYY') ?? 'Error'
}

setYear(year: number | null): this {
this.year = year
return this
Expand Down Expand Up @@ -146,6 +162,12 @@ export class YearHalf extends DateRangePresetBase {
.endOf('month') ?? null
}

getLabeltext(): string {
const y = this.getYear(this.year)?.format('YYYY')

return y ? `${y}H${this.half}` : 'Error'
}

setYear(year: number | null): this {
this.year = year
return this
Expand Down Expand Up @@ -187,6 +209,12 @@ export class YearQuarter extends DateRangePresetBase {
.endOf('month') ?? null
}

getLabeltext(): string {
const y = this.getYear(this.year)?.format('YYYY')

return y ? `${y}Q${this.quarter}` : 'Error'
}

setYear(year: number | null): this {
this.year = year
return this
Expand Down

0 comments on commit e5a4159

Please sign in to comment.