@klinking/colander 3.0.1-alpha.1 → 3.1.0-alpha.10

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.
Files changed (98) hide show
  1. package/dist/formats/date.cjs +2 -3
  2. package/dist/formats/date.d.cts +10 -14
  3. package/dist/formats/date.d.mts +10 -14
  4. package/dist/formats/date.mjs +2 -3
  5. package/dist/formats/object.cjs +2 -3
  6. package/dist/formats/object.d.cts +10 -14
  7. package/dist/formats/object.d.mts +10 -14
  8. package/dist/formats/object.mjs +2 -3
  9. package/dist/formats/plain-date-time.cjs +2 -3
  10. package/dist/formats/plain-date-time.d.cts +10 -14
  11. package/dist/formats/plain-date-time.d.mts +10 -14
  12. package/dist/formats/plain-date-time.mjs +2 -3
  13. package/dist/formats/plain-date.cjs +2 -3
  14. package/dist/formats/plain-date.d.cts +10 -14
  15. package/dist/formats/plain-date.d.mts +10 -14
  16. package/dist/formats/plain-date.mjs +2 -3
  17. package/dist/formats/plain-month-day.cjs +2 -3
  18. package/dist/formats/plain-month-day.d.cts +10 -14
  19. package/dist/formats/plain-month-day.d.mts +10 -14
  20. package/dist/formats/plain-month-day.mjs +2 -3
  21. package/dist/formats/plain-year-month.cjs +2 -3
  22. package/dist/formats/plain-year-month.d.cts +10 -14
  23. package/dist/formats/plain-year-month.d.mts +10 -14
  24. package/dist/formats/plain-year-month.mjs +2 -3
  25. package/dist/formats/zoned-date-time.cjs +2 -3
  26. package/dist/formats/zoned-date-time.d.cts +10 -14
  27. package/dist/formats/zoned-date-time.d.mts +10 -14
  28. package/dist/formats/zoned-date-time.mjs +2 -3
  29. package/dist/{weeks-view-context-CNAq2Hiu.d.cts → index-K1_zOIpa.d.mts} +198 -114
  30. package/dist/{weeks-view-context-CsP3PBiu.d.mts → index-bRoYX03d.d.cts} +198 -114
  31. package/dist/index.cjs +1 -1
  32. package/dist/index.d.cts +2 -11
  33. package/dist/index.d.mts +2 -11
  34. package/dist/index.mjs +1 -2
  35. package/dist/{weeks-navigation-CJzhxbgj.cjs → weeks-navigation-3UTRiTUP.cjs} +1102 -952
  36. package/dist/{weeks-navigation-Bg0nZLgh.mjs → weeks-navigation-BoHqK5fq.mjs} +1102 -951
  37. package/package.json +21 -18
  38. package/src/calendar-provider.test.tsx +88 -1
  39. package/src/calendar-provider.tsx +89 -73
  40. package/src/calendar-types.ts +2 -0
  41. package/src/compute-weeks-in-window.test.ts +1 -1
  42. package/src/day-cell-state.test.ts +214 -0
  43. package/src/day-cell-state.ts +244 -0
  44. package/src/day-cell-store.test.tsx +273 -0
  45. package/src/day-cell-store.tsx +204 -0
  46. package/src/day-cell.test.tsx +73 -90
  47. package/src/day-cell.tsx +106 -444
  48. package/src/drag-handle.tsx +2 -2
  49. package/src/formats/date.ts +2 -0
  50. package/src/formats/formats.test.ts +1 -1
  51. package/src/formats/object.ts +2 -0
  52. package/src/formats/plain-date-time.ts +2 -0
  53. package/src/formats/plain-date.ts +2 -0
  54. package/src/formats/plain-month-day.ts +2 -0
  55. package/src/formats/plain-year-month.ts +2 -0
  56. package/src/formats/zoned-date-time.ts +2 -0
  57. package/src/grid-header.tsx +3 -3
  58. package/src/grid.event-perf.test.tsx +7 -1
  59. package/src/grid.perf.test.tsx +7 -1
  60. package/src/grid.tsx +38 -77
  61. package/src/index.ts +2 -0
  62. package/src/keyboard.test.ts +1 -1
  63. package/src/month-separator.test.tsx +1 -1
  64. package/src/month-view-navigation.ts +180 -0
  65. package/src/month-view-types.ts +3 -0
  66. package/src/month-view.bounds.test.tsx +136 -0
  67. package/src/month-view.test.tsx +89 -1
  68. package/src/month-view.tsx +74 -217
  69. package/src/month-window.test.ts +207 -0
  70. package/src/month-window.ts +93 -0
  71. package/src/navigation.tsx +18 -76
  72. package/src/out-of-range.test.ts +34 -54
  73. package/src/out-of-range.ts +47 -123
  74. package/src/range-preview.test.ts +120 -0
  75. package/src/range-preview.tsx +3 -5
  76. package/src/resolve-first-week.test.ts +1 -1
  77. package/src/root-selection.ts +99 -97
  78. package/src/selected-range.tsx +3 -5
  79. package/src/store.ts +72 -0
  80. package/src/today.test.tsx +105 -0
  81. package/src/types.ts +24 -1
  82. package/src/utils.bench.ts +158 -106
  83. package/src/utils.test.ts +26 -1
  84. package/src/utils.ts +40 -9
  85. package/src/view-context.test.tsx +126 -0
  86. package/src/view-context.ts +45 -0
  87. package/src/week-number.test.tsx +76 -0
  88. package/src/week-number.tsx +48 -19
  89. package/src/weeks-grid.tsx +20 -10
  90. package/src/weeks-keyboard.test.ts +1 -1
  91. package/src/weeks-navigation.tsx +15 -31
  92. package/src/weeks-view-state.ts +67 -101
  93. package/src/weeks-view-types.ts +4 -1
  94. package/src/weeks-view.bounds.test.tsx +246 -0
  95. package/src/weeks-view.test.tsx +116 -14
  96. package/src/weeks-view.tsx +24 -52
  97. package/src/weeks-window.test.ts +131 -0
  98. package/src/weeks-window.ts +102 -0
package/src/store.ts ADDED
@@ -0,0 +1,72 @@
1
+ import {
2
+ useCallback,
3
+ useEffect,
4
+ useInsertionEffect,
5
+ useLayoutEffect,
6
+ useRef,
7
+ } from "react";
8
+ import { useSyncExternalStoreWithSelector } from "use-sync-external-store/shim/with-selector";
9
+
10
+ /** A minimal external store. `set` replaces the state and notifies. */
11
+ export interface Store<T> {
12
+ get: () => T;
13
+ set: (next: T) => void;
14
+ subscribe: (listener: () => void) => () => void;
15
+ }
16
+
17
+ export function createStore<T>(initial: T): Store<T> {
18
+ let state = initial;
19
+ const listeners = new Set<() => void>();
20
+ return {
21
+ get: () => state,
22
+ set: (next) => {
23
+ if (Object.is(state, next)) return;
24
+ state = next;
25
+ for (const listener of listeners) listener();
26
+ },
27
+ subscribe: (listener) => {
28
+ listeners.add(listener);
29
+ return () => listeners.delete(listener);
30
+ },
31
+ };
32
+ }
33
+
34
+ /** `useLayoutEffect` in the browser, `useEffect` on the server (no warning). */
35
+ export const useIsoLayoutEffect =
36
+ typeof window === "undefined" ? useEffect : useLayoutEffect;
37
+
38
+ /**
39
+ * Subscribes to the slice of `store` that `select` picks. The component
40
+ * re-renders only when `isEqual` says the slice changed; otherwise the
41
+ * previously returned slice is handed back, keeping its identity.
42
+ */
43
+ export function useStoreSelector<T, S>(
44
+ store: Store<T>,
45
+ select: (state: T) => S,
46
+ isEqual: (a: S, b: S) => boolean,
47
+ ): S {
48
+ return useSyncExternalStoreWithSelector(
49
+ store.subscribe,
50
+ store.get,
51
+ store.get,
52
+ select,
53
+ isEqual,
54
+ );
55
+ }
56
+
57
+ /**
58
+ * Returns a function with a stable identity that always calls the latest
59
+ * `fn`, so contexts holding it don't change when its closure does. Call it
60
+ * from event handlers and effects, not during render.
61
+ */
62
+ export function useStableCallback<A extends unknown[], R>(
63
+ fn: (...args: A) => R,
64
+ ): (...args: A) => R {
65
+ const latest = useRef(fn);
66
+ // Insertion effects run before every layout effect, so no effect in the
67
+ // tree can call a stale `fn`.
68
+ useInsertionEffect(() => {
69
+ latest.current = fn;
70
+ });
71
+ return useCallback((...args: A) => latest.current(...args), []);
72
+ }
@@ -0,0 +1,105 @@
1
+ import { Temporal } from "@js-temporal/polyfill";
2
+ import { render, screen, cleanup } from "@testing-library/react";
3
+ import {
4
+ describe,
5
+ it,
6
+ expect,
7
+ vi,
8
+ afterEach,
9
+ beforeEach,
10
+ } from "vite-plus/test";
11
+ import {
12
+ Grid,
13
+ GridBody,
14
+ WeekTemplate,
15
+ DayCellTemplate,
16
+ DayButton,
17
+ } from "./grid";
18
+ import { MonthView } from "./month-view";
19
+ import { useViewContext } from "./view-context";
20
+ import { WeeksView } from "./weeks-view";
21
+
22
+ const T = Temporal;
23
+
24
+ function FocusDisplay() {
25
+ const view = useViewContext();
26
+ return <span data-testid="focus">{view.focusedDate.toString()}</span>;
27
+ }
28
+
29
+ const days = (
30
+ <Grid>
31
+ <GridBody>
32
+ <WeekTemplate>
33
+ <DayCellTemplate>
34
+ <DayButton />
35
+ </DayCellTemplate>
36
+ </WeekTemplate>
37
+ </GridBody>
38
+ </Grid>
39
+ );
40
+
41
+ // 2026-03-31T20:00Z is 2026-04-01 in Auckland (NZDT, +13) and 2026-03-31 in Pago Pago (-11).
42
+ // 2026-04-04T20:00Z is Sunday 2026-04-05 in Auckland (NZST, +12) and Saturday 2026-04-04 in Pago Pago.
43
+ describe("today follows the provider time zone", () => {
44
+ beforeEach(() => {
45
+ vi.useFakeTimers({ toFake: ["Date"] });
46
+ });
47
+ afterEach(() => {
48
+ cleanup();
49
+ vi.useRealTimers();
50
+ });
51
+
52
+ it.each([
53
+ {
54
+ description: "ahead of the clock's UTC date",
55
+ timeZone: "Pacific/Auckland",
56
+ expected: { today: "2026-04-01", todayCell: "1" },
57
+ },
58
+ {
59
+ description: "behind the clock's UTC date",
60
+ timeZone: "Pacific/Pago_Pago",
61
+ expected: { today: "2026-03-31", todayCell: "31" },
62
+ },
63
+ ])("MonthView: $description", ({ timeZone, expected }) => {
64
+ vi.setSystemTime(new Date("2026-03-31T20:00:00Z"));
65
+ const { container } = render(
66
+ <MonthView temporal={T} timeZone={timeZone}>
67
+ <FocusDisplay />
68
+ {days}
69
+ </MonthView>,
70
+ );
71
+
72
+ expect(screen.getByTestId("focus").textContent).toBe(expected.today);
73
+ const marked = container.querySelectorAll("td[data-today]");
74
+ expect(marked.length).toBe(1);
75
+ expect(marked[0]?.textContent).toBe(expected.todayCell);
76
+ expect(marked[0]?.hasAttribute("data-outside-month")).toBe(false);
77
+ });
78
+
79
+ it.each([
80
+ {
81
+ description: "ahead of the clock's UTC date",
82
+ timeZone: "Pacific/Auckland",
83
+ expected: { firstCell: "5", todayCell: "5" },
84
+ },
85
+ {
86
+ description: "behind the clock's UTC date",
87
+ timeZone: "Pacific/Pago_Pago",
88
+ expected: { firstCell: "29", todayCell: "4" },
89
+ },
90
+ ])("WeeksView: $description", ({ timeZone, expected }) => {
91
+ vi.setSystemTime(new Date("2026-04-04T20:00:00Z"));
92
+ const { container } = render(
93
+ <WeeksView temporal={T} timeZone={timeZone} weekCount={1}>
94
+ {days}
95
+ </WeeksView>,
96
+ );
97
+
98
+ expect(container.querySelector("td[role='gridcell']")?.textContent).toBe(
99
+ expected.firstCell,
100
+ );
101
+ const marked = container.querySelectorAll("td[data-today]");
102
+ expect(marked.length).toBe(1);
103
+ expect(marked[0]?.textContent).toBe(expected.todayCell);
104
+ });
105
+ });
package/src/types.ts CHANGED
@@ -813,11 +813,34 @@ export type WeekNumberCellState = {
813
813
  weekNumber: number;
814
814
  };
815
815
 
816
+ /**
817
+ * Computes the week number displayed by a `WeekNumberCell`.
818
+ *
819
+ * @param weekDays - The dates of the week row, in display order.
820
+ * @param temporal - The Temporal namespace in use by the calendar.
821
+ * @returns The week number to display.
822
+ */
823
+ export type WeekNumberStrategy = (
824
+ weekDays: Temporal.PlainDate[],
825
+ temporal: TemporalNamespace,
826
+ ) => number;
827
+
828
+ /** Own props for the `WeekNumberCell` component. */
829
+ export interface WeekNumberCellOwnProps {
830
+ /**
831
+ * Custom week-numbering strategy. When omitted, week numbers follow
832
+ * ISO 8601 (generalized to the week's midpoint day for calendars whose
833
+ * weeks are not 7 days). See {@link WeekNumberCell} for details.
834
+ */
835
+ getWeekNumber?: WeekNumberStrategy;
836
+ }
837
+
816
838
  /** Full props for the `WeekNumberCell` component. */
817
839
  export type WeekNumberCellProps = useRender.ComponentProps<
818
840
  "td",
819
841
  WeekNumberCellState
820
- >;
842
+ > &
843
+ WeekNumberCellOwnProps;
821
844
 
822
845
  /** State exposed by the `WeekNumberHeader` component. */
823
846
  export type WeekNumberHeaderState = {
@@ -1,5 +1,5 @@
1
1
  import { Temporal } from "@js-temporal/polyfill";
2
- import { bench, describe } from "vitest";
2
+ import { test, describe } from "vite-plus/test";
3
3
  import type { TemporalNamespace } from "./types";
4
4
  import {
5
5
  getMonthWeeks,
@@ -29,82 +29,112 @@ const rangeEnd = Temporal.PlainDate.from("2026-03-20");
29
29
  const midWeek = marchWeeks[2]!; // a week in the middle of the range
30
30
 
31
31
  describe("getMonthWeeks", () => {
32
- bench("generate month grid (March 2026)", () => {
33
- getMonthWeeks(2026, 3, T);
32
+ test("generate month grid (March 2026)", async ({ bench }) => {
33
+ await bench("generate month grid (March 2026)", () => {
34
+ getMonthWeeks(2026, 3, T);
35
+ }).run();
34
36
  });
35
37
 
36
- bench("generate month grid (February — short month)", () => {
37
- getMonthWeeks(2026, 2, T);
38
+ test("generate month grid (February — short month)", async ({ bench }) => {
39
+ await bench("generate month grid (February — short month)", () => {
40
+ getMonthWeeks(2026, 2, T);
41
+ }).run();
38
42
  });
39
43
 
40
- bench("generate month grid (August — 6-week grid)", () => {
41
- getMonthWeeks(2026, 8, T);
44
+ test("generate month grid (August — 6-week grid)", async ({ bench }) => {
45
+ await bench("generate month grid (August — 6-week grid)", () => {
46
+ getMonthWeeks(2026, 8, T);
47
+ }).run();
42
48
  });
43
49
  });
44
50
 
45
51
  describe("computeWeekRangeInfo", () => {
46
- bench("week fully inside range", () => {
47
- computeWeekRangeInfo(midWeek, rangeStart, rangeEnd, T);
52
+ test("week fully inside range", async ({ bench }) => {
53
+ await bench("week fully inside range", () => {
54
+ computeWeekRangeInfo(midWeek, rangeStart, rangeEnd, T);
55
+ }).run();
48
56
  });
49
57
 
50
- bench("week with no overlap", () => {
51
- const noOverlapWeek = marchWeeks[marchWeeks.length - 1]!;
52
- computeWeekRangeInfo(
53
- noOverlapWeek,
54
- Temporal.PlainDate.from("2026-03-01"),
55
- Temporal.PlainDate.from("2026-03-04"),
56
- T,
57
- );
58
+ test("week with no overlap", async ({ bench }) => {
59
+ await bench("week with no overlap", () => {
60
+ const noOverlapWeek = marchWeeks[marchWeeks.length - 1]!;
61
+ computeWeekRangeInfo(
62
+ noOverlapWeek,
63
+ Temporal.PlainDate.from("2026-03-01"),
64
+ Temporal.PlainDate.from("2026-03-04"),
65
+ T,
66
+ );
67
+ }).run();
58
68
  });
59
69
 
60
- bench("no range (undefined bounds)", () => {
61
- computeWeekRangeInfo(midWeek, undefined, undefined, T);
70
+ test("no range (undefined bounds)", async ({ bench }) => {
71
+ await bench("no range (undefined bounds)", () => {
72
+ computeWeekRangeInfo(midWeek, undefined, undefined, T);
73
+ }).run();
62
74
  });
63
75
 
64
- bench("all 6 weeks of a month", () => {
65
- for (const week of marchWeeks) {
66
- computeWeekRangeInfo(week, rangeStart, rangeEnd, T);
67
- }
76
+ test("all 6 weeks of a month", async ({ bench }) => {
77
+ await bench("all 6 weeks of a month", () => {
78
+ for (const week of marchWeeks) {
79
+ computeWeekRangeInfo(week, rangeStart, rangeEnd, T);
80
+ }
81
+ }).run();
68
82
  });
69
83
  });
70
84
 
71
85
  describe("computeAdjacentMonth", () => {
72
- bench("next month", () => {
73
- computeAdjacentMonth({ year: 2026, month: 3 }, "next", T);
86
+ test("next month", async ({ bench }) => {
87
+ await bench("next month", () => {
88
+ computeAdjacentMonth({ year: 2026, month: 3 }, "next", T);
89
+ }).run();
74
90
  });
75
91
 
76
- bench("prev month", () => {
77
- computeAdjacentMonth({ year: 2026, month: 3 }, "prev", T);
92
+ test("prev month", async ({ bench }) => {
93
+ await bench("prev month", () => {
94
+ computeAdjacentMonth({ year: 2026, month: 3 }, "prev", T);
95
+ }).run();
78
96
  });
79
97
 
80
- bench("year boundary (Dec → Jan)", () => {
81
- computeAdjacentMonth({ year: 2026, month: 12 }, "next", T);
98
+ test("year boundary (Dec → Jan)", async ({ bench }) => {
99
+ await bench("year boundary (Dec → Jan)", () => {
100
+ computeAdjacentMonth({ year: 2026, month: 12 }, "next", T);
101
+ }).run();
82
102
  });
83
103
 
84
- bench("year boundary (Jan → Dec)", () => {
85
- computeAdjacentMonth({ year: 2026, month: 1 }, "prev", T);
104
+ test("year boundary (Jan → Dec)", async ({ bench }) => {
105
+ await bench("year boundary (Jan → Dec)", () => {
106
+ computeAdjacentMonth({ year: 2026, month: 1 }, "prev", T);
107
+ }).run();
86
108
  });
87
109
  });
88
110
 
89
111
  describe("isInRange", () => {
90
112
  const allDays = marchWeeks.flat();
91
113
 
92
- bench("single date check (in range)", () => {
93
- isInRange(Temporal.PlainDate.from("2026-03-10"), rangeStart, rangeEnd, T);
114
+ test("single date check (in range)", async ({ bench }) => {
115
+ await bench("single date check (in range)", () => {
116
+ isInRange(Temporal.PlainDate.from("2026-03-10"), rangeStart, rangeEnd, T);
117
+ }).run();
94
118
  });
95
119
 
96
- bench("single date check (out of range)", () => {
97
- isInRange(Temporal.PlainDate.from("2026-03-25"), rangeStart, rangeEnd, T);
120
+ test("single date check (out of range)", async ({ bench }) => {
121
+ await bench("single date check (out of range)", () => {
122
+ isInRange(Temporal.PlainDate.from("2026-03-25"), rangeStart, rangeEnd, T);
123
+ }).run();
98
124
  });
99
125
 
100
- bench("check all 42 grid cells", () => {
101
- for (const day of allDays) {
102
- isInRange(day, rangeStart, rangeEnd, T);
103
- }
126
+ test("check all 42 grid cells", async ({ bench }) => {
127
+ await bench("check all 42 grid cells", () => {
128
+ for (const day of allDays) {
129
+ isInRange(day, rangeStart, rangeEnd, T);
130
+ }
131
+ }).run();
104
132
  });
105
133
 
106
- bench("undefined range (early exit)", () => {
107
- isInRange(Temporal.PlainDate.from("2026-03-10"), undefined, undefined, T);
134
+ test("undefined range (early exit)", async ({ bench }) => {
135
+ await bench("undefined range (early exit)", () => {
136
+ isInRange(Temporal.PlainDate.from("2026-03-10"), undefined, undefined, T);
137
+ }).run();
108
138
  });
109
139
  });
110
140
 
@@ -113,49 +143,65 @@ describe("toZonedDateTime / fromZonedDateTime", () => {
113
143
  const plainDateTime = Temporal.PlainDateTime.from("2026-03-15T10:30:00");
114
144
  const nativeDate = new Date(2026, 2, 15, 10, 30, 0);
115
145
 
116
- bench("PlainDate → ZonedDateTime", () => {
117
- toZonedDateTime({ format: "PlainDate", value: plainDate }, timeZone, T);
146
+ test("PlainDate → ZonedDateTime", async ({ bench }) => {
147
+ await bench("PlainDate → ZonedDateTime", () => {
148
+ toZonedDateTime({ format: "PlainDate", value: plainDate }, timeZone, T);
149
+ }).run();
118
150
  });
119
151
 
120
- bench("PlainDateTime → ZonedDateTime", () => {
121
- toZonedDateTime(
122
- { format: "PlainDateTime", value: plainDateTime },
123
- timeZone,
124
- T,
125
- );
152
+ test("PlainDateTime → ZonedDateTime", async ({ bench }) => {
153
+ await bench("PlainDateTime → ZonedDateTime", () => {
154
+ toZonedDateTime(
155
+ { format: "PlainDateTime", value: plainDateTime },
156
+ timeZone,
157
+ T,
158
+ );
159
+ }).run();
126
160
  });
127
161
 
128
- bench("Date → ZonedDateTime", () => {
129
- toZonedDateTime({ format: "Date", value: nativeDate }, timeZone, T);
162
+ test("Date → ZonedDateTime", async ({ bench }) => {
163
+ await bench("Date → ZonedDateTime", () => {
164
+ toZonedDateTime({ format: "Date", value: nativeDate }, timeZone, T);
165
+ }).run();
130
166
  });
131
167
 
132
- bench("ZonedDateTime → PlainDate", () => {
133
- const zdt = plainDate.toZonedDateTime(timeZone);
134
- fromZonedDateTime(zdt, "PlainDate", T);
168
+ test("ZonedDateTime → PlainDate", async ({ bench }) => {
169
+ await bench("ZonedDateTime → PlainDate", () => {
170
+ const zdt = plainDate.toZonedDateTime(timeZone);
171
+ fromZonedDateTime(zdt, "PlainDate", T);
172
+ }).run();
135
173
  });
136
174
 
137
- bench("ZonedDateTime → object", () => {
138
- const zdt = plainDate.toZonedDateTime(timeZone);
139
- fromZonedDateTime(zdt, "object", T);
175
+ test("ZonedDateTime → object", async ({ bench }) => {
176
+ await bench("ZonedDateTime → object", () => {
177
+ const zdt = plainDate.toZonedDateTime(timeZone);
178
+ fromZonedDateTime(zdt, "object", T);
179
+ }).run();
140
180
  });
141
181
 
142
- bench("round-trip PlainDate → ZDT → PlainDate", () => {
143
- const zdt = toZonedDateTime(
144
- { format: "PlainDate", value: plainDate },
145
- timeZone,
146
- T,
147
- );
148
- fromZonedDateTime(zdt, "PlainDate", T);
182
+ test("round-trip PlainDate → ZDT → PlainDate", async ({ bench }) => {
183
+ await bench("round-trip PlainDate → ZDT → PlainDate", () => {
184
+ const zdt = toZonedDateTime(
185
+ { format: "PlainDate", value: plainDate },
186
+ timeZone,
187
+ T,
188
+ );
189
+ fromZonedDateTime(zdt, "PlainDate", T);
190
+ }).run();
149
191
  });
150
192
  });
151
193
 
152
194
  describe("getWeekdayNames", () => {
153
- bench("en-US locale (7× toLocaleString)", () => {
154
- getWeekdayNames("en-US", T);
195
+ test("en-US locale (7× toLocaleString)", async ({ bench }) => {
196
+ await bench("en-US locale (7× toLocaleString)", () => {
197
+ getWeekdayNames("en-US", T);
198
+ }).run();
155
199
  });
156
200
 
157
- bench("de-DE locale", () => {
158
- getWeekdayNames("de-DE", T);
201
+ test("de-DE locale", async ({ bench }) => {
202
+ await bench("de-DE locale", () => {
203
+ getWeekdayNames("de-DE", T);
204
+ }).run();
159
205
  });
160
206
  });
161
207
 
@@ -165,43 +211,49 @@ describe("resolveFocusTarget", () => {
165
211
  const currentMonth = { year: 2026, month: 3 };
166
212
  const noDisabled = () => false;
167
213
 
168
- bench("focused date in grid (fast path)", () => {
169
- resolveFocusTarget(
170
- focusedDate,
171
- selectedDate,
172
- marchWeeks,
173
- currentMonth,
174
- noDisabled,
175
- T,
176
- true,
177
- );
178
- });
179
-
180
- bench("selected date fallback", () => {
181
- // Focus a date outside the grid to force fallback to selectedDate
182
- const outsideFocus = Temporal.PlainDate.from("2026-05-01");
183
- resolveFocusTarget(
184
- outsideFocus,
185
- selectedDate,
186
- marchWeeks,
187
- currentMonth,
188
- noDisabled,
189
- T,
190
- true,
191
- );
192
- });
193
-
194
- bench("no match — linear scan to first enabled", () => {
195
- const outsideFocus = Temporal.PlainDate.from("2026-05-01");
196
- const outsideSelected = Temporal.PlainDate.from("2026-05-15");
197
- resolveFocusTarget(
198
- outsideFocus,
199
- outsideSelected,
200
- marchWeeks,
201
- currentMonth,
202
- noDisabled,
203
- T,
204
- true,
205
- );
214
+ test("focused date in grid (fast path)", async ({ bench }) => {
215
+ await bench("focused date in grid (fast path)", () => {
216
+ resolveFocusTarget(
217
+ focusedDate,
218
+ selectedDate,
219
+ marchWeeks,
220
+ currentMonth,
221
+ noDisabled,
222
+ T,
223
+ true,
224
+ );
225
+ }).run();
226
+ });
227
+
228
+ test("selected date fallback", async ({ bench }) => {
229
+ await bench("selected date fallback", () => {
230
+ // Focus a date outside the grid to force fallback to selectedDate
231
+ const outsideFocus = Temporal.PlainDate.from("2026-05-01");
232
+ resolveFocusTarget(
233
+ outsideFocus,
234
+ selectedDate,
235
+ marchWeeks,
236
+ currentMonth,
237
+ noDisabled,
238
+ T,
239
+ true,
240
+ );
241
+ }).run();
242
+ });
243
+
244
+ test("no match — linear scan to first enabled", async ({ bench }) => {
245
+ await bench("no match — linear scan to first enabled", () => {
246
+ const outsideFocus = Temporal.PlainDate.from("2026-05-01");
247
+ const outsideSelected = Temporal.PlainDate.from("2026-05-15");
248
+ resolveFocusTarget(
249
+ outsideFocus,
250
+ outsideSelected,
251
+ marchWeeks,
252
+ currentMonth,
253
+ noDisabled,
254
+ T,
255
+ true,
256
+ );
257
+ }).run();
206
258
  });
207
259
  });
package/src/utils.test.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { Temporal } from "@js-temporal/polyfill";
2
- import { describe, it, expect } from "vitest";
2
+ import { describe, it, expect } from "vite-plus/test";
3
3
  import { temporalVariants } from "./test-temporal";
4
4
  import type { TemporalNamespace } from "./types";
5
5
  import {
@@ -8,6 +8,7 @@ import {
8
8
  toZonedDateTime,
9
9
  fromZonedDateTime,
10
10
  selectedToZdt,
11
+ resolveInitialAnchor,
11
12
  getMonthWeeks,
12
13
  zdtToNativeDate,
13
14
  sameCalendarDay,
@@ -249,6 +250,30 @@ describe("selectedToZdt", () => {
249
250
  });
250
251
  });
251
252
 
253
+ describe("resolveInitialAnchor", () => {
254
+ const today = () => Temporal.PlainDate.from("2026-04-01");
255
+
256
+ it.each([
257
+ {
258
+ description: "falls back to today when nothing is selected",
259
+ selected: undefined,
260
+ expected: "2026-04-01",
261
+ },
262
+ {
263
+ description: "uses the selected date over today",
264
+ selected: {
265
+ format: "PlainDate",
266
+ value: Temporal.PlainDate.from("2026-03-15"),
267
+ } as const,
268
+ expected: "2026-03-15",
269
+ },
270
+ ])("$description", ({ selected, expected }) => {
271
+ expect(resolveInitialAnchor(selected, today, "UTC", T).toString()).toBe(
272
+ expected,
273
+ );
274
+ });
275
+ });
276
+
252
277
  describe("zdtToNativeDate", () => {
253
278
  it("converts a ZonedDateTime to a native Date with the correct epoch", () => {
254
279
  const zdt = Temporal.PlainDateTime.from(
package/src/utils.ts CHANGED
@@ -39,6 +39,23 @@ export function getSystemTimeZone(T: TemporalNamespace): string {
39
39
  return T.Now.timeZoneId();
40
40
  }
41
41
 
42
+ /**
43
+ * The date a view opens on: the selected date, else today in the provider's time zone.
44
+ *
45
+ * @param selected - The currently selected value, or `undefined`.
46
+ * @param today - Reads today's date in the provider's time zone.
47
+ * @param timeZone - IANA time zone.
48
+ * @param T - Temporal namespace.
49
+ */
50
+ export function resolveInitialAnchor(
51
+ selected: DateValueObject | undefined,
52
+ today: () => Temporal.PlainDate,
53
+ timeZone: string,
54
+ T: TemporalNamespace,
55
+ ): Temporal.PlainDate {
56
+ return selectedToZdt(selected, timeZone, T)?.toPlainDate() ?? today();
57
+ }
58
+
42
59
  /**
43
60
  * Converts any {@link DateValueObject} into a `Temporal.ZonedDateTime`.
44
61
  *
@@ -575,7 +592,14 @@ export function getWeekdayNames(
575
592
  /**
576
593
  * Computes the ISO 8601 week number for a given date.
577
594
  *
578
- * The ISO week number is determined by the Thursday of the date's ISO week.
595
+ * The ISO week number is determined by the Thursday of the date's ISO week:
596
+ * a week belongs to the year that contains the majority of its days, i.e.
597
+ * the year containing its midpoint day. For 7-day weeks this is exactly
598
+ * ISO 8601 (Thursday rule, Jan 4 always in week 1).
599
+ *
600
+ * For calendars whose weeks have N ≠ 7 days, the same construction is
601
+ * generalized: the midpoint is day ⌈N/2⌉ of the week, and the anchor date
602
+ * that is always in week 1 is January (N − ⌈N/2⌉ + 1).
579
603
  *
580
604
  * @param date - The date to compute the week number for.
581
605
  * @param T - Temporal namespace.
@@ -584,12 +608,19 @@ export function getISOWeekNumber(
584
608
  date: Temporal.PlainDate,
585
609
  T: TemporalNamespace,
586
610
  ): number {
587
- // ISO dayOfWeek: 1=Mon...7=Sun
588
- // Find the Thursday of this ISO week
589
- const thursday = date.add({ days: 4 - date.dayOfWeek });
590
- // Jan 4 is always in ISO week 1
591
- const jan4 = T.PlainDate.from({ year: thursday.year, month: 1, day: 4 });
592
- const jan4Thursday = jan4.add({ days: 4 - jan4.dayOfWeek });
593
- const daysDiff = thursday.since(jan4Thursday, { largestUnit: "days" }).days;
594
- return Math.round(daysDiff / 7) + 1;
611
+ const daysInWeek = date.daysInWeek;
612
+ // Midpoint of the week: day ⌈N/2⌉, 1-indexed (Thursday for ISO weeks).
613
+ const midpoint = Math.ceil(daysInWeek / 2);
614
+ const midDay = date.add({ days: midpoint - date.dayOfWeek });
615
+ // Jan (N − midpoint + 1) is always in week 1 (Jan 4 for ISO weeks):
616
+ // it is the only date whose week is guaranteed to contain the year's
617
+ // first midpoint day.
618
+ const anchor = T.PlainDate.from({
619
+ year: midDay.year,
620
+ month: 1,
621
+ day: daysInWeek - midpoint + 1,
622
+ });
623
+ const anchorMidDay = anchor.add({ days: midpoint - anchor.dayOfWeek });
624
+ const daysDiff = midDay.since(anchorMidDay, { largestUnit: "days" }).days;
625
+ return Math.round(daysDiff / daysInWeek) + 1;
595
626
  }