@klinking/colander 3.1.0-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.
- package/dist/formats/date.cjs +2 -3
- package/dist/formats/date.d.cts +10 -14
- package/dist/formats/date.d.mts +10 -14
- package/dist/formats/date.mjs +2 -3
- package/dist/formats/object.cjs +2 -3
- package/dist/formats/object.d.cts +10 -14
- package/dist/formats/object.d.mts +10 -14
- package/dist/formats/object.mjs +2 -3
- package/dist/formats/plain-date-time.cjs +2 -3
- package/dist/formats/plain-date-time.d.cts +10 -14
- package/dist/formats/plain-date-time.d.mts +10 -14
- package/dist/formats/plain-date-time.mjs +2 -3
- package/dist/formats/plain-date.cjs +2 -3
- package/dist/formats/plain-date.d.cts +10 -14
- package/dist/formats/plain-date.d.mts +10 -14
- package/dist/formats/plain-date.mjs +2 -3
- package/dist/formats/plain-month-day.cjs +2 -3
- package/dist/formats/plain-month-day.d.cts +10 -14
- package/dist/formats/plain-month-day.d.mts +10 -14
- package/dist/formats/plain-month-day.mjs +2 -3
- package/dist/formats/plain-year-month.cjs +2 -3
- package/dist/formats/plain-year-month.d.cts +10 -14
- package/dist/formats/plain-year-month.d.mts +10 -14
- package/dist/formats/plain-year-month.mjs +2 -3
- package/dist/formats/zoned-date-time.cjs +2 -3
- package/dist/formats/zoned-date-time.d.cts +10 -14
- package/dist/formats/zoned-date-time.d.mts +10 -14
- package/dist/formats/zoned-date-time.mjs +2 -3
- package/dist/{weeks-view-context-CNAq2Hiu.d.cts → index-K1_zOIpa.d.mts} +198 -114
- package/dist/{weeks-view-context-CsP3PBiu.d.mts → index-bRoYX03d.d.cts} +198 -114
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +2 -11
- package/dist/index.d.mts +2 -11
- package/dist/index.mjs +1 -2
- package/dist/{weeks-navigation-CJzhxbgj.cjs → weeks-navigation-3UTRiTUP.cjs} +1102 -952
- package/dist/{weeks-navigation-Bg0nZLgh.mjs → weeks-navigation-BoHqK5fq.mjs} +1102 -951
- package/package.json +21 -18
- package/src/calendar-provider.test.tsx +88 -1
- package/src/calendar-provider.tsx +89 -73
- package/src/calendar-types.ts +2 -0
- package/src/compute-weeks-in-window.test.ts +1 -1
- package/src/day-cell-state.test.ts +214 -0
- package/src/day-cell-state.ts +244 -0
- package/src/day-cell-store.test.tsx +273 -0
- package/src/day-cell-store.tsx +204 -0
- package/src/day-cell.test.tsx +73 -90
- package/src/day-cell.tsx +106 -444
- package/src/drag-handle.tsx +2 -2
- package/src/formats/date.ts +2 -0
- package/src/formats/formats.test.ts +1 -1
- package/src/formats/object.ts +2 -0
- package/src/formats/plain-date-time.ts +2 -0
- package/src/formats/plain-date.ts +2 -0
- package/src/formats/plain-month-day.ts +2 -0
- package/src/formats/plain-year-month.ts +2 -0
- package/src/formats/zoned-date-time.ts +2 -0
- package/src/grid-header.tsx +3 -3
- package/src/grid.event-perf.test.tsx +7 -1
- package/src/grid.perf.test.tsx +7 -1
- package/src/grid.tsx +38 -77
- package/src/index.ts +2 -0
- package/src/keyboard.test.ts +1 -1
- package/src/month-separator.test.tsx +1 -1
- package/src/month-view-navigation.ts +180 -0
- package/src/month-view-types.ts +3 -0
- package/src/month-view.bounds.test.tsx +136 -0
- package/src/month-view.test.tsx +89 -1
- package/src/month-view.tsx +74 -217
- package/src/month-window.test.ts +207 -0
- package/src/month-window.ts +93 -0
- package/src/navigation.tsx +18 -76
- package/src/out-of-range.test.ts +34 -54
- package/src/out-of-range.ts +47 -123
- package/src/range-preview.test.ts +120 -0
- package/src/range-preview.tsx +3 -5
- package/src/resolve-first-week.test.ts +1 -1
- package/src/root-selection.ts +99 -97
- package/src/selected-range.tsx +3 -5
- package/src/store.ts +72 -0
- package/src/today.test.tsx +105 -0
- package/src/types.ts +24 -1
- package/src/utils.bench.ts +158 -106
- package/src/utils.test.ts +26 -1
- package/src/utils.ts +40 -9
- package/src/view-context.test.tsx +126 -0
- package/src/view-context.ts +45 -0
- package/src/week-number.test.tsx +76 -0
- package/src/week-number.tsx +48 -19
- package/src/weeks-grid.tsx +20 -10
- package/src/weeks-keyboard.test.ts +1 -1
- package/src/weeks-navigation.tsx +15 -31
- package/src/weeks-view-state.ts +67 -101
- package/src/weeks-view-types.ts +4 -1
- package/src/weeks-view.bounds.test.tsx +246 -0
- package/src/weeks-view.test.tsx +116 -14
- package/src/weeks-view.tsx +24 -52
- package/src/weeks-window.test.ts +131 -0
- 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 = {
|
package/src/utils.bench.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Temporal } from "@js-temporal/polyfill";
|
|
2
|
-
import {
|
|
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
|
-
|
|
33
|
-
|
|
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
|
-
|
|
37
|
-
|
|
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
|
-
|
|
41
|
-
|
|
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
|
-
|
|
47
|
-
|
|
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
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
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
|
-
|
|
61
|
-
|
|
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
|
-
|
|
65
|
-
|
|
66
|
-
|
|
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
|
-
|
|
73
|
-
|
|
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
|
-
|
|
77
|
-
|
|
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
|
-
|
|
81
|
-
|
|
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
|
-
|
|
85
|
-
|
|
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
|
-
|
|
93
|
-
|
|
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
|
-
|
|
97
|
-
|
|
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
|
-
|
|
101
|
-
|
|
102
|
-
|
|
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
|
-
|
|
107
|
-
|
|
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
|
-
|
|
117
|
-
|
|
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
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
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
|
-
|
|
129
|
-
|
|
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
|
-
|
|
133
|
-
|
|
134
|
-
|
|
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
|
-
|
|
138
|
-
|
|
139
|
-
|
|
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
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
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
|
-
|
|
154
|
-
|
|
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
|
-
|
|
158
|
-
|
|
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
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
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 "
|
|
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
|
-
|
|
588
|
-
//
|
|
589
|
-
const
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
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
|
}
|