@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
@@ -1,6 +1,13 @@
1
1
  import { Temporal } from "@js-temporal/polyfill";
2
- import { render, screen, act, cleanup, within } from "@testing-library/react";
3
- import { describe, it, expect, vi, afterEach } from "vitest";
2
+ import {
3
+ render,
4
+ screen,
5
+ act,
6
+ cleanup,
7
+ fireEvent,
8
+ within,
9
+ } from "@testing-library/react";
10
+ import { describe, it, expect, vi, afterEach } from "vite-plus/test";
4
11
  import {
5
12
  Grid,
6
13
  GridBody,
@@ -16,7 +23,7 @@ import {
16
23
  MonthSeparatorWeekCount,
17
24
  } from "./month-separator";
18
25
  import { useMonthViewState } from "./month-view-context";
19
- import { useViewContext } from "./view-context";
26
+ import { useViewContext, useViewState } from "./view-context";
20
27
  import { NextWeeksButton, PrevWeeksButton } from "./weeks-navigation";
21
28
  import { WeeksView } from "./weeks-view";
22
29
  import { useWeeksViewState, useWeeksViewStable } from "./weeks-view-context";
@@ -538,21 +545,19 @@ describe("WeeksView regression: goNext/goPrev shift by weekCount (Bug #3)", () =
538
545
  });
539
546
  });
540
547
 
541
- describe("WeeksView regression: rootState shim is populated", () => {
542
- function ShimRootStateCapture({
548
+ describe("WeeksView regression: rootState is populated", () => {
549
+ function RootStateCapture({
543
550
  onCapture,
544
551
  }: {
545
- onCapture: (
546
- root: ReturnType<typeof useMonthViewState>["rootState"],
547
- ) => void;
552
+ onCapture: (root: ReturnType<typeof useViewState>["rootState"]) => void;
548
553
  }) {
549
- const { rootState } = useMonthViewState();
554
+ const { rootState } = useViewState();
550
555
  onCapture(rootState);
551
556
  return null;
552
557
  }
553
558
 
554
- it("exposes a real rootState to components reading useMonthViewState()", () => {
555
- let root: ReturnType<typeof useMonthViewState>["rootState"] | undefined;
559
+ it("exposes a real rootState to components reading useViewState()", () => {
560
+ let root: ReturnType<typeof useViewState>["rootState"] | undefined;
556
561
  render(
557
562
  <WeeksView
558
563
  temporal={T}
@@ -560,7 +565,7 @@ describe("WeeksView regression: rootState shim is populated", () => {
560
565
  defaultFirstWeek={T.PlainDate.from("2026-03-01")}
561
566
  defaultValue={T.PlainDate.from("2026-03-15")}
562
567
  >
563
- <ShimRootStateCapture
568
+ <RootStateCapture
564
569
  onCapture={(r) => {
565
570
  root = r;
566
571
  }}
@@ -568,7 +573,6 @@ describe("WeeksView regression: rootState shim is populated", () => {
568
573
  </WeeksView>,
569
574
  );
570
575
 
571
- // Pre-fix this shim was `{} as RootState` — every field undefined at runtime.
572
576
  expect(root).toBeDefined();
573
577
  expect(root!.viewing).toBeInstanceOf(Temporal.PlainYearMonth);
574
578
  expect(root!.focused).toBeInstanceOf(Temporal.PlainDate);
@@ -576,7 +580,7 @@ describe("WeeksView regression: rootState shim is populated", () => {
576
580
  expect(root!.timeZone.length).toBeGreaterThan(0);
577
581
  expect(typeof root!.locale).toBe("string");
578
582
  expect(typeof root!.readOnly).toBe("boolean");
579
- // Selection flows through the shared baseRootState into the shim.
583
+ // Selection flows through the shared baseRootState.
580
584
  expect(root!.hasSelection).toBe(true);
581
585
  expect(root!.selected).toBeInstanceOf(Temporal.PlainDate);
582
586
  expect(root!.selected!.toString()).toBe("2026-03-15");
@@ -585,3 +589,101 @@ describe("WeeksView regression: rootState shim is populated", () => {
585
589
  ).toBe(true);
586
590
  });
587
591
  });
592
+
593
+ describe("WeeksView does not provide MonthView contexts", () => {
594
+ function MonthStateProbe() {
595
+ useMonthViewState();
596
+ return null;
597
+ }
598
+
599
+ it("useMonthViewState throws instead of returning placeholder values", () => {
600
+ const error = vi.spyOn(console, "error").mockImplementation(() => {});
601
+ expect(() =>
602
+ render(
603
+ <WeeksView temporal={T} weekCount={4}>
604
+ <MonthStateProbe />
605
+ </WeeksView>,
606
+ ),
607
+ ).toThrow();
608
+ error.mockRestore();
609
+ });
610
+ });
611
+
612
+ describe("WeeksView: Grid renders the weeks grid shell", () => {
613
+ function renderWeeksGrid() {
614
+ return render(
615
+ <WeeksView
616
+ temporal={T}
617
+ weekCount={4}
618
+ defaultFirstWeek={T.PlainDate.from("2026-03-01")}
619
+ defaultValue={T.PlainDate.from("2026-03-15")}
620
+ >
621
+ <FocusDisplay />
622
+ <WindowDisplay />
623
+ <Grid>
624
+ <GridBody>
625
+ <WeekTemplate>
626
+ <DayCellTemplate>
627
+ <DayButton />
628
+ </DayCellTemplate>
629
+ </WeekTemplate>
630
+ </GridBody>
631
+ </Grid>
632
+ </WeeksView>,
633
+ );
634
+ }
635
+
636
+ it("reports the window's week count on the grid", () => {
637
+ const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
638
+ renderWeeksGrid();
639
+
640
+ const grid = screen.getByRole("grid");
641
+ expect(grid.getAttribute("data-weeks-in-month")).toBe("4");
642
+ expect(grid.style.getPropertyValue("--calendar-weeks-in-month")).toBe("4");
643
+ expect(warn).not.toHaveBeenCalled();
644
+ warn.mockRestore();
645
+ });
646
+
647
+ it.each([
648
+ {
649
+ description: "PageDown moves focus by weekCount weeks",
650
+ key: "PageDown",
651
+ shiftKey: false,
652
+ expected: { focus: "2026-04-12" },
653
+ },
654
+ {
655
+ description: "Shift+PageDown moves focus by a year",
656
+ key: "PageDown",
657
+ shiftKey: true,
658
+ expected: { focus: "2027-03-15" },
659
+ },
660
+ ])("$description", ({ key, shiftKey, expected }) => {
661
+ renderWeeksGrid();
662
+
663
+ fireEvent.keyDown(screen.getByRole("grid"), { key, shiftKey });
664
+
665
+ expect(screen.getByTestId("focus").textContent).toBe(expected.focus);
666
+ });
667
+
668
+ it("moves DOM focus to the newly focused day after an arrow key", () => {
669
+ renderWeeksGrid();
670
+ const start = screen.getByRole("button", { name: /15/ });
671
+ act(() => start.focus());
672
+
673
+ fireEvent.keyDown(start, { key: "ArrowRight" });
674
+
675
+ expect(screen.getByTestId("focus").textContent).toBe("2026-03-16");
676
+ expect(document.activeElement).toBe(
677
+ screen.getByRole("button", { name: /16/ }),
678
+ );
679
+ });
680
+
681
+ it("End moves focus to the last day of the window and keeps the window", () => {
682
+ renderWeeksGrid();
683
+
684
+ fireEvent.keyDown(screen.getByRole("grid"), { key: "End" });
685
+
686
+ expect(screen.getByTestId("focus").textContent).toBe("2026-03-28");
687
+ expect(screen.getByTestId("start").textContent).toBe("2026-03-01");
688
+ });
689
+ });
@@ -1,14 +1,14 @@
1
- import type { Temporal } from "@js-temporal/polyfill";
2
- import React, { forwardRef, useImperativeHandle, useMemo, useRef } from "react";
1
+ import React, { forwardRef, useImperativeHandle } from "react";
3
2
  import { CalendarProvider } from "./calendar-provider";
4
3
  import type { CalendarProviderProps } from "./calendar-types";
5
- import {
6
- MonthViewStableContext,
7
- MonthViewStateContext,
8
- } from "./month-view-context";
4
+ import { DayCellStoreProvider } from "./day-cell-store";
9
5
  import type { FirstWeekSpec, ScrollToWeekSnap } from "./resolve-first-week";
10
6
  import type { ValueFormat } from "./types";
11
- import { ViewContext } from "./view-context";
7
+ import {
8
+ ViewContext,
9
+ ViewStableContext,
10
+ ViewStateContext,
11
+ } from "./view-context";
12
12
  import {
13
13
  WeeksViewStableContext,
14
14
  WeeksViewStateContext,
@@ -33,57 +33,29 @@ function WeeksViewRootFn(
33
33
  ref: React.ForwardedRef<WeeksViewRootHandle>,
34
34
  ) {
35
35
  const { children } = props;
36
- const { stableCtx, stateCtx, viewCtx, scrollToWeek } =
37
- useWeeksViewRootState(props);
36
+ const {
37
+ stableCtx,
38
+ stateCtx,
39
+ viewCtx,
40
+ viewStableCtx,
41
+ viewStateCtx,
42
+ scrollToWeek,
43
+ } = useWeeksViewRootState(props);
38
44
 
39
45
  useImperativeHandle(ref, () => ({ scrollToWeek }), [scrollToWeek]);
40
46
 
41
- const gridFocusedRef = useRef(false);
42
-
43
- // Provide MonthViewStableContext/MonthViewStateContext shims so shared
44
- // components (WeeksNavButton, WeekCount, etc.) that read rootState via
45
- // useMonthViewState() can function outside the Grid in WeeksView.
46
- const monthViewStableShim = useMemo(
47
- () => ({
48
- numberOfMonths: 1,
49
- fixedWeeks: false,
50
- outsideDays: "enabled" as const,
51
- outOfRangeBehavior: "unbounded" as const,
52
- goNextMonth: () => {},
53
- goPrevMonth: () => {},
54
- setGridLabelId: () => {},
55
- gridFocusedRef,
56
- }),
57
- [],
58
- );
59
-
60
- const monthViewStateShim = useMemo(
61
- () => ({
62
- currentMonth: {
63
- year: stateCtx.currentDateTime.year,
64
- month: stateCtx.currentDateTime.month,
65
- },
66
- weeks: [] as Temporal.PlainDate[][],
67
- allMonths: [],
68
- currentDateTime: stateCtx.currentDateTime,
69
- gridLabelIds: stateCtx.gridLabelIds,
70
- rootState: stateCtx.rootState,
71
- }),
72
- [stateCtx.currentDateTime, stateCtx.gridLabelIds, stateCtx.rootState],
73
- );
74
-
75
47
  return (
76
- <MonthViewStableContext.Provider value={monthViewStableShim}>
77
- <MonthViewStateContext.Provider value={monthViewStateShim}>
78
- <WeeksViewStableContext.Provider value={stableCtx}>
79
- <WeeksViewStateContext.Provider value={stateCtx}>
48
+ <WeeksViewStableContext.Provider value={stableCtx}>
49
+ <WeeksViewStateContext.Provider value={stateCtx}>
50
+ <ViewStableContext.Provider value={viewStableCtx}>
51
+ <ViewStateContext.Provider value={viewStateCtx}>
80
52
  <ViewContext.Provider value={viewCtx}>
81
- {children}
53
+ <DayCellStoreProvider>{children}</DayCellStoreProvider>
82
54
  </ViewContext.Provider>
83
- </WeeksViewStateContext.Provider>
84
- </WeeksViewStableContext.Provider>
85
- </MonthViewStateContext.Provider>
86
- </MonthViewStableContext.Provider>
55
+ </ViewStateContext.Provider>
56
+ </ViewStableContext.Provider>
57
+ </WeeksViewStateContext.Provider>
58
+ </WeeksViewStableContext.Provider>
87
59
  );
88
60
  }
89
61
 
@@ -0,0 +1,131 @@
1
+ import { Temporal } from "@js-temporal/polyfill";
2
+ import { describe, it, expect } from "vite-plus/test";
3
+ import type { OutOfRangeBehavior } from "./out-of-range";
4
+ import { createWeekWindowPolicy } from "./weeks-window";
5
+
6
+ const T = Temporal;
7
+ const pd = (s: string) => T.PlainDate.from(s);
8
+
9
+ function policyFor(
10
+ behavior: OutOfRangeBehavior,
11
+ bounds: { min?: string; max?: string } = {
12
+ min: "2025-12-01",
13
+ max: "2026-01-10",
14
+ },
15
+ ) {
16
+ return createWeekWindowPolicy({
17
+ weekCount: 8,
18
+ behavior,
19
+ min: bounds.min ? pd(bounds.min) : undefined,
20
+ max: bounds.max ? pd(bounds.max) : undefined,
21
+ weekStartDay: 0,
22
+ T,
23
+ });
24
+ }
25
+
26
+ const shown = (firstWeek: string, weekCount = 8) => ({
27
+ firstWeek: pd(firstWeek),
28
+ weekCount,
29
+ });
30
+
31
+ describe("WeekWindowPolicy.shift", () => {
32
+ it.each([
33
+ {
34
+ description: "unbounded — always allowed",
35
+ behavior: "unbounded" as const,
36
+ from: shown("2026-06-01"),
37
+ direction: 1 as const,
38
+ expected: { allowed: true },
39
+ },
40
+ {
41
+ description: "stop — refused when the target window has no in-range week",
42
+ behavior: "stop" as const,
43
+ from: shown("2025-12-28"),
44
+ direction: 1 as const,
45
+ expected: { allowed: false },
46
+ },
47
+ {
48
+ description:
49
+ "stop-shrink — refused when the target window has no in-range week",
50
+ behavior: "stop-shrink" as const,
51
+ from: shown("2025-12-28"),
52
+ direction: 1 as const,
53
+ expected: { allowed: false },
54
+ },
55
+ {
56
+ description: "snap — refused when already at the clamp position",
57
+ behavior: "snap" as const,
58
+ from: shown("2025-11-16"),
59
+ direction: 1 as const,
60
+ expected: { allowed: false },
61
+ },
62
+ {
63
+ description: "snap — allowed away from the clamp position",
64
+ behavior: "snap" as const,
65
+ from: shown("2025-11-16"),
66
+ direction: -1 as const,
67
+ expected: { allowed: true },
68
+ },
69
+ ])("$description", ({ behavior, from, direction, expected }) => {
70
+ expect(policyFor(behavior).shift(from, direction) !== null).toBe(
71
+ expected.allowed,
72
+ );
73
+ });
74
+
75
+ it("shifts by the rows currently shown, not the weekCount prop", () => {
76
+ const next = policyFor("stop-shrink", {
77
+ min: "2025-01-01",
78
+ max: "2026-03-14",
79
+ }).shift(shown("2026-03-01", 2), -1);
80
+ expect(next?.firstWeek.toString()).toBe("2026-02-15");
81
+ });
82
+
83
+ it.each([
84
+ {
85
+ description: "min only, stop — Prev refused below the bound",
86
+ behavior: "stop" as const,
87
+ bounds: { min: "2026-03-01" },
88
+ direction: -1 as const,
89
+ expected: { allowed: false },
90
+ },
91
+ {
92
+ description: "max only, stop — Next refused past the bound",
93
+ behavior: "stop" as const,
94
+ bounds: { max: "2026-03-14" },
95
+ direction: 1 as const,
96
+ expected: { allowed: false },
97
+ },
98
+ ])("$description", ({ behavior, bounds, direction, expected }) => {
99
+ expect(
100
+ policyFor(behavior, bounds).shift(shown("2026-03-01"), direction) !==
101
+ null,
102
+ ).toBe(expected.allowed);
103
+ });
104
+ });
105
+
106
+ describe("WeekWindowPolicy.moveTo", () => {
107
+ it.each([
108
+ {
109
+ description: "stop — refused when no row is in range",
110
+ behavior: "stop" as const,
111
+ target: "2026-06-07",
112
+ expected: null,
113
+ },
114
+ {
115
+ description: "stop — allowed when part of the window is in range",
116
+ behavior: "stop" as const,
117
+ target: "2026-01-04",
118
+ expected: "2026-01-04",
119
+ },
120
+ {
121
+ description: "snap — pulls back instead of refusing",
122
+ behavior: "snap" as const,
123
+ target: "2026-06-07",
124
+ expected: "2025-11-16",
125
+ },
126
+ ])("$description", ({ behavior, target, expected }) => {
127
+ expect(
128
+ policyFor(behavior).moveTo(pd(target))?.firstWeek.toString() ?? null,
129
+ ).toBe(expected);
130
+ });
131
+ });
@@ -0,0 +1,102 @@
1
+ import type { Temporal } from "@js-temporal/polyfill";
2
+ import {
3
+ applyOutOfRange,
4
+ windowHasValidWeek,
5
+ type OutOfRangeBehavior,
6
+ } from "./out-of-range";
7
+ import type { TemporalNamespace, WeekStartDay } from "./types";
8
+
9
+ /** Inputs that determine how a weeks window may move relative to `min`/`max`. */
10
+ export interface WeekWindowPolicyInput {
11
+ /** The `weekCount` prop: the most rows a window will ever show. */
12
+ weekCount: number;
13
+ behavior: OutOfRangeBehavior;
14
+ min: Temporal.PlainDate | undefined;
15
+ max: Temporal.PlainDate | undefined;
16
+ weekStartDay: WeekStartDay;
17
+ T: TemporalNamespace;
18
+ }
19
+
20
+ /** A window as shown: its first week and how many rows it has (≤ the prop). */
21
+ export interface EffectiveWindow {
22
+ firstWeek: Temporal.PlainDate;
23
+ weekCount: number;
24
+ }
25
+
26
+ /**
27
+ * The rules for where a WeeksView window may sit. Built once from the bounds and
28
+ * shared by every way of moving the window (buttons, keyboard, `scrollToWeek`).
29
+ */
30
+ export interface WeekWindowPolicy {
31
+ /** The window that is shown for a requested first week. Never refuses. */
32
+ resolve(requestedFirstWeek: Temporal.PlainDate): EffectiveWindow;
33
+ /**
34
+ * The window to move to for a requested first week, or `null` when the
35
+ * behavior forbids the move (`stop`/`stop-shrink` with no in-range day in it).
36
+ */
37
+ moveTo(requestedFirstWeek: Temporal.PlainDate): EffectiveWindow | null;
38
+ /**
39
+ * Moves by `shiftBy` weeks (default: the rows currently shown) from the
40
+ * window as shown. `null` when the move is forbidden or would not change
41
+ * the window (`snap` already at the edge).
42
+ */
43
+ shift(
44
+ current: EffectiveWindow,
45
+ direction: 1 | -1,
46
+ shiftBy?: number,
47
+ ): EffectiveWindow | null;
48
+ }
49
+
50
+ export function createWeekWindowPolicy({
51
+ weekCount,
52
+ behavior,
53
+ min,
54
+ max,
55
+ weekStartDay,
56
+ T,
57
+ }: WeekWindowPolicyInput): WeekWindowPolicy {
58
+ const bounds = {
59
+ weekCount,
60
+ behavior,
61
+ ...(min !== undefined && { min }),
62
+ ...(max !== undefined && { max }),
63
+ weekStartDay,
64
+ T,
65
+ };
66
+ const isStop = behavior === "stop" || behavior === "stop-shrink";
67
+ const isBounded = min !== undefined || max !== undefined;
68
+
69
+ const resolve: WeekWindowPolicy["resolve"] = (requestedFirstWeek) =>
70
+ applyOutOfRange({ ...bounds, targetFirstWeek: requestedFirstWeek });
71
+
72
+ const moveTo: WeekWindowPolicy["moveTo"] = (requestedFirstWeek) => {
73
+ if (
74
+ isStop &&
75
+ isBounded &&
76
+ !windowHasValidWeek(requestedFirstWeek, weekCount, min, max, T)
77
+ ) {
78
+ return null;
79
+ }
80
+ return resolve(requestedFirstWeek);
81
+ };
82
+
83
+ return {
84
+ resolve,
85
+ moveTo,
86
+ shift: (current, direction, shiftBy) => {
87
+ const by = shiftBy ?? current.weekCount;
88
+ const target = current.firstWeek.add({ weeks: direction * by });
89
+ const next = moveTo(target);
90
+ if (next === null) return null;
91
+ const isSnap = behavior === "snap" || behavior === "snap-shrink";
92
+ if (
93
+ isSnap &&
94
+ isBounded &&
95
+ T.PlainDate.compare(next.firstWeek, current.firstWeek) === 0
96
+ ) {
97
+ return null;
98
+ }
99
+ return next;
100
+ },
101
+ };
102
+ }