From c5f4b7b52659c7308b050a5f015a1a4bead211e0 Mon Sep 17 00:00:00 2001 From: Polianin Nikita Date: Mon, 10 Jun 2024 21:44:41 +0300 Subject: [PATCH] refactor: set readonly ticks --- src/shared/structs/DateOnly.ts | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/src/shared/structs/DateOnly.ts b/src/shared/structs/DateOnly.ts index 690a4b3..e574d23 100644 --- a/src/shared/structs/DateOnly.ts +++ b/src/shared/structs/DateOnly.ts @@ -1,42 +1,36 @@ export class DateOnly { - private ticks: number; - - /* - constructor(year: number, month: number, day: number) { - this.ticks = new Date(year, month - 1, day).getTime(); - } -*/ + private readonly _ticks: number; constructor(year: number, month: number, day: number); constructor(date: Date); constructor(date: string); constructor(yearOrDate: number | Date | string, month?: number, day?: number) { if (yearOrDate instanceof Date) { - this.ticks = yearOrDate.getTime(); + this._ticks = yearOrDate.getTime(); } else if (typeof yearOrDate === 'number' && month !== undefined && day !== undefined) { - this.ticks = new Date(yearOrDate, month - 1, day).getTime(); + this._ticks = new Date(yearOrDate, month - 1, day).getTime(); } else if (typeof yearOrDate === 'string') { const [year, month, day] = yearOrDate.split('-').map(Number); - this.ticks = new Date(year, month - 1, day).getTime(); + this._ticks = new Date(year, month - 1, day).getTime(); } else { throw new Error('Invalid constructor arguments'); } } get year(): number { - return new Date(this.ticks).getFullYear(); + return new Date(this._ticks).getFullYear(); } get month(): number { - return new Date(this.ticks).getMonth() + 1; + return new Date(this._ticks).getMonth() + 1; } get day(): number { - return new Date(this.ticks).getDate(); + return new Date(this._ticks).getDate(); } get date(): Date { - return new Date(this.ticks); + return new Date(this._ticks); } toString(): string {