Skip to content

Commit

Permalink
fix: correcting types
Browse files Browse the repository at this point in the history
  • Loading branch information
ValeraS committed May 25, 2024
1 parent 9f7aea8 commit 5b2a73b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/dateTime/__tests__/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ test('toISOString', () => {

//invalid dates
date = dateTimeUtc({input: '2017-12-32k'});
expect(date.toISOString()).toBe(null); // 'An invalid date to iso string is null'
expect(() => date.toISOString()).toThrow(); // 'An invalid date to iso string is null'
});

test('toISOString without UTC conversion', () => {
Expand All @@ -143,7 +143,7 @@ test('toISOString without UTC conversion', () => {

//invalid dates
date = dateTimeUtc({input: '2017-12-32k'}).utcOffset('+05:30');
expect(date.toISOString(true)).toBe(null); // 'An invalid date to iso string is null'
expect(() => date.toISOString(true)).toThrow(); // 'An invalid date to iso string is null'
});

test('long years', () => {
Expand Down
28 changes: 17 additions & 11 deletions src/dateTime/dateTime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,7 @@ class DateTimeImpl implements DateTime {
}

toISOString(keepOffset?: boolean): string {
if (!this.isValid()) {
// @ts-expect-error it's not a correct value, but moment returns it with the same typings, luxon also returns null, dayjs throws an error.
return null;
}
// invalid date throws an error
if (keepOffset) {
return new Date(this.valueOf() + this.utcOffset() * 60 * 1000)
.toISOString()
Expand Down Expand Up @@ -139,6 +136,10 @@ class DateTimeImpl implements DateTime {
return this._timeZone === 'system' ? guessUserTimeZone() : this._timeZone;
}

if (!this.isValid()) {
return this;
}

const zone = normalizeTimeZone(timeZone, settings.getDefaultTimeZone());
let ts = this.valueOf();
let offset = timeZoneOffset(zone, ts);
Expand Down Expand Up @@ -276,7 +277,7 @@ class DateTimeImpl implements DateTime {
}

valueOf(): number {
return this._timestamp;
return this.isValid() ? this._timestamp : NaN;
}

isSame(input?: DateTimeInput, granularity?: DurationUnit): boolean {
Expand Down Expand Up @@ -378,7 +379,7 @@ class DateTimeImpl implements DateTime {
}
from(formaInput: DateTimeInput, withoutSuffix?: boolean): string {
if (!this.isValid()) {
return INVALID_DATE_STRING;
return this._localeData.invalidDate || INVALID_DATE_STRING;
}
return fromTo(this, formaInput, this._localeData.relativeTime, withoutSuffix, true);
}
Expand All @@ -388,6 +389,9 @@ class DateTimeImpl implements DateTime {
if (!locale) {
return this._locale;
}
if (!this.isValid()) {
return this;
}
return createDateTime({
ts: this.valueOf(),
timeZone: this._timeZone,
Expand All @@ -399,13 +403,13 @@ class DateTimeImpl implements DateTime {
return new Date(this.valueOf());
}
unix(): number {
return Math.floor(this.valueOf() / 1000);
return this.isValid() ? Math.floor(this.valueOf() / 1000) : NaN;
}
utc(keepLocalTime?: boolean | undefined): DateTime {
return this.timeZone(UtcTimeZone, keepLocalTime);
}
daysInMonth(): number {
return daysInMonth(this._c.year, this._c.month);
return this.isValid() ? daysInMonth(this._c.year, this._c.month) : NaN;
}

// eslint-disable-next-line complexity
Expand Down Expand Up @@ -653,11 +657,13 @@ class DateTimeImpl implements DateTime {
}

toString(): string {
return this.isValid() ? this.toDate().toUTCString() : INVALID_DATE_STRING;
return this.isValid()
? this.toDate().toUTCString()
: this._localeData.invalidDate || INVALID_DATE_STRING;
}

toJSON(): string {
return this.toISOString();
toJSON(): string | null {
return this.isValid() ? this.toISOString() : null;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/typings/dateTime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export interface DateTime {
endOf(unitOfTime: StartOfUnit): DateTime;
toDate(): Date;
toISOString(keepOffset?: boolean): string;
toJSON(): string;
toJSON(): string | null;
valueOf(): number;
unix(): number;
utc(keepLocalTime?: boolean): DateTime;
Expand Down

0 comments on commit 5b2a73b

Please sign in to comment.