@klinking/colander 3.1.0-alpha.6 → 3.1.0-alpha.7

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 (45) hide show
  1. package/dist/formats/date.cjs +1 -1
  2. package/dist/formats/date.d.cts +1 -1
  3. package/dist/formats/date.d.mts +1 -1
  4. package/dist/formats/date.mjs +1 -1
  5. package/dist/formats/object.cjs +1 -1
  6. package/dist/formats/object.d.cts +1 -1
  7. package/dist/formats/object.d.mts +1 -1
  8. package/dist/formats/object.mjs +1 -1
  9. package/dist/formats/plain-date-time.cjs +1 -1
  10. package/dist/formats/plain-date-time.d.cts +1 -1
  11. package/dist/formats/plain-date-time.d.mts +1 -1
  12. package/dist/formats/plain-date-time.mjs +1 -1
  13. package/dist/formats/plain-date.cjs +1 -1
  14. package/dist/formats/plain-date.d.cts +1 -1
  15. package/dist/formats/plain-date.d.mts +1 -1
  16. package/dist/formats/plain-date.mjs +1 -1
  17. package/dist/formats/plain-month-day.cjs +1 -1
  18. package/dist/formats/plain-month-day.d.cts +1 -1
  19. package/dist/formats/plain-month-day.d.mts +1 -1
  20. package/dist/formats/plain-month-day.mjs +1 -1
  21. package/dist/formats/plain-year-month.cjs +1 -1
  22. package/dist/formats/plain-year-month.d.cts +1 -1
  23. package/dist/formats/plain-year-month.d.mts +1 -1
  24. package/dist/formats/plain-year-month.mjs +1 -1
  25. package/dist/formats/zoned-date-time.cjs +1 -1
  26. package/dist/formats/zoned-date-time.d.cts +1 -1
  27. package/dist/formats/zoned-date-time.d.mts +1 -1
  28. package/dist/formats/zoned-date-time.mjs +1 -1
  29. package/dist/index.cjs +1 -1
  30. package/dist/index.d.cts +11 -2
  31. package/dist/index.d.mts +11 -2
  32. package/dist/index.mjs +1 -1
  33. package/dist/{weeks-navigation-Dn3UtG76.mjs → weeks-navigation-BSKwFvdP.mjs} +57 -78
  34. package/dist/{weeks-navigation-SuKJZJCM.cjs → weeks-navigation-CPevz3Xg.cjs} +57 -78
  35. package/dist/{index-DnFCKyyw.d.cts → weeks-view-context-BTojXkys.d.cts} +23 -16
  36. package/dist/{index-q3cQKJdA.d.mts → weeks-view-context-CFXV8orh.d.mts} +23 -16
  37. package/package.json +1 -1
  38. package/src/grid.tsx +1 -1
  39. package/src/out-of-range.test.ts +33 -53
  40. package/src/out-of-range.ts +47 -123
  41. package/src/weeks-navigation.tsx +15 -6
  42. package/src/weeks-view-state.ts +21 -12
  43. package/src/weeks-view.bounds.test.tsx +246 -0
  44. package/src/weeks-window.test.ts +109 -21
  45. package/src/weeks-window.ts +57 -19
@@ -1,10 +1,6 @@
1
1
  import { Temporal } from "@js-temporal/polyfill";
2
2
  import { describe, it, expect } from "vitest";
3
- import {
4
- applyOutOfRange,
5
- canShift,
6
- type OutOfRangeBehavior,
7
- } from "./out-of-range";
3
+ import { applyOutOfRange, type OutOfRangeBehavior } from "./out-of-range";
8
4
 
9
5
  const T = Temporal;
10
6
  const pd = (s: string) => T.PlainDate.from(s);
@@ -19,7 +15,7 @@ describe("applyOutOfRange", () => {
19
15
  targetFirstWeek: pd("2026-02-01"),
20
16
  weekCount: 8,
21
17
  behavior: "unbounded" as OutOfRangeBehavior,
22
- useMinMax: true,
18
+ useMinMax: "both" as const,
23
19
  expected: { firstWeek: "2026-02-01", weekCount: 8 },
24
20
  },
25
21
  {
@@ -27,7 +23,7 @@ describe("applyOutOfRange", () => {
27
23
  targetFirstWeek: pd("2025-12-07"),
28
24
  weekCount: 8,
29
25
  behavior: "stop" as OutOfRangeBehavior,
30
- useMinMax: true,
26
+ useMinMax: "both" as const,
31
27
  expected: { firstWeek: "2025-12-07", weekCount: 8 },
32
28
  },
33
29
  {
@@ -35,7 +31,7 @@ describe("applyOutOfRange", () => {
35
31
  targetFirstWeek: pd("2026-01-11"),
36
32
  weekCount: 8,
37
33
  behavior: "snap" as OutOfRangeBehavior,
38
- useMinMax: true,
34
+ useMinMax: "both" as const,
39
35
  expected: {
40
36
  firstWeek: "2025-11-16",
41
37
  weekCount: 8,
@@ -46,7 +42,7 @@ describe("applyOutOfRange", () => {
46
42
  targetFirstWeek: pd("2026-01-11"),
47
43
  weekCount: 8,
48
44
  behavior: "snap-shrink" as OutOfRangeBehavior,
49
- useMinMax: true,
45
+ useMinMax: "both" as const,
50
46
  expected: {
51
47
  firstWeek: "2025-11-30",
52
48
  weekCount: 6,
@@ -57,18 +53,42 @@ describe("applyOutOfRange", () => {
57
53
  targetFirstWeek: pd("2025-12-07"),
58
54
  weekCount: 8,
59
55
  behavior: "stop-shrink" as OutOfRangeBehavior,
60
- useMinMax: true,
56
+ useMinMax: "both" as const,
61
57
  expected: {
62
58
  firstWeek: "2025-12-07",
63
59
  weekCount: 5,
64
60
  },
65
61
  },
62
+ {
63
+ description: "max only, snap — pulls back when target overshoots max",
64
+ targetFirstWeek: pd("2026-01-11"),
65
+ weekCount: 8,
66
+ behavior: "snap" as OutOfRangeBehavior,
67
+ useMinMax: "max" as const,
68
+ expected: { firstWeek: "2025-11-16", weekCount: 8 },
69
+ },
70
+ {
71
+ description: "min only, snap — pulls forward when target undershoots min",
72
+ targetFirstWeek: pd("2025-10-01"),
73
+ weekCount: 8,
74
+ behavior: "snap" as OutOfRangeBehavior,
75
+ useMinMax: "min" as const,
76
+ expected: { firstWeek: "2025-11-30", weekCount: 8 },
77
+ },
78
+ {
79
+ description: "min only, stop-shrink — trims leading disabled rows",
80
+ targetFirstWeek: pd("2025-11-16"),
81
+ weekCount: 8,
82
+ behavior: "stop-shrink" as OutOfRangeBehavior,
83
+ useMinMax: "min" as const,
84
+ expected: { firstWeek: "2025-11-30", weekCount: 6 },
85
+ },
66
86
  {
67
87
  description: "no min/max — all modes behave as unbounded",
68
88
  targetFirstWeek: pd("2026-06-01"),
69
89
  weekCount: 8,
70
90
  behavior: "snap" as OutOfRangeBehavior,
71
- useMinMax: false,
91
+ useMinMax: "none" as const,
72
92
  expected: { firstWeek: "2026-06-01", weekCount: 8 },
73
93
  },
74
94
  ])(
@@ -78,7 +98,8 @@ describe("applyOutOfRange", () => {
78
98
  targetFirstWeek,
79
99
  weekCount,
80
100
  behavior,
81
- ...(useMinMax && { min, max }),
101
+ ...((useMinMax === "both" || useMinMax === "min") && { min }),
102
+ ...((useMinMax === "both" || useMinMax === "max") && { max }),
82
103
  weekStartDay: 0,
83
104
  T,
84
105
  });
@@ -87,44 +108,3 @@ describe("applyOutOfRange", () => {
87
108
  },
88
109
  );
89
110
  });
90
-
91
- describe("canShift", () => {
92
- const min = pd("2025-12-01");
93
- const max = pd("2026-01-10");
94
-
95
- it.each([
96
- {
97
- description: "unbounded — always true",
98
- behavior: "unbounded" as OutOfRangeBehavior,
99
- currentFirstWeek: pd("2026-06-01"),
100
- direction: 1 as const,
101
- expected: true,
102
- },
103
- {
104
- description: "stop — false when target has no valid weeks",
105
- behavior: "stop" as OutOfRangeBehavior,
106
- currentFirstWeek: pd("2025-12-28"),
107
- direction: 1 as const,
108
- expected: false,
109
- },
110
- {
111
- description: "snap — false when already at clamp position",
112
- behavior: "snap" as OutOfRangeBehavior,
113
- currentFirstWeek: pd("2025-11-16"),
114
- direction: 1 as const,
115
- expected: false,
116
- },
117
- ])("$description", ({ behavior, currentFirstWeek, direction, expected }) => {
118
- const result = canShift({
119
- currentFirstWeek,
120
- weekCount: 8,
121
- direction,
122
- behavior,
123
- min,
124
- max,
125
- weekStartDay: 0,
126
- T,
127
- });
128
- expect(result).toBe(expected);
129
- });
130
- });
@@ -12,7 +12,8 @@ import type { TemporalNamespace, WeekStartDay } from "./types";
12
12
  * - `"unbounded"` — Navigation is never restricted by `min`/`max`; any range of
13
13
  * weeks can be viewed. Out-of-range days simply render disabled. (Default.)
14
14
  * - `"stop"` — Navigation halts at the boundary: the Prev/Next button is
15
- * disabled once the next step would show a window containing no in-range day.
15
+ * disabled, and `goNext`/`goPrev`/`scrollToWeek` and keyboard paging do
16
+ * nothing, once the next step would show a window containing no in-range day.
16
17
  * - `"stop-shrink"` — Like `"stop"`, but near the edge the window **shrinks** to
17
18
  * fewer week rows instead of showing rows that are entirely out of range.
18
19
  * - `"snap"` — A jump that would overshoot **snaps** the window so its edge
@@ -20,6 +21,8 @@ import type { TemporalNamespace, WeekStartDay } from "./types";
20
21
  * - `"snap-shrink"` — Snap to the boundary, then **shrink** the window to just
21
22
  * the in-range weeks.
22
23
  *
24
+ * A single bound (`min` only or `max` only) is honored; the missing side is unlimited.
25
+ *
23
26
  * `"snap"` and `"snap-shrink"` are identical **unless the selectable range spans
24
27
  * fewer weeks than `weekCount`** — i.e. the window is taller than the range.
25
28
  * Snapping can only pin *one* edge to a bound; if the range is narrower than the
@@ -63,19 +66,33 @@ export type MonthOutOfRangeBehavior = "unbounded" | "stop";
63
66
  /** Returns true if any day in the 7-day span starting at weekStart falls within [min, max]. */
64
67
  function weekHasValidDay(
65
68
  weekStart: Temporal.PlainDate,
66
- min: Temporal.PlainDate,
67
- max: Temporal.PlainDate,
69
+ min: Temporal.PlainDate | undefined,
70
+ max: Temporal.PlainDate | undefined,
68
71
  T: TemporalNamespace,
69
72
  ): boolean {
70
73
  // Week spans [weekStart, weekStart+6]. Overlaps with [min, max] iff
71
- // weekStart <= max AND weekStart+6 >= min.
74
+ // weekStart <= max AND weekStart+6 >= min. A missing bound is unlimited.
72
75
  const weekEnd = weekStart.add({ days: 6 });
73
76
  return (
74
- T.PlainDate.compare(weekStart, max) <= 0 &&
75
- T.PlainDate.compare(weekEnd, min) >= 0
77
+ (!max || T.PlainDate.compare(weekStart, max) <= 0) &&
78
+ (!min || T.PlainDate.compare(weekEnd, min) >= 0)
76
79
  );
77
80
  }
78
81
 
82
+ /** Returns true if any of the `weekCount` weeks starting at firstWeek has a day in [min, max]. */
83
+ export function windowHasValidWeek(
84
+ firstWeek: Temporal.PlainDate,
85
+ weekCount: number,
86
+ min: Temporal.PlainDate | undefined,
87
+ max: Temporal.PlainDate | undefined,
88
+ T: TemporalNamespace,
89
+ ): boolean {
90
+ for (let i = 0; i < weekCount; i++) {
91
+ if (weekHasValidDay(firstWeek.add({ weeks: i }), min, max, T)) return true;
92
+ }
93
+ return false;
94
+ }
95
+
79
96
  /**
80
97
  * Returns the start of the week (per weekStartDay) that contains the given date.
81
98
  * Uses dayOfWeek % 7 → Sun=0…Sat=6 convention.
@@ -83,31 +100,12 @@ function weekHasValidDay(
83
100
  function weekStartContaining(
84
101
  date: Temporal.PlainDate,
85
102
  weekStartDay: WeekStartDay,
86
- _T: TemporalNamespace,
87
103
  ): Temporal.PlainDate {
88
104
  const dow = date.dayOfWeek % 7; // Sun=0…Sat=6
89
105
  const daysFromStart = (dow - weekStartDay + 7) % 7;
90
106
  return date.subtract({ days: daysFromStart });
91
107
  }
92
108
 
93
- /** Find the week start (per weekStartDay) whose 7-day span contains min. */
94
- function findFirstValidWeekStart(
95
- min: Temporal.PlainDate,
96
- weekStartDay: WeekStartDay,
97
- T: TemporalNamespace,
98
- ): Temporal.PlainDate {
99
- return weekStartContaining(min, weekStartDay, T);
100
- }
101
-
102
- /** Find the week start (per weekStartDay) whose 7-day span contains max. */
103
- function findLastValidWeekStart(
104
- max: Temporal.PlainDate,
105
- weekStartDay: WeekStartDay,
106
- T: TemporalNamespace,
107
- ): Temporal.PlainDate {
108
- return weekStartContaining(max, weekStartDay, T);
109
- }
110
-
111
109
  /**
112
110
  * Trim leading and trailing fully-disabled weeks from a window.
113
111
  * Returns new { firstWeek, weekCount }.
@@ -115,8 +113,8 @@ function findLastValidWeekStart(
115
113
  function shrinkWindow(
116
114
  firstWeek: Temporal.PlainDate,
117
115
  weekCount: number,
118
- min: Temporal.PlainDate,
119
- max: Temporal.PlainDate,
116
+ min: Temporal.PlainDate | undefined,
117
+ max: Temporal.PlainDate | undefined,
120
118
  T: TemporalNamespace,
121
119
  ): { firstWeek: Temporal.PlainDate; weekCount: number } {
122
120
  let start = 0;
@@ -174,13 +172,14 @@ export function applyOutOfRange({
174
172
  weekStartDay,
175
173
  T,
176
174
  }: ApplyOutOfRangeInput): { firstWeek: Temporal.PlainDate; weekCount: number } {
177
- // Without bounds, all modes behave as unbounded
178
- if (!min || !max || behavior === "unbounded") {
175
+ // Without bounds, all modes behave as unbounded. A single bound is honored;
176
+ // the missing side is unlimited.
177
+ if ((!min && !max) || behavior === "unbounded") {
179
178
  return { firstWeek: targetFirstWeek, weekCount };
180
179
  }
181
180
 
182
181
  if (behavior === "stop") {
183
- // Caller checks canShift; here just return the target as-is
182
+ // Whether the move is allowed is decided by the caller (see WeekWindowPolicy)
184
183
  return { firstWeek: targetFirstWeek, weekCount };
185
184
  }
186
185
 
@@ -188,100 +187,25 @@ export function applyOutOfRange({
188
187
  return shrinkWindow(targetFirstWeek, weekCount, min, max, T);
189
188
  }
190
189
 
191
- if (behavior === "snap" || behavior === "snap-shrink") {
192
- // Compute the snapped position: window ends at lastValidWeekStart
193
- const lastValid = findLastValidWeekStart(max, weekStartDay, T);
194
-
195
- // The snapped firstWeek positions the window so it ends at lastValid
196
- // i.e. firstWeek = lastValid - (weekCount - 1) * 7
197
- const snappedFirst = lastValid.subtract({ days: (weekCount - 1) * 7 });
198
-
199
- // Determine which firstWeek to use: if target is already within bounds
200
- // (doesn't overshoot max), use the target; otherwise snap.
201
- // "Overshoots max" means the target window extends past the last valid week.
202
- // The last week of the target window is targetFirstWeek + (weekCount-1)*7.
203
- const targetLastWeek = targetFirstWeek.add({ days: (weekCount - 1) * 7 });
204
-
205
- let resolvedFirst: Temporal.PlainDate;
206
- if (T.PlainDate.compare(targetLastWeek, lastValid) <= 0) {
207
- // Target window fits within max — also check if it goes before min
208
- const firstValid = findFirstValidWeekStart(min, weekStartDay, T);
209
- if (T.PlainDate.compare(targetFirstWeek, firstValid) >= 0) {
210
- resolvedFirst = targetFirstWeek;
211
- } else {
212
- // Overshoots min: snap forward so window starts at firstValid
213
- resolvedFirst = firstValid;
214
- }
215
- } else {
216
- // Overshoots max: snap back
217
- resolvedFirst = snappedFirst;
218
- }
219
-
220
- if (behavior === "snap-shrink") {
221
- return shrinkWindow(resolvedFirst, weekCount, min, max, T);
222
- }
223
- return { firstWeek: resolvedFirst, weekCount };
224
- }
225
-
226
- return { firstWeek: targetFirstWeek, weekCount };
227
- }
228
-
229
- export interface CanShiftInput {
230
- currentFirstWeek: Temporal.PlainDate;
231
- weekCount: number;
232
- direction: 1 | -1;
233
- shiftBy?: number;
234
- behavior: OutOfRangeBehavior;
235
- min?: Temporal.PlainDate;
236
- max?: Temporal.PlainDate;
237
- weekStartDay: WeekStartDay;
238
- T: TemporalNamespace;
239
- }
240
-
241
- /**
242
- * Returns whether the navigation button for the given direction should be enabled.
243
- */
244
- export function canShift({
245
- currentFirstWeek,
246
- weekCount,
247
- direction,
248
- shiftBy,
249
- behavior,
250
- min,
251
- max,
252
- weekStartDay,
253
- T,
254
- }: CanShiftInput): boolean {
255
- if (behavior === "unbounded" || !min || !max) {
256
- return true;
190
+ // snap | snap-shrink
191
+ const targetLastWeek = targetFirstWeek.add({ weeks: weekCount - 1 });
192
+ const lastValid = max && weekStartContaining(max, weekStartDay);
193
+ const firstValid = min && weekStartContaining(min, weekStartDay);
194
+
195
+ let resolvedFirst = targetFirstWeek;
196
+ if (lastValid && T.PlainDate.compare(targetLastWeek, lastValid) > 0) {
197
+ // Overshoots max: snap back so the window ends at the last in-range week
198
+ resolvedFirst = lastValid.subtract({ weeks: weekCount - 1 });
199
+ } else if (
200
+ firstValid &&
201
+ T.PlainDate.compare(targetFirstWeek, firstValid) < 0
202
+ ) {
203
+ // Overshoots min: snap forward so the window starts at the first in-range week
204
+ resolvedFirst = firstValid;
257
205
  }
258
206
 
259
- const shift = shiftBy ?? weekCount;
260
- const targetFirstWeek = currentFirstWeek.add({ days: direction * shift * 7 });
261
-
262
- if (behavior === "stop" || behavior === "stop-shrink") {
263
- // Check if any week in the target window has valid days
264
- for (let i = 0; i < weekCount; i++) {
265
- const ws = targetFirstWeek.add({ days: i * 7 });
266
- if (weekHasValidDay(ws, min, max, T)) return true;
267
- }
268
- return false;
207
+ if (behavior === "snap-shrink") {
208
+ return shrinkWindow(resolvedFirst, weekCount, min, max, T);
269
209
  }
270
-
271
- if (behavior === "snap" || behavior === "snap-shrink") {
272
- // Disabled when the current position is already the snapped position
273
- // (i.e. applying the out-of-range behavior to the target produces the same firstWeek)
274
- const applied = applyOutOfRange({
275
- targetFirstWeek,
276
- weekCount,
277
- behavior,
278
- min,
279
- max,
280
- weekStartDay,
281
- T,
282
- });
283
- return T.PlainDate.compare(applied.firstWeek, currentFirstWeek) !== 0;
284
- }
285
-
286
- return true;
210
+ return { firstWeek: resolvedFirst, weekCount };
287
211
  }
@@ -74,16 +74,25 @@ function useWeeksNavButton(
74
74
  const { windowInfo } = useWeeksViewState();
75
75
  const { rootState } = useViewState();
76
76
 
77
- const currentFirstWeek = windowInfo.windowStart;
77
+ const { windowStart, weekCount } = windowInfo;
78
78
 
79
79
  const isDisabled = useMemo(() => {
80
80
  if (globalDisabled) return true;
81
- return !windowPolicy.canShift(
82
- currentFirstWeek,
83
- direction === "next" ? 1 : -1,
84
- shiftByProp,
81
+ return (
82
+ windowPolicy.shift(
83
+ { firstWeek: windowStart, weekCount },
84
+ direction === "next" ? 1 : -1,
85
+ shiftByProp,
86
+ ) === null
85
87
  );
86
- }, [globalDisabled, windowPolicy, currentFirstWeek, direction, shiftByProp]);
88
+ }, [
89
+ globalDisabled,
90
+ windowPolicy,
91
+ windowStart,
92
+ weekCount,
93
+ direction,
94
+ shiftByProp,
95
+ ]);
87
96
 
88
97
  const state = useMemo<WeeksNavButtonState>(
89
98
  () => ({ root: rootState, direction, disabled: isDisabled }),
@@ -307,22 +307,30 @@ export function useWeeksViewRootState(props: WeeksViewRootProps) {
307
307
  [isControlled, onFirstWeekChange],
308
308
  );
309
309
 
310
+ // Moves start from the window as shown (post snap/shrink), not the requested
311
+ // first week, so they agree with what the Prev/Next buttons report.
312
+ const shownWindow = useMemo(
313
+ () => ({
314
+ firstWeek: windowInfo.windowStart,
315
+ weekCount: windowInfo.weekCount,
316
+ }),
317
+ [windowInfo.windowStart, windowInfo.weekCount],
318
+ );
319
+
310
320
  const goNext = useCallback(
311
321
  (shiftBy?: number) => {
312
- const target = resolvedFirstWeek.add({ weeks: shiftBy ?? weekCount });
313
- updateFirstWeek(windowPolicy.resolve(target).firstWeek);
322
+ const next = windowPolicy.shift(shownWindow, 1, shiftBy);
323
+ if (next) updateFirstWeek(next.firstWeek);
314
324
  },
315
- [resolvedFirstWeek, weekCount, windowPolicy, updateFirstWeek],
325
+ [shownWindow, windowPolicy, updateFirstWeek],
316
326
  );
317
327
 
318
328
  const goPrev = useCallback(
319
329
  (shiftBy?: number) => {
320
- const target = resolvedFirstWeek.subtract({
321
- weeks: shiftBy ?? weekCount,
322
- });
323
- updateFirstWeek(windowPolicy.resolve(target).firstWeek);
330
+ const next = windowPolicy.shift(shownWindow, -1, shiftBy);
331
+ if (next) updateFirstWeek(next.firstWeek);
324
332
  },
325
- [resolvedFirstWeek, weekCount, windowPolicy, updateFirstWeek],
333
+ [shownWindow, windowPolicy, updateFirstWeek],
326
334
  );
327
335
 
328
336
  // --- scrollToWeek ---
@@ -331,15 +339,16 @@ export function useWeeksViewRootState(props: WeeksViewRootProps) {
331
339
  target: FirstWeekSpec,
332
340
  options?: { snap?: "start" | "center" | "end" | "nearest" },
333
341
  ) => {
334
- const newFirstWeek = resolveFirstWeek(
335
- resolvedFirstWeek,
342
+ const requested = resolveFirstWeek(
343
+ shownWindow.firstWeek,
336
344
  weekCount,
337
345
  resolveSpec(target),
338
346
  options,
339
347
  );
340
- updateFirstWeek(windowPolicy.resolve(newFirstWeek).firstWeek);
348
+ const next = windowPolicy.moveTo(requested);
349
+ if (next) updateFirstWeek(next.firstWeek);
341
350
  },
342
- [resolvedFirstWeek, weekCount, resolveSpec, windowPolicy, updateFirstWeek],
351
+ [shownWindow, weekCount, resolveSpec, windowPolicy, updateFirstWeek],
343
352
  );
344
353
 
345
354
  // --- Fire onWindowChange ---