Skip to content

Commit 535f724

Browse files
committed
Add isAfter and isBefore function
1 parent 984eb61 commit 535f724

File tree

4 files changed

+214
-0
lines changed

4 files changed

+214
-0
lines changed

src/datetime/isAfter.test.ts

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { Temporal } from "@js-temporal/polyfill";
2+
import { expect, test } from "vitest";
3+
4+
import { isAfter } from "./isAfter.js";
5+
6+
test("isAfter() with Instant", () => {
7+
const a = Temporal.Instant.fromEpochSeconds(1720000000);
8+
const b = Temporal.Instant.fromEpochSeconds(1700000000);
9+
expect(isAfter(a, b)).toBe(true);
10+
expect(isAfter(b, a)).toBe(false);
11+
expect(isAfter(a, a)).toBe(false);
12+
});
13+
test("isAfter() with ZonedDateTime", () => {
14+
const a = Temporal.ZonedDateTime.from(
15+
"2024-01-01T00:00:00-05:00[America/Toronto]",
16+
);
17+
const b = Temporal.ZonedDateTime.from(
18+
"2024-01-01T03:00:00+01:00[Europe/Paris]",
19+
);
20+
expect(isAfter(a, b)).toBe(true);
21+
expect(isAfter(b, a)).toBe(false);
22+
expect(isAfter(a, a)).toBe(false);
23+
});
24+
test("isAfter() with PlainDate", () => {
25+
const a = Temporal.PlainDate.from("2024-01-02");
26+
const b = Temporal.PlainDate.from("2024-01-01[u-ca=hebrew]");
27+
expect(isAfter(a, b)).toBe(true);
28+
expect(isAfter(b, a)).toBe(false);
29+
expect(isAfter(a, a)).toBe(false);
30+
});
31+
test("isAfter() with PlainTime", () => {
32+
const a = Temporal.PlainTime.from("23:45:00");
33+
const b = Temporal.PlainTime.from("06:00:00");
34+
expect(isAfter(a, b)).toBe(true);
35+
expect(isAfter(b, a)).toBe(false);
36+
expect(isAfter(a, a)).toBe(false);
37+
});
38+
test("isAfter() with PlainDateTime", () => {
39+
const a = Temporal.PlainDateTime.from(
40+
"2024-01-01T09:00:00+09:00[Asia/Tokyo]",
41+
);
42+
const b = Temporal.PlainDateTime.from(
43+
"2024-01-01T03:00:00+01:00[Europe/Paris]",
44+
);
45+
expect(isAfter(a, b)).toBe(true);
46+
expect(isAfter(b, a)).toBe(false);
47+
expect(isAfter(a, a)).toBe(false);
48+
});
49+
test("isAfter() with PlainYearMonth", () => {
50+
const a = Temporal.PlainYearMonth.from("2024-01");
51+
const b = Temporal.PlainYearMonth.from("2023-12");
52+
expect(isAfter(a, b)).toBe(true);
53+
expect(isAfter(b, a)).toBe(false);
54+
expect(isAfter(a, a)).toBe(false);
55+
});
56+
test("isAfter() with PlainMonthDay", () => {
57+
expect(() => {
58+
isAfter(
59+
// @ts-expect-error
60+
Temporal.PlainMonthDay.from("12-03"),
61+
Temporal.PlainMonthDay.from("12-04"),
62+
);
63+
}).toThrow();
64+
});

src/datetime/isAfter.ts

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import type { ComparableDateTimeType, Temporal } from "../types.js";
2+
import { compare } from "./_compare.js";
3+
4+
/**
5+
* Checks whether the first datetime is after the second one.
6+
* @param dateTime datetime object
7+
* @param dateTimeToCompare datetime object to compare with
8+
* @returns whether the first datetime is after the second one
9+
*/
10+
export function isAfter(
11+
dateTime: Temporal.Instant,
12+
dateTimeToCompare: Temporal.Instant,
13+
): boolean;
14+
export function isAfter(
15+
dateTime: Temporal.ZonedDateTime,
16+
dateTimeToCompare: Temporal.ZonedDateTime,
17+
): boolean;
18+
export function isAfter(
19+
dateTime: Temporal.PlainDate,
20+
dateTimeToCompare: Temporal.PlainDate,
21+
): boolean;
22+
export function isAfter(
23+
dateTime: Temporal.PlainTime,
24+
dateTimeToCompare: Temporal.PlainTime,
25+
): boolean;
26+
export function isAfter(
27+
dateTime: Temporal.PlainDateTime,
28+
dateTimeToCompare: Temporal.PlainDateTime,
29+
): boolean;
30+
export function isAfter(
31+
dateTime: Temporal.PlainYearMonth,
32+
dateTimeToCompare: Temporal.PlainYearMonth,
33+
): boolean;
34+
export function isAfter(
35+
dateTime: ComparableDateTimeType,
36+
dateTimeToCompare: ComparableDateTimeType,
37+
): boolean;
38+
export function isAfter(
39+
dateTime: ComparableDateTimeType,
40+
dateTimeToCompare: ComparableDateTimeType,
41+
) {
42+
return compare(dateTime, dateTimeToCompare) === 1;
43+
}

src/datetime/isBefore.test.ts

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { Temporal } from "@js-temporal/polyfill";
2+
import { expect, test } from "vitest";
3+
4+
import { isBefore } from "./isBefore.js";
5+
6+
test("isBefore() with Instant", () => {
7+
const a = Temporal.Instant.fromEpochSeconds(1720000000);
8+
const b = Temporal.Instant.fromEpochSeconds(1700000000);
9+
expect(isBefore(a, b)).toBe(false);
10+
expect(isBefore(b, a)).toBe(true);
11+
expect(isBefore(a, a)).toBe(false);
12+
});
13+
test("isBefore() with ZonedDateTime", () => {
14+
const a = Temporal.ZonedDateTime.from(
15+
"2024-01-01T00:00:00-05:00[America/Toronto]",
16+
);
17+
const b = Temporal.ZonedDateTime.from(
18+
"2024-01-01T03:00:00+01:00[Europe/Paris]",
19+
);
20+
expect(isBefore(a, b)).toBe(false);
21+
expect(isBefore(b, a)).toBe(true);
22+
expect(isBefore(a, a)).toBe(false);
23+
});
24+
test("isBefore() with PlainDate", () => {
25+
const a = Temporal.PlainDate.from("2024-01-02");
26+
const b = Temporal.PlainDate.from("2024-01-01[u-ca=hebrew]");
27+
expect(isBefore(a, b)).toBe(false);
28+
expect(isBefore(b, a)).toBe(true);
29+
expect(isBefore(a, a)).toBe(false);
30+
});
31+
test("isBefore() with PlainTime", () => {
32+
const a = Temporal.PlainTime.from("23:45:00");
33+
const b = Temporal.PlainTime.from("06:00:00");
34+
expect(isBefore(a, b)).toBe(false);
35+
expect(isBefore(b, a)).toBe(true);
36+
expect(isBefore(a, a)).toBe(false);
37+
});
38+
test("isBefore() with PlainDateTime", () => {
39+
const a = Temporal.PlainDateTime.from(
40+
"2024-01-01T09:00:00+09:00[Asia/Tokyo]",
41+
);
42+
const b = Temporal.PlainDateTime.from(
43+
"2024-01-01T03:00:00+01:00[Europe/Paris]",
44+
);
45+
expect(isBefore(a, b)).toBe(false);
46+
expect(isBefore(b, a)).toBe(true);
47+
expect(isBefore(a, a)).toBe(false);
48+
});
49+
test("isBefore() with PlainYearMonth", () => {
50+
const a = Temporal.PlainYearMonth.from("2024-01");
51+
const b = Temporal.PlainYearMonth.from("2023-12");
52+
expect(isBefore(a, b)).toBe(false);
53+
expect(isBefore(b, a)).toBe(true);
54+
expect(isBefore(a, a)).toBe(false);
55+
});
56+
test("isBefore() with PlainMonthDay", () => {
57+
expect(() => {
58+
isBefore(
59+
// @ts-expect-error
60+
Temporal.PlainMonthDay.from("12-03"),
61+
Temporal.PlainMonthDay.from("12-04"),
62+
);
63+
}).toThrow();
64+
});

src/datetime/isBefore.ts

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import type { ComparableDateTimeType, Temporal } from "../types.js";
2+
import { compare } from "./_compare.js";
3+
4+
/**
5+
* Checks whether the first datetime is before the second one.
6+
* @param dateTime datetime object
7+
* @param dateTimeToCompare datetime object to compare with
8+
* @returns whether the first datetime is before the second one
9+
*/
10+
export function isBefore(
11+
dateTime: Temporal.Instant,
12+
dateTimeToCompare: Temporal.Instant,
13+
): boolean;
14+
export function isBefore(
15+
dateTime: Temporal.ZonedDateTime,
16+
dateTimeToCompare: Temporal.ZonedDateTime,
17+
): boolean;
18+
export function isBefore(
19+
dateTime: Temporal.PlainDate,
20+
dateTimeToCompare: Temporal.PlainDate,
21+
): boolean;
22+
export function isBefore(
23+
dateTime: Temporal.PlainTime,
24+
dateTimeToCompare: Temporal.PlainTime,
25+
): boolean;
26+
export function isBefore(
27+
dateTime: Temporal.PlainDateTime,
28+
dateTimeToCompare: Temporal.PlainDateTime,
29+
): boolean;
30+
export function isBefore(
31+
dateTime: Temporal.PlainYearMonth,
32+
dateTimeToCompare: Temporal.PlainYearMonth,
33+
): boolean;
34+
export function isBefore(
35+
dateTime: ComparableDateTimeType,
36+
dateTimeToCompare: ComparableDateTimeType,
37+
): boolean;
38+
export function isBefore(
39+
dateTime: ComparableDateTimeType,
40+
dateTimeToCompare: ComparableDateTimeType,
41+
) {
42+
return compare(dateTime, dateTimeToCompare) === -1;
43+
}

0 commit comments

Comments
 (0)