Skip to content

Commit

Permalink
cleaned up some jsdocs, a type fix, removed usage of instant, renamed…
Browse files Browse the repository at this point in the history
… toInstant to toTemporalInstant to match the spec
  • Loading branch information
zgavin committed May 2, 2024
1 parent 2b900c7 commit 3450954
Show file tree
Hide file tree
Showing 7 changed files with 10 additions and 15 deletions.
1 change: 1 addition & 0 deletions src/array/toObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export {}; // needed for TS to realize this file can be imported
declare global {
interface Array<T> {
toObject<K extends PropertyKey, V extends any>(this: [K, V][]): Record<K, V>;
toObject<K extends PropertyKey, V extends any>(this: (readonly [K, V])[]): Record<K, V>;
}

interface ReadonlyArray<T> {
Expand Down
11 changes: 5 additions & 6 deletions src/temporal/legacyDate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,19 @@ import { Temporal, toTemporalInstant } from "temporal-polyfill";

declare global {
interface Date {
toInstant(): Temporal.Instant;
// This method is part of the Temporal spec and will be added in native implementations automatically
toTemporalInstant(): Temporal.Instant;
toZonedDateTime(tzLike: Temporal.TimeZoneLike): Temporal.ZonedDateTime;
toPlainDate(tzLike: Temporal.TimeZoneLike): Temporal.PlainDate;
}
}

Date.prototype.toInstant = function () {
return toTemporalInstant.call(this);
};
Date.prototype.toTemporalInstant = toTemporalInstant;

Date.prototype.toZonedDateTime = function (tzLike: Temporal.TimeZoneLike) {
return toTemporalInstant.call(this).toZonedDateTimeISO(tzLike);
return this.toTemporalInstant().toZonedDateTimeISO(tzLike);
};

Date.prototype.toPlainDate = function (tzLike: Temporal.TimeZoneLike) {
return toTemporalInstant.call(this).toZonedDateTimeISO(tzLike).toPlainDate();
return this.toTemporalInstant().toZonedDateTimeISO(tzLike).toPlainDate();
};
2 changes: 0 additions & 2 deletions src/temporal/plainDate/endOfYear.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ declare module "temporal-polyfill" {
* Return the end of a year for the given date.
* The result will be in the local timezone.
*
* @typeParam DateType - The `Date` type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like [`UTCDate`](https://github.com/date-fns/utc).
*
* @param date - The original date
*
* @returns The end of a year
Expand Down
2 changes: 0 additions & 2 deletions src/temporal/plainDate/isWeekend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ declare module "temporal-polyfill" {
* @description
* Does the given date fall on a weekend?
*
* @typeParam DateType - The `Date` type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like [`UTCDate`](https://github.com/date-fns/utc).
*
* @param options - An object with options
*
* @returns The date falls on a weekend
Expand Down
5 changes: 3 additions & 2 deletions src/temporal/setupTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ export function newPD(date: string): Temporal.PlainDate {
return Temporal.PlainDate.from(date);
}

export function newZDT(datetime: string, tzLike: Temporal.TimeZoneLike = "UTC"): Temporal.ZonedDateTime {
return Temporal.Instant.from(datetime).toZonedDateTimeISO(tzLike);
export function newZDT(datetime: string): Temporal.ZonedDateTime {
if (datetime.endsWith("Z")) datetime = datetime.slice(0, -1).concat(`+00:00[UTC]`);
return Temporal.ZonedDateTime.from(datetime);
}

export function newPDInterval(start: string, end: string): Temporal.Interval<Temporal.PlainDate> {
Expand Down
2 changes: 0 additions & 2 deletions src/temporal/zonedDateTime/isWeekend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ declare module "temporal-polyfill" {
* @description
* Does the given date fall on a weekend?
*
* @typeParam DateType - The `Date` type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like [`UTCDate`](https://github.com/date-fns/utc).
*
* @param options - An object with options
*
* @returns The date falls on a weekend
Expand Down
2 changes: 1 addition & 1 deletion src/temporal/zonedDateTime/toPlainDate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import "./index";

describe("toPlainDate", () => {
it("generates a plain date from the zoned date using the UTC timezone", () => {
const zdt = newZDT("2024-05-02T00:00:00.000+0900", "Asia/Kolkata");
const zdt = newZDT("2024-05-02T00:00:00.000+09:00[Asia/Tokyo]");
const result = zdt.toPlainDateUTC();
expect(result).toEqual(newPD("2024-05-01"));
});
Expand Down

0 comments on commit 3450954

Please sign in to comment.