@klinking/colander 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/formats/date.cjs +49 -0
- package/dist/formats/date.d.cts +45 -0
- package/dist/formats/date.d.mts +45 -0
- package/dist/formats/date.mjs +2 -0
- package/dist/formats/object.cjs +49 -0
- package/dist/formats/object.d.cts +45 -0
- package/dist/formats/object.d.mts +45 -0
- package/dist/formats/object.mjs +2 -0
- package/dist/formats/plain-date-time.cjs +49 -0
- package/dist/formats/plain-date-time.d.cts +45 -0
- package/dist/formats/plain-date-time.d.mts +45 -0
- package/dist/formats/plain-date-time.mjs +2 -0
- package/dist/formats/plain-date.cjs +49 -0
- package/dist/formats/plain-date.d.cts +45 -0
- package/dist/formats/plain-date.d.mts +45 -0
- package/dist/formats/plain-date.mjs +2 -0
- package/dist/formats/plain-month-day.cjs +49 -0
- package/dist/formats/plain-month-day.d.cts +45 -0
- package/dist/formats/plain-month-day.d.mts +45 -0
- package/dist/formats/plain-month-day.mjs +2 -0
- package/dist/formats/plain-year-month.cjs +49 -0
- package/dist/formats/plain-year-month.d.cts +45 -0
- package/dist/formats/plain-year-month.d.mts +45 -0
- package/dist/formats/plain-year-month.mjs +2 -0
- package/dist/formats/zoned-date-time.cjs +49 -0
- package/dist/formats/zoned-date-time.d.cts +45 -0
- package/dist/formats/zoned-date-time.d.mts +45 -0
- package/dist/formats/zoned-date-time.mjs +2 -0
- package/dist/index.cjs +49 -0
- package/dist/index.d.cts +2 -0
- package/dist/index.d.mts +2 -0
- package/dist/index.mjs +2 -0
- package/dist/temporal-polyfill-DTx0VIX5.mjs +4556 -0
- package/dist/temporal-polyfill-DU8VfleL.cjs +4860 -0
- package/dist/weeks-view-context-BN6qR-83.d.mts +2022 -0
- package/dist/weeks-view-context-aHGgYkpt.d.cts +2022 -0
- package/package.json +89 -0
- package/src/calendar-context.ts +22 -0
- package/src/calendar-provider.test.tsx +123 -0
- package/src/calendar-provider.tsx +538 -0
- package/src/calendar-types.ts +212 -0
- package/src/compute-weeks-in-window.test.ts +83 -0
- package/src/compute-weeks-in-window.ts +40 -0
- package/src/context.ts +34 -0
- package/src/day-cell.tsx +635 -0
- package/src/drag-handle.tsx +137 -0
- package/src/formats/date.ts +110 -0
- package/src/formats/formats.test.ts +18 -0
- package/src/formats/object.ts +110 -0
- package/src/formats/plain-date-time.ts +110 -0
- package/src/formats/plain-date.ts +110 -0
- package/src/formats/plain-month-day.ts +110 -0
- package/src/formats/plain-year-month.ts +110 -0
- package/src/formats/zoned-date-time.ts +110 -0
- package/src/grid-header.tsx +141 -0
- package/src/grid.event-perf.test.tsx +282 -0
- package/src/grid.perf.test.tsx +467 -0
- package/src/grid.tsx +608 -0
- package/src/index.ts +116 -0
- package/src/keyboard.test.ts +498 -0
- package/src/keyboard.ts +142 -0
- package/src/month-separator.test.tsx +183 -0
- package/src/month-separator.tsx +238 -0
- package/src/month-view-context.ts +26 -0
- package/src/month-view-types.ts +89 -0
- package/src/month-view.test.tsx +1433 -0
- package/src/month-view.tsx +373 -0
- package/src/navigation.tsx +350 -0
- package/src/overflow.test.ts +124 -0
- package/src/overflow.ts +233 -0
- package/src/range-preview.tsx +90 -0
- package/src/resolve-first-week.test.ts +132 -0
- package/src/resolve-first-week.ts +145 -0
- package/src/root-selection.ts +621 -0
- package/src/selected-range.tsx +163 -0
- package/src/temporal-polyfill.test.ts +317 -0
- package/src/temporal-polyfill.ts +566 -0
- package/src/test-temporal.ts +17 -0
- package/src/types.ts +814 -0
- package/src/utils.bench.ts +183 -0
- package/src/utils.test.ts +983 -0
- package/src/utils.ts +493 -0
- package/src/view-context.ts +28 -0
- package/src/week-number.tsx +118 -0
- package/src/weeks-grid.tsx +265 -0
- package/src/weeks-keyboard.test.ts +222 -0
- package/src/weeks-keyboard.ts +156 -0
- package/src/weeks-navigation.tsx +209 -0
- package/src/weeks-view-context.ts +26 -0
- package/src/weeks-view-state.ts +421 -0
- package/src/weeks-view-types.ts +102 -0
- package/src/weeks-view.test.tsx +479 -0
- package/src/weeks-view.tsx +127 -0
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import { mergeProps } from "@base-ui/react/merge-props";
|
|
2
|
+
import { useRender } from "@base-ui/react/use-render";
|
|
3
|
+
import type { Temporal } from "@js-temporal/polyfill";
|
|
4
|
+
import { forwardRef, useContext, useMemo } from "react";
|
|
5
|
+
import { useCalendarStable, useCalendarState } from "./calendar-context";
|
|
6
|
+
import { WeekDataContext, GridContext } from "./context";
|
|
7
|
+
import { useMonthViewState } from "./month-view-context";
|
|
8
|
+
import { MonthViewStableContext } from "./month-view-context";
|
|
9
|
+
import type { StateAttributesMapping } from "./types";
|
|
10
|
+
import type {
|
|
11
|
+
ValueFormat,
|
|
12
|
+
RangeSelectedState,
|
|
13
|
+
RangeSelectedProps,
|
|
14
|
+
OutsideDays,
|
|
15
|
+
TemporalNamespace,
|
|
16
|
+
} from "./types";
|
|
17
|
+
import { computeWeekRangeInfo } from "./utils";
|
|
18
|
+
|
|
19
|
+
export const rangeOverlayStateAttributesMapping = {
|
|
20
|
+
root: () => null,
|
|
21
|
+
active: (v) => (v ? { "data-active": "" } : null),
|
|
22
|
+
weekIndex: (v) => ({ "data-week-index": String(v) }),
|
|
23
|
+
startIndex: (v) => ({ "data-start-index": String(v) }),
|
|
24
|
+
endIndex: (v) => ({ "data-end-index": String(v) }),
|
|
25
|
+
startDate: (v) => (v ? { "data-start-date": v } : null),
|
|
26
|
+
endDate: (v) => (v ? { "data-end-date": v } : null),
|
|
27
|
+
extendsBefore: (v) => (v ? { "data-extends-before": "" } : null),
|
|
28
|
+
extendsAfter: (v) => (v ? { "data-extends-after": "" } : null),
|
|
29
|
+
hasStart: (v) => (v ? { "data-has-start": "" } : null),
|
|
30
|
+
hasEnd: (v) => (v ? { "data-has-end": "" } : null),
|
|
31
|
+
orientation: (v) => ({ "data-orientation": v }),
|
|
32
|
+
} as const satisfies StateAttributesMapping<RangeSelectedState>;
|
|
33
|
+
|
|
34
|
+
type RangeInfo = {
|
|
35
|
+
active: boolean;
|
|
36
|
+
startIndex: number;
|
|
37
|
+
endIndex: number;
|
|
38
|
+
extendsBefore: boolean;
|
|
39
|
+
extendsAfter: boolean;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
export function computeClippedRangeInfo(
|
|
43
|
+
days: Temporal.PlainDate[],
|
|
44
|
+
rangeStart: Temporal.PlainDate | undefined,
|
|
45
|
+
rangeEnd: Temporal.PlainDate | undefined,
|
|
46
|
+
T: TemporalNamespace,
|
|
47
|
+
outsideDays: OutsideDays,
|
|
48
|
+
gridMonth: { year: number; month: number } | undefined,
|
|
49
|
+
): RangeInfo {
|
|
50
|
+
const raw = computeWeekRangeInfo(days, rangeStart, rangeEnd, T);
|
|
51
|
+
if ((outsideDays === "disabled" || outsideDays === "hidden") && raw.active && gridMonth) {
|
|
52
|
+
let { startIndex, endIndex, extendsBefore, extendsAfter } = raw;
|
|
53
|
+
// Clip range to only in-month cells
|
|
54
|
+
while (
|
|
55
|
+
startIndex <= endIndex &&
|
|
56
|
+
(days[startIndex].year !== gridMonth.year || days[startIndex].month !== gridMonth.month)
|
|
57
|
+
) {
|
|
58
|
+
startIndex++;
|
|
59
|
+
extendsBefore = true;
|
|
60
|
+
}
|
|
61
|
+
while (
|
|
62
|
+
endIndex >= startIndex &&
|
|
63
|
+
(days[endIndex].year !== gridMonth.year || days[endIndex].month !== gridMonth.month)
|
|
64
|
+
) {
|
|
65
|
+
endIndex--;
|
|
66
|
+
extendsAfter = true;
|
|
67
|
+
}
|
|
68
|
+
if (startIndex > endIndex) {
|
|
69
|
+
return {
|
|
70
|
+
active: false,
|
|
71
|
+
startIndex: 0,
|
|
72
|
+
endIndex: 0,
|
|
73
|
+
extendsBefore: false,
|
|
74
|
+
extendsAfter: false,
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
return { active: true, startIndex, endIndex, extendsBefore, extendsAfter };
|
|
78
|
+
}
|
|
79
|
+
return raw;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function RangeSelectedFn(props: RangeSelectedProps, ref: React.ForwardedRef<HTMLTableCellElement>) {
|
|
83
|
+
const { selectionMode } = useCalendarStable();
|
|
84
|
+
if (selectionMode !== "range") {
|
|
85
|
+
return null;
|
|
86
|
+
}
|
|
87
|
+
return <RangeSelectedInner ref={ref} {...props} />;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Visual overlay (`<td role="presentation">`) that highlights the selected
|
|
92
|
+
* date range within a single week row. Exposes data-attributes for
|
|
93
|
+
* `active`, `week-index`, `start-index`, `end-index`, `start-date`,
|
|
94
|
+
* `end-date`, `extends-before`, and `extends-after`.
|
|
95
|
+
*/
|
|
96
|
+
export const RangeSelected = forwardRef(RangeSelectedFn) as <F extends ValueFormat = ValueFormat>(
|
|
97
|
+
props: RangeSelectedProps<F> & React.RefAttributes<HTMLTableCellElement>,
|
|
98
|
+
) => React.ReactElement | null;
|
|
99
|
+
|
|
100
|
+
function RangeSelectedInnerFn(
|
|
101
|
+
props: RangeSelectedProps,
|
|
102
|
+
ref: React.ForwardedRef<HTMLTableCellElement>,
|
|
103
|
+
) {
|
|
104
|
+
const { render, ...otherProps } = props;
|
|
105
|
+
const weekData = useContext(WeekDataContext);
|
|
106
|
+
const { orientation } = useContext(GridContext);
|
|
107
|
+
const { temporal: T } = useCalendarStable();
|
|
108
|
+
const { rangeStart, rangeEnd } = useCalendarState();
|
|
109
|
+
const monthStable = useContext(MonthViewStableContext);
|
|
110
|
+
const { rootState } = useMonthViewState();
|
|
111
|
+
const outsideDays = monthStable?.outsideDays ?? "enabled";
|
|
112
|
+
|
|
113
|
+
const days = weekData?.days ?? [];
|
|
114
|
+
const gridMonth = weekData?.gridMonth;
|
|
115
|
+
|
|
116
|
+
const info = useMemo(
|
|
117
|
+
() => computeClippedRangeInfo(days, rangeStart, rangeEnd, T, outsideDays, gridMonth),
|
|
118
|
+
[days, rangeStart, rangeEnd, T, outsideDays, gridMonth],
|
|
119
|
+
);
|
|
120
|
+
|
|
121
|
+
const startDate = info.active ? days[info.startIndex].toString() : "";
|
|
122
|
+
const endDate = info.active ? days[info.endIndex].toString() : "";
|
|
123
|
+
|
|
124
|
+
const weekIndex = weekData?.weekIndex ?? 0;
|
|
125
|
+
|
|
126
|
+
const state = useMemo<RangeSelectedState>(
|
|
127
|
+
() => ({
|
|
128
|
+
root: rootState as any,
|
|
129
|
+
active: info.active,
|
|
130
|
+
weekIndex,
|
|
131
|
+
startIndex: info.startIndex,
|
|
132
|
+
endIndex: info.endIndex,
|
|
133
|
+
startDate,
|
|
134
|
+
endDate,
|
|
135
|
+
extendsBefore: info.extendsBefore,
|
|
136
|
+
extendsAfter: info.extendsAfter,
|
|
137
|
+
hasStart: rangeStart !== undefined,
|
|
138
|
+
hasEnd: rangeEnd !== undefined,
|
|
139
|
+
orientation,
|
|
140
|
+
}),
|
|
141
|
+
[rootState, info, weekIndex, startDate, endDate, rangeStart, rangeEnd, orientation],
|
|
142
|
+
);
|
|
143
|
+
|
|
144
|
+
const defaultProps: Record<string, unknown> = {
|
|
145
|
+
role: "presentation",
|
|
146
|
+
"aria-hidden": true,
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
return useRender({
|
|
150
|
+
defaultTagName: "td",
|
|
151
|
+
render,
|
|
152
|
+
ref: ref ? [ref] : [],
|
|
153
|
+
state,
|
|
154
|
+
stateAttributesMapping: rangeOverlayStateAttributesMapping,
|
|
155
|
+
props: mergeProps<"td">(defaultProps, otherProps),
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
const RangeSelectedInner = forwardRef(RangeSelectedInnerFn) as <
|
|
160
|
+
F extends ValueFormat = ValueFormat,
|
|
161
|
+
>(
|
|
162
|
+
props: RangeSelectedProps<F> & React.RefAttributes<HTMLTableCellElement>,
|
|
163
|
+
) => React.ReactElement | null;
|
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { Temporal } from "./temporal-polyfill";
|
|
3
|
+
|
|
4
|
+
const T = Temporal;
|
|
5
|
+
|
|
6
|
+
function date(iso: string) {
|
|
7
|
+
return T.PlainDate.from(iso);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
describe("PlainDate.from", () => {
|
|
11
|
+
it("parses ISO string", () => {
|
|
12
|
+
const d = date("2026-03-15");
|
|
13
|
+
expect(d.year).toBe(2026);
|
|
14
|
+
expect(d.month).toBe(3);
|
|
15
|
+
expect(d.day).toBe(15);
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it("parses ISO string with time portion (ignores time)", () => {
|
|
19
|
+
const d = T.PlainDate.from("2026-03-15T10:30:00");
|
|
20
|
+
expect(d.year).toBe(2026);
|
|
21
|
+
expect(d.month).toBe(3);
|
|
22
|
+
expect(d.day).toBe(15);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it("creates from object", () => {
|
|
26
|
+
const d = T.PlainDate.from({ year: 2026, month: 2, day: 28 });
|
|
27
|
+
expect(d.toString()).toBe("2026-02-28");
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it("constrains overflow day", () => {
|
|
31
|
+
const d = T.PlainDate.from({ year: 2026, month: 2, day: 31 }, { overflow: "constrain" });
|
|
32
|
+
expect(d.day).toBe(28);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it("constrains overflow day for leap year", () => {
|
|
36
|
+
const d = T.PlainDate.from({ year: 2024, month: 2, day: 31 }, { overflow: "constrain" });
|
|
37
|
+
expect(d.day).toBe(29);
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
describe("PlainDate.compare", () => {
|
|
42
|
+
it.each<{
|
|
43
|
+
description: string;
|
|
44
|
+
a: string;
|
|
45
|
+
b: string;
|
|
46
|
+
expected: number;
|
|
47
|
+
}>([
|
|
48
|
+
{
|
|
49
|
+
description: "equal dates",
|
|
50
|
+
a: "2026-03-15",
|
|
51
|
+
b: "2026-03-15",
|
|
52
|
+
expected: 0,
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
description: "a before b (day)",
|
|
56
|
+
a: "2026-03-14",
|
|
57
|
+
b: "2026-03-15",
|
|
58
|
+
expected: -1,
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
description: "a after b (day)",
|
|
62
|
+
a: "2026-03-16",
|
|
63
|
+
b: "2026-03-15",
|
|
64
|
+
expected: 1,
|
|
65
|
+
},
|
|
66
|
+
{
|
|
67
|
+
description: "a before b (month)",
|
|
68
|
+
a: "2026-02-15",
|
|
69
|
+
b: "2026-03-15",
|
|
70
|
+
expected: -1,
|
|
71
|
+
},
|
|
72
|
+
{
|
|
73
|
+
description: "a before b (year)",
|
|
74
|
+
a: "2025-03-15",
|
|
75
|
+
b: "2026-03-15",
|
|
76
|
+
expected: -1,
|
|
77
|
+
},
|
|
78
|
+
])("$description", ({ a, b, expected }) => {
|
|
79
|
+
expect(T.PlainDate.compare(date(a), date(b))).toBe(expected);
|
|
80
|
+
});
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
describe("PlainDate add/subtract", () => {
|
|
84
|
+
it("adds days", () => {
|
|
85
|
+
expect(date("2026-03-15").add({ days: 5 }).toString()).toBe("2026-03-20");
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it("adds weeks", () => {
|
|
89
|
+
expect(date("2026-03-15").add({ weeks: 2 }).toString()).toBe("2026-03-29");
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it("adds months with day clamping (Jan 31 + 1 month)", () => {
|
|
93
|
+
expect(date("2026-01-31").add({ months: 1 }).toString()).toBe("2026-02-28");
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it("adds months with day clamping (Jan 31 + 1 month, leap year)", () => {
|
|
97
|
+
expect(date("2024-01-31").add({ months: 1 }).toString()).toBe("2024-02-29");
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it("subtracts days", () => {
|
|
101
|
+
expect(date("2026-03-15").subtract({ days: 5 }).toString()).toBe("2026-03-10");
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it("subtracts months", () => {
|
|
105
|
+
expect(date("2026-03-31").subtract({ months: 1 }).toString()).toBe("2026-02-28");
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it("crosses year boundary forward", () => {
|
|
109
|
+
expect(date("2026-12-31").add({ days: 1 }).toString()).toBe("2027-01-01");
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it("crosses year boundary backward", () => {
|
|
113
|
+
expect(date("2027-01-01").subtract({ days: 1 }).toString()).toBe("2026-12-31");
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it("adds months crossing year boundary", () => {
|
|
117
|
+
expect(date("2026-11-15").add({ months: 3 }).toString()).toBe("2027-02-15");
|
|
118
|
+
});
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
describe("PlainDate since/until", () => {
|
|
122
|
+
it("positive days", () => {
|
|
123
|
+
expect(date("2026-03-20").since(date("2026-03-10")).days).toBe(10);
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
it("negative days", () => {
|
|
127
|
+
expect(date("2026-03-10").since(date("2026-03-20")).days).toBe(-10);
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
it("zero days", () => {
|
|
131
|
+
expect(date("2026-03-15").since(date("2026-03-15")).days).toBe(0);
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
it("until returns positive for future dates", () => {
|
|
135
|
+
expect(date("2026-03-10").until(date("2026-03-20")).days).toBe(10);
|
|
136
|
+
});
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
describe("PlainDate dayOfWeek", () => {
|
|
140
|
+
it.each<{ description: string; d: string; expected: number }>([
|
|
141
|
+
{ description: "2026-03-15 is Sunday", d: "2026-03-15", expected: 7 },
|
|
142
|
+
{ description: "2026-03-16 is Monday", d: "2026-03-16", expected: 1 },
|
|
143
|
+
{ description: "2026-03-17 is Tuesday", d: "2026-03-17", expected: 2 },
|
|
144
|
+
{ description: "2026-03-21 is Saturday", d: "2026-03-21", expected: 6 },
|
|
145
|
+
])("$description", ({ d, expected }) => {
|
|
146
|
+
expect(date(d).dayOfWeek).toBe(expected);
|
|
147
|
+
});
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
describe("PlainDate daysInMonth", () => {
|
|
151
|
+
it.each<{ description: string; d: string; expected: number }>([
|
|
152
|
+
{ description: "Feb non-leap", d: "2026-02-01", expected: 28 },
|
|
153
|
+
{ description: "Feb leap", d: "2024-02-01", expected: 29 },
|
|
154
|
+
{ description: "30-day month (Apr)", d: "2026-04-15", expected: 30 },
|
|
155
|
+
{ description: "31-day month (Mar)", d: "2026-03-15", expected: 31 },
|
|
156
|
+
])("$description", ({ d, expected }) => {
|
|
157
|
+
expect(date(d).daysInMonth).toBe(expected);
|
|
158
|
+
});
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
describe("PlainDate toString roundtrip", () => {
|
|
162
|
+
it("roundtrips through from/toString", () => {
|
|
163
|
+
const iso = "2026-03-15";
|
|
164
|
+
expect(T.PlainDate.from(date(iso).toString()).toString()).toBe(iso);
|
|
165
|
+
});
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
describe("PlainDate equals", () => {
|
|
169
|
+
it("returns true for same date", () => {
|
|
170
|
+
expect(date("2026-03-15").equals(date("2026-03-15"))).toBe(true);
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
it("returns false for different date", () => {
|
|
174
|
+
expect(date("2026-03-15").equals(date("2026-03-16"))).toBe(false);
|
|
175
|
+
});
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
describe("PlainDateTime", () => {
|
|
179
|
+
it("from string", () => {
|
|
180
|
+
const dt = T.PlainDateTime.from("2026-03-15T10:30:45");
|
|
181
|
+
expect(dt.year).toBe(2026);
|
|
182
|
+
expect(dt.hour).toBe(10);
|
|
183
|
+
expect(dt.minute).toBe(30);
|
|
184
|
+
expect(dt.second).toBe(45);
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
it("from object", () => {
|
|
188
|
+
const dt = T.PlainDateTime.from({
|
|
189
|
+
year: 2026,
|
|
190
|
+
month: 3,
|
|
191
|
+
day: 15,
|
|
192
|
+
hour: 10,
|
|
193
|
+
});
|
|
194
|
+
expect(dt.hour).toBe(10);
|
|
195
|
+
expect(dt.minute).toBe(0);
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
it("toPlainDate", () => {
|
|
199
|
+
const dt = T.PlainDateTime.from("2026-03-15T10:30:00");
|
|
200
|
+
expect(dt.toPlainDate().toString()).toBe("2026-03-15");
|
|
201
|
+
});
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
describe("PlainMonthDay", () => {
|
|
205
|
+
it("from object", () => {
|
|
206
|
+
const md = T.PlainMonthDay.from({ month: 7, day: 4 });
|
|
207
|
+
expect(md.monthCode).toBe("M07");
|
|
208
|
+
expect(md.day).toBe(4);
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
it("toPlainDate with year", () => {
|
|
212
|
+
const md = T.PlainMonthDay.from({ month: 2, day: 28 });
|
|
213
|
+
const pd = md.toPlainDate({ year: 2026 });
|
|
214
|
+
expect(pd.toString()).toBe("2026-02-28");
|
|
215
|
+
});
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
describe("PlainYearMonth", () => {
|
|
219
|
+
it("from object", () => {
|
|
220
|
+
const ym = T.PlainYearMonth.from({ year: 2026, month: 6 });
|
|
221
|
+
expect(ym.year).toBe(2026);
|
|
222
|
+
expect(ym.month).toBe(6);
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
it("toPlainDate with day", () => {
|
|
226
|
+
const ym = T.PlainYearMonth.from({ year: 2026, month: 6 });
|
|
227
|
+
expect(ym.toPlainDate({ day: 15 }).toString()).toBe("2026-06-15");
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
it("daysInMonth", () => {
|
|
231
|
+
expect(T.PlainYearMonth.from({ year: 2026, month: 2 }).daysInMonth).toBe(28);
|
|
232
|
+
expect(T.PlainYearMonth.from({ year: 2024, month: 2 }).daysInMonth).toBe(29);
|
|
233
|
+
});
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
describe("ZonedDateTime", () => {
|
|
237
|
+
it("extracts components in UTC", () => {
|
|
238
|
+
const pdt = T.PlainDateTime.from("2026-03-15T10:30:00");
|
|
239
|
+
const zdt = pdt.toZonedDateTime("UTC");
|
|
240
|
+
expect(zdt.year).toBe(2026);
|
|
241
|
+
expect(zdt.month).toBe(3);
|
|
242
|
+
expect(zdt.day).toBe(15);
|
|
243
|
+
expect(zdt.hour).toBe(10);
|
|
244
|
+
expect(zdt.minute).toBe(30);
|
|
245
|
+
expect(zdt.timeZoneId).toBe("UTC");
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
it("extracts components in a non-UTC timezone", () => {
|
|
249
|
+
const pdt = T.PlainDateTime.from("2026-03-15T10:30:00");
|
|
250
|
+
const zdt = pdt.toZonedDateTime("America/New_York");
|
|
251
|
+
expect(zdt.year).toBe(2026);
|
|
252
|
+
expect(zdt.month).toBe(3);
|
|
253
|
+
expect(zdt.day).toBe(15);
|
|
254
|
+
expect(zdt.hour).toBe(10);
|
|
255
|
+
expect(zdt.timeZoneId).toBe("America/New_York");
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
it("toPlainDate roundtrip", () => {
|
|
259
|
+
const pd = date("2026-06-20");
|
|
260
|
+
const zdt = pd.toZonedDateTime("America/New_York");
|
|
261
|
+
expect(zdt.toPlainDate().toString()).toBe("2026-06-20");
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
it("toPlainDateTime roundtrip", () => {
|
|
265
|
+
const pdt = T.PlainDateTime.from("2026-03-15T14:30:00");
|
|
266
|
+
const zdt = pdt.toZonedDateTime("UTC");
|
|
267
|
+
const roundtrip = zdt.toPlainDateTime();
|
|
268
|
+
expect(roundtrip.hour).toBe(14);
|
|
269
|
+
expect(roundtrip.minute).toBe(30);
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
it("epochMilliseconds is valid", () => {
|
|
273
|
+
const zdt = T.Now.zonedDateTimeISO("UTC");
|
|
274
|
+
expect(typeof zdt.epochMilliseconds).toBe("number");
|
|
275
|
+
expect(Math.abs(zdt.epochMilliseconds - Date.now())).toBeLessThan(1000);
|
|
276
|
+
});
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
describe("Now", () => {
|
|
280
|
+
it("timeZoneId returns a non-empty string", () => {
|
|
281
|
+
const tz = T.Now.timeZoneId();
|
|
282
|
+
expect(typeof tz).toBe("string");
|
|
283
|
+
expect(tz.length).toBeGreaterThan(0);
|
|
284
|
+
});
|
|
285
|
+
|
|
286
|
+
it("zonedDateTimeISO returns current time", () => {
|
|
287
|
+
const zdt = T.Now.zonedDateTimeISO("UTC");
|
|
288
|
+
expect(zdt.year).toBeGreaterThan(2020);
|
|
289
|
+
});
|
|
290
|
+
|
|
291
|
+
it("plainDateISO returns today", () => {
|
|
292
|
+
const pd = T.Now.plainDateISO();
|
|
293
|
+
expect(pd.year).toBeGreaterThan(2020);
|
|
294
|
+
expect(pd.month).toBeGreaterThanOrEqual(1);
|
|
295
|
+
expect(pd.month).toBeLessThanOrEqual(12);
|
|
296
|
+
});
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
describe("PlainDate → ZonedDateTime → PlainDate roundtrip", () => {
|
|
300
|
+
it.each(["2026-01-01", "2026-06-15", "2026-12-31", "2024-02-29"])(
|
|
301
|
+
"%s roundtrips through UTC",
|
|
302
|
+
(iso) => {
|
|
303
|
+
const pd = date(iso);
|
|
304
|
+
const zdt = pd.toZonedDateTime("UTC");
|
|
305
|
+
expect(zdt.toPlainDate().toString()).toBe(iso);
|
|
306
|
+
},
|
|
307
|
+
);
|
|
308
|
+
|
|
309
|
+
it.each(["2026-01-01", "2026-06-15", "2026-12-31"])(
|
|
310
|
+
"%s roundtrips through America/New_York",
|
|
311
|
+
(iso) => {
|
|
312
|
+
const pd = date(iso);
|
|
313
|
+
const zdt = pd.toZonedDateTime("America/New_York");
|
|
314
|
+
expect(zdt.toPlainDate().toString()).toBe(iso);
|
|
315
|
+
},
|
|
316
|
+
);
|
|
317
|
+
});
|