@zag-js/date-utils 1.34.1 → 1.35.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. package/dist/align.d.mts +8 -0
  2. package/dist/align.d.ts +8 -0
  3. package/dist/align.js +52 -0
  4. package/dist/align.mjs +26 -0
  5. package/dist/assertion.d.mts +10 -0
  6. package/dist/assertion.d.ts +10 -0
  7. package/dist/assertion.js +58 -0
  8. package/dist/assertion.mjs +29 -0
  9. package/dist/constrain.d.mts +9 -0
  10. package/dist/constrain.d.ts +9 -0
  11. package/dist/constrain.js +109 -0
  12. package/dist/constrain.mjs +87 -0
  13. package/dist/date-month.d.mts +23 -0
  14. package/dist/date-month.d.ts +23 -0
  15. package/dist/date-month.js +131 -0
  16. package/dist/date-month.mjs +105 -0
  17. package/dist/date-year.d.mts +14 -0
  18. package/dist/date-year.d.ts +14 -0
  19. package/dist/date-year.js +72 -0
  20. package/dist/date-year.mjs +44 -0
  21. package/dist/duration.d.mts +13 -0
  22. package/dist/duration.d.ts +13 -0
  23. package/dist/duration.js +42 -0
  24. package/dist/duration.mjs +16 -0
  25. package/dist/format.d.mts +8 -0
  26. package/dist/format.d.ts +8 -0
  27. package/dist/format.js +83 -0
  28. package/dist/format.mjs +56 -0
  29. package/dist/formatter.d.mts +6 -0
  30. package/dist/formatter.d.ts +6 -0
  31. package/dist/formatter.js +55 -0
  32. package/dist/formatter.mjs +29 -0
  33. package/dist/get-era-format.d.mts +6 -0
  34. package/dist/get-era-format.d.ts +6 -0
  35. package/dist/get-era-format.js +37 -0
  36. package/dist/get-era-format.mjs +12 -0
  37. package/dist/index.d.mts +14 -103
  38. package/dist/index.d.ts +14 -103
  39. package/dist/index.js +46 -637
  40. package/dist/index.mjs +13 -591
  41. package/dist/mutation.d.mts +10 -0
  42. package/dist/mutation.d.ts +10 -0
  43. package/dist/mutation.js +62 -0
  44. package/dist/mutation.mjs +39 -0
  45. package/dist/pagination.d.mts +21 -0
  46. package/dist/pagination.d.ts +21 -0
  47. package/dist/pagination.js +220 -0
  48. package/dist/pagination.mjs +187 -0
  49. package/dist/parse-date.d.mts +6 -0
  50. package/dist/parse-date.d.ts +6 -0
  51. package/dist/parse-date.js +79 -0
  52. package/dist/parse-date.mjs +54 -0
  53. package/dist/preset.d.mts +6 -0
  54. package/dist/preset.d.ts +6 -0
  55. package/dist/preset.js +66 -0
  56. package/dist/preset.mjs +50 -0
  57. package/dist/types.d.mts +23 -0
  58. package/dist/types.d.ts +23 -0
  59. package/dist/types.js +18 -0
  60. package/dist/types.mjs +0 -0
  61. package/package.json +1 -1
package/dist/index.mjs CHANGED
@@ -1,591 +1,13 @@
1
- import { startOfYear, startOfMonth, startOfWeek, maxDate, toCalendarDate, minDate, isSameDay, toCalendarDateTime, today, DateFormatter, endOfMonth, endOfWeek, getWeeksInMonth, getLocalTimeZone, toCalendar, CalendarDate, now, endOfYear } from '@internationalized/date';
2
-
3
- // src/constrain.ts
4
- function alignCenter(date, duration, locale, min, max) {
5
- const halfDuration = {};
6
- for (let prop in duration) {
7
- const key = prop;
8
- const value = duration[key];
9
- if (value == null) continue;
10
- halfDuration[key] = Math.floor(value / 2);
11
- if (halfDuration[key] > 0 && value % 2 === 0) {
12
- halfDuration[key]--;
13
- }
14
- }
15
- const aligned = alignStart(date, duration, locale).subtract(halfDuration);
16
- return constrainStart(date, aligned, duration, locale, min, max);
17
- }
18
- function alignStart(date, duration, locale, min, max) {
19
- let aligned = date;
20
- if (duration.years) {
21
- aligned = startOfYear(date);
22
- } else if (duration.months) {
23
- aligned = startOfMonth(date);
24
- } else if (duration.weeks) {
25
- aligned = startOfWeek(date, locale);
26
- }
27
- return constrainStart(date, aligned, duration, locale, min, max);
28
- }
29
- function alignEnd(date, duration, locale, min, max) {
30
- let d = { ...duration };
31
- if (d.days) {
32
- d.days--;
33
- } else if (d.weeks) {
34
- d.weeks--;
35
- } else if (d.months) {
36
- d.months--;
37
- } else if (d.years) {
38
- d.years--;
39
- }
40
- let aligned = alignStart(date, duration, locale).subtract(d);
41
- return constrainStart(date, aligned, duration, locale, min, max);
42
- }
43
- function constrainStart(date, aligned, duration, locale, min, max) {
44
- if (min && date.compare(min) >= 0) {
45
- aligned = maxDate(aligned, alignStart(toCalendarDate(min), duration, locale));
46
- }
47
- if (max && date.compare(max) <= 0) {
48
- aligned = minDate(aligned, alignEnd(toCalendarDate(max), duration, locale));
49
- }
50
- return aligned;
51
- }
52
- function constrainValue(date, minValue, maxValue) {
53
- const dateOnly = toCalendarDate(date);
54
- const minOnly = minValue ? toCalendarDate(minValue) : void 0;
55
- const maxOnly = maxValue ? toCalendarDate(maxValue) : void 0;
56
- let constrainedDateOnly = dateOnly;
57
- if (minOnly) {
58
- constrainedDateOnly = maxDate(constrainedDateOnly, minOnly);
59
- }
60
- if (maxOnly) {
61
- constrainedDateOnly = minDate(constrainedDateOnly, maxOnly);
62
- }
63
- if (constrainedDateOnly.compare(dateOnly) === 0) {
64
- return date;
65
- }
66
- if ("hour" in date) {
67
- return date.set({
68
- year: constrainedDateOnly.year,
69
- month: constrainedDateOnly.month,
70
- day: constrainedDateOnly.day
71
- });
72
- }
73
- return constrainedDateOnly;
74
- }
75
-
76
- // src/align.ts
77
- function alignDate(date, alignment, duration, locale, min, max) {
78
- switch (alignment) {
79
- case "start":
80
- return alignStart(date, duration, locale, min, max);
81
- case "end":
82
- return alignEnd(date, duration, locale, min, max);
83
- case "center":
84
- default:
85
- return alignCenter(date, duration, locale, min, max);
86
- }
87
- }
88
- function alignStartDate(date, startDate, endDate, duration, locale, min, max) {
89
- if (date.compare(startDate) < 0) {
90
- return alignEnd(date, duration, locale, min, max);
91
- }
92
- if (date.compare(endDate) > 0) {
93
- return alignStart(date, duration, locale, min, max);
94
- }
95
- return startDate;
96
- }
97
- function isDateEqual(dateA, dateB) {
98
- if (dateA == null || dateB == null) return dateA === dateB;
99
- return isSameDay(dateA, dateB);
100
- }
101
- function isDateUnavailable(date, isUnavailable, locale, minValue, maxValue) {
102
- if (!date) return false;
103
- if (isUnavailable?.(date, locale)) return true;
104
- return isDateOutsideRange(date, minValue, maxValue);
105
- }
106
- function isDateOutsideRange(date, startDate, endDate) {
107
- return startDate != null && date.compare(startDate) < 0 || endDate != null && date.compare(endDate) > 0;
108
- }
109
- function isPreviousRangeInvalid(startDate, minValue, maxValue) {
110
- const prevDate = startDate.subtract({ days: 1 });
111
- return isSameDay(prevDate, startDate) || isDateOutsideRange(prevDate, minValue, maxValue);
112
- }
113
- function isNextRangeInvalid(endDate, minValue, maxValue) {
114
- const nextDate = endDate.add({ days: 1 });
115
- return isSameDay(nextDate, endDate) || isDateOutsideRange(nextDate, minValue, maxValue);
116
- }
117
-
118
- // src/duration.ts
119
- function getUnitDuration(duration) {
120
- let clone = { ...duration };
121
- for (let key in clone) clone[key] = 1;
122
- return clone;
123
- }
124
- function getEndDate(startDate, duration) {
125
- let clone = { ...duration };
126
- if (clone.days) clone.days--;
127
- else clone.days = -1;
128
- return startDate.add(clone);
129
- }
130
-
131
- // src/get-era-format.ts
132
- function getEraFormat(date) {
133
- return date?.calendar.identifier === "gregory" && date.era === "BC" ? "short" : void 0;
134
- }
135
-
136
- // src/formatter.ts
137
- function getDayFormatter(locale, timeZone) {
138
- const date = toCalendarDateTime(today(timeZone));
139
- return new DateFormatter(locale, {
140
- weekday: "long",
141
- month: "long",
142
- year: "numeric",
143
- day: "numeric",
144
- era: getEraFormat(date),
145
- timeZone
146
- });
147
- }
148
- function getMonthFormatter(locale, timeZone) {
149
- const date = today(timeZone);
150
- return new DateFormatter(locale, {
151
- month: "long",
152
- year: "numeric",
153
- era: getEraFormat(date),
154
- calendar: date?.calendar.identifier,
155
- timeZone
156
- });
157
- }
158
-
159
- // src/format.ts
160
- function formatRange(startDate, endDate, formatter, toString, timeZone) {
161
- let parts = formatter.formatRangeToParts(startDate.toDate(timeZone), endDate.toDate(timeZone));
162
- let separatorIndex = -1;
163
- for (let i = 0; i < parts.length; i++) {
164
- let part = parts[i];
165
- if (part.source === "shared" && part.type === "literal") {
166
- separatorIndex = i;
167
- } else if (part.source === "endRange") {
168
- break;
169
- }
170
- }
171
- let start = "";
172
- let end = "";
173
- for (let i = 0; i < parts.length; i++) {
174
- if (i < separatorIndex) {
175
- start += parts[i].value;
176
- } else if (i > separatorIndex) {
177
- end += parts[i].value;
178
- }
179
- }
180
- return toString(start, end);
181
- }
182
- function formatSelectedDate(startDate, endDate, locale, timeZone) {
183
- if (!startDate) return "";
184
- let start = startDate;
185
- let end = endDate ?? startDate;
186
- let formatter = getDayFormatter(locale, timeZone);
187
- if (isSameDay(start, end)) {
188
- return formatter.format(start.toDate(timeZone));
189
- }
190
- return formatRange(start, end, formatter, (start2, end2) => `${start2} \u2013 ${end2}`, timeZone);
191
- }
192
- function formatVisibleRange(startDate, endDate, locale, timeZone) {
193
- const start = startDate;
194
- const end = endDate ?? startDate;
195
- const dayFormatter = getDayFormatter(locale, timeZone);
196
- if (!isSameDay(start, startOfMonth(start))) {
197
- return dayFormatter.formatRange(start.toDate(timeZone), end.toDate(timeZone));
198
- }
199
- const monthFormatter = getMonthFormatter(locale, timeZone);
200
- if (isSameDay(end, endOfMonth(start))) {
201
- return monthFormatter.format(start.toDate(timeZone));
202
- }
203
- if (isSameDay(end, endOfMonth(end))) {
204
- return monthFormatter.formatRange(start.toDate(timeZone), end.toDate(timeZone));
205
- }
206
- return "";
207
- }
208
- var daysOfTheWeek = ["sun", "mon", "tue", "wed", "thu", "fri", "sat"];
209
- function normalizeFirstDayOfWeek(firstDayOfWeek) {
210
- return firstDayOfWeek != null ? daysOfTheWeek[firstDayOfWeek] : void 0;
211
- }
212
- function getStartOfWeek(date, locale, firstDayOfWeek) {
213
- const firstDay = normalizeFirstDayOfWeek(firstDayOfWeek);
214
- return startOfWeek(date, locale, firstDay);
215
- }
216
- function getEndOfWeek(date, locale, firstDayOfWeek = 0) {
217
- const firstDay = normalizeFirstDayOfWeek(firstDayOfWeek);
218
- return endOfWeek(date, locale, firstDay);
219
- }
220
- function getDaysInWeek(weekIndex, from, locale, firstDayOfWeek) {
221
- const weekDate = from.add({ weeks: weekIndex });
222
- const dates = [];
223
- let date = getStartOfWeek(weekDate, locale, firstDayOfWeek);
224
- while (dates.length < 7) {
225
- dates.push(date);
226
- let nextDate = date.add({ days: 1 });
227
- if (isSameDay(date, nextDate)) break;
228
- date = nextDate;
229
- }
230
- return dates;
231
- }
232
- function getMonthDays(from, locale, numOfWeeks, firstDayOfWeek) {
233
- const firstDay = normalizeFirstDayOfWeek(firstDayOfWeek);
234
- const monthWeeks = numOfWeeks ?? getWeeksInMonth(from, locale, firstDay);
235
- const weeks = [...new Array(monthWeeks).keys()];
236
- return weeks.map((week) => getDaysInWeek(week, from, locale, firstDayOfWeek));
237
- }
238
- function getWeekdayFormats(locale, timeZone) {
239
- const longFormat = new DateFormatter(locale, { weekday: "long", timeZone });
240
- const shortFormat = new DateFormatter(locale, { weekday: "short", timeZone });
241
- const narrowFormat = new DateFormatter(locale, { weekday: "narrow", timeZone });
242
- return (value) => {
243
- const date = value instanceof Date ? value : value.toDate(timeZone);
244
- return {
245
- value,
246
- short: shortFormat.format(date),
247
- long: longFormat.format(date),
248
- narrow: narrowFormat.format(date)
249
- };
250
- };
251
- }
252
- function getWeekDays(date, startOfWeekProp, timeZone, locale) {
253
- const firstDayOfWeek = getStartOfWeek(date, locale, startOfWeekProp);
254
- const weeks = [...new Array(7).keys()];
255
- const format = getWeekdayFormats(locale, timeZone);
256
- return weeks.map((index) => format(firstDayOfWeek.add({ days: index })));
257
- }
258
- function getMonthNames(locale, format = "long") {
259
- const date = new Date(2021, 0, 1);
260
- const monthNames = [];
261
- for (let i = 0; i < 12; i++) {
262
- monthNames.push(date.toLocaleString(locale, { month: format }));
263
- date.setMonth(date.getMonth() + 1);
264
- }
265
- return monthNames;
266
- }
267
- function getWeekOfYear(date, locale) {
268
- const mondayOfWeek = startOfWeek(date, locale, "mon");
269
- const year = mondayOfWeek.year;
270
- const jan4 = mondayOfWeek.set({ month: 1, day: 4 });
271
- const week1Monday = startOfWeek(jan4, locale, "mon");
272
- const julianMonday = mondayOfWeek.calendar.toJulianDay(mondayOfWeek);
273
- const julianWeek1 = week1Monday.calendar.toJulianDay(week1Monday);
274
- if (julianMonday >= julianWeek1) {
275
- return 1 + Math.floor((julianMonday - julianWeek1) / 7);
276
- }
277
- const prevJan4 = mondayOfWeek.set({ year: year - 1, month: 1, day: 4 });
278
- const prevWeek1Monday = startOfWeek(prevJan4, locale, "mon");
279
- const julianPrevWeek1 = prevWeek1Monday.calendar.toJulianDay(prevWeek1Monday);
280
- return 1 + Math.floor((julianMonday - julianPrevWeek1) / 7);
281
- }
282
-
283
- // src/date-year.ts
284
- function getYearsRange(range) {
285
- const years = [];
286
- for (let year = range.from; year <= range.to; year += 1) years.push(year);
287
- return years;
288
- }
289
- var FUTURE_YEAR_COERCION = 10;
290
- function normalizeYear(year) {
291
- if (!year) return;
292
- if (year.length === 3) return year.padEnd(4, "0");
293
- if (year.length === 2) {
294
- const currentYear = (/* @__PURE__ */ new Date()).getFullYear();
295
- const currentCentury = Math.floor(currentYear / 100) * 100;
296
- const twoDigitYear = parseInt(year.slice(-2), 10);
297
- const fullYear = currentCentury + twoDigitYear;
298
- return fullYear > currentYear + FUTURE_YEAR_COERCION ? (fullYear - 100).toString() : fullYear.toString();
299
- }
300
- return year;
301
- }
302
- function getDecadeRange(year, opts) {
303
- const chunkSize = opts?.strict ? 10 : 12;
304
- const computedYear = year - year % 10;
305
- const years = [];
306
- for (let i = 0; i < chunkSize; i += 1) {
307
- const value = computedYear + i;
308
- years.push(value);
309
- }
310
- return years;
311
- }
312
- function getTodayDate(timeZone) {
313
- return today(timeZone ?? getLocalTimeZone());
314
- }
315
- function setCalendar(date, calendar) {
316
- return toCalendar(toCalendarDateTime(date), calendar);
317
- }
318
- function setDate(date, startDate, isDateUnavailable2, locale, minValue, maxValue) {
319
- let result;
320
- result = constrainValue(date, minValue, maxValue);
321
- result = getPreviousAvailableDate(date, startDate, locale, isDateUnavailable2);
322
- return result;
323
- }
324
- function getPreviousAvailableDate(date, minValue, locale, isDateUnavailable2) {
325
- if (!isDateUnavailable2) {
326
- return date;
327
- }
328
- while (date.compare(minValue) >= 0 && isDateUnavailable2(date, locale)) {
329
- date = date.subtract({ days: 1 });
330
- }
331
- if (date.compare(minValue) >= 0) {
332
- return date;
333
- }
334
- }
335
- function getAdjustedDateFn(visibleDuration, locale, minValue, maxValue) {
336
- return function getDate(options) {
337
- const { startDate, focusedDate } = options;
338
- const endDate = getEndDate(startDate, visibleDuration);
339
- if (isDateOutsideRange(focusedDate, minValue, maxValue)) {
340
- return {
341
- startDate,
342
- focusedDate: constrainValue(focusedDate, minValue, maxValue),
343
- endDate
344
- };
345
- }
346
- if (focusedDate.compare(startDate) < 0) {
347
- return {
348
- startDate: alignEnd(focusedDate, visibleDuration, locale, minValue, maxValue),
349
- focusedDate: constrainValue(focusedDate, minValue, maxValue),
350
- endDate
351
- };
352
- }
353
- if (focusedDate.compare(endDate) > 0) {
354
- return {
355
- startDate: alignStart(focusedDate, visibleDuration, locale, minValue, maxValue),
356
- endDate,
357
- focusedDate: constrainValue(focusedDate, minValue, maxValue)
358
- };
359
- }
360
- return {
361
- startDate,
362
- endDate,
363
- focusedDate: constrainValue(focusedDate, minValue, maxValue)
364
- };
365
- };
366
- }
367
- function getNextPage(focusedDate, startDate, visibleDuration, locale, minValue, maxValue) {
368
- const adjust = getAdjustedDateFn(visibleDuration, locale, minValue, maxValue);
369
- const start = startDate.add(visibleDuration);
370
- return adjust({
371
- focusedDate: focusedDate.add(visibleDuration),
372
- startDate: alignStart(
373
- constrainStart(focusedDate, start, visibleDuration, locale, minValue, maxValue),
374
- visibleDuration,
375
- locale
376
- )
377
- });
378
- }
379
- function getPreviousPage(focusedDate, startDate, visibleDuration, locale, minValue, maxValue) {
380
- const adjust = getAdjustedDateFn(visibleDuration, locale, minValue, maxValue);
381
- let start = startDate.subtract(visibleDuration);
382
- return adjust({
383
- focusedDate: focusedDate.subtract(visibleDuration),
384
- startDate: alignStart(
385
- constrainStart(focusedDate, start, visibleDuration, locale, minValue, maxValue),
386
- visibleDuration,
387
- locale
388
- )
389
- });
390
- }
391
- function getNextRow(focusedDate, startDate, visibleDuration, locale, minValue, maxValue) {
392
- const adjust = getAdjustedDateFn(visibleDuration, locale, minValue, maxValue);
393
- if (visibleDuration.days) {
394
- return getNextPage(focusedDate, startDate, visibleDuration, locale, minValue, maxValue);
395
- }
396
- if (visibleDuration.weeks || visibleDuration.months || visibleDuration.years) {
397
- return adjust({
398
- focusedDate: focusedDate.add({ weeks: 1 }),
399
- startDate
400
- });
401
- }
402
- }
403
- function getPreviousRow(focusedDate, startDate, visibleDuration, locale, minValue, maxValue) {
404
- const adjust = getAdjustedDateFn(visibleDuration, locale, minValue, maxValue);
405
- if (visibleDuration.days) {
406
- return getPreviousPage(focusedDate, startDate, visibleDuration, locale, minValue, maxValue);
407
- }
408
- if (visibleDuration.weeks || visibleDuration.months || visibleDuration.years) {
409
- return adjust({
410
- focusedDate: focusedDate.subtract({ weeks: 1 }),
411
- startDate
412
- });
413
- }
414
- }
415
- function getSectionStart(focusedDate, startDate, visibleDuration, locale, minValue, maxValue) {
416
- const adjust = getAdjustedDateFn(visibleDuration, locale, minValue, maxValue);
417
- if (visibleDuration.days) {
418
- return adjust({
419
- focusedDate: startDate,
420
- startDate
421
- });
422
- }
423
- if (visibleDuration.weeks) {
424
- return adjust({
425
- focusedDate: startOfWeek(focusedDate, locale),
426
- startDate
427
- });
428
- }
429
- if (visibleDuration.months || visibleDuration.years) {
430
- return adjust({
431
- focusedDate: startOfMonth(focusedDate),
432
- startDate
433
- });
434
- }
435
- }
436
- function getSectionEnd(focusedDate, startDate, visibleDuration, locale, minValue, maxValue) {
437
- const adjust = getAdjustedDateFn(visibleDuration, locale, minValue, maxValue);
438
- const endDate = getEndDate(startDate, visibleDuration);
439
- if (visibleDuration.days) {
440
- return adjust({
441
- focusedDate: endDate,
442
- startDate
443
- });
444
- }
445
- if (visibleDuration.weeks) {
446
- return adjust({
447
- focusedDate: endOfWeek(focusedDate, locale),
448
- startDate
449
- });
450
- }
451
- if (visibleDuration.months || visibleDuration.years) {
452
- return adjust({
453
- focusedDate: endOfMonth(focusedDate),
454
- startDate
455
- });
456
- }
457
- }
458
- function getNextSection(focusedDate, startDate, larger, visibleDuration, locale, minValue, maxValue) {
459
- const adjust = getAdjustedDateFn(visibleDuration, locale, minValue, maxValue);
460
- if (!larger && !visibleDuration.days) {
461
- return adjust({
462
- focusedDate: focusedDate.add(getUnitDuration(visibleDuration)),
463
- startDate
464
- });
465
- }
466
- if (visibleDuration.days) {
467
- return getNextPage(focusedDate, startDate, visibleDuration, locale, minValue, maxValue);
468
- }
469
- if (visibleDuration.weeks) {
470
- return adjust({
471
- focusedDate: focusedDate.add({ months: 1 }),
472
- startDate
473
- });
474
- }
475
- if (visibleDuration.months || visibleDuration.years) {
476
- return adjust({
477
- focusedDate: focusedDate.add({ years: 1 }),
478
- startDate
479
- });
480
- }
481
- }
482
- function getPreviousSection(focusedDate, startDate, larger, visibleDuration, locale, minValue, maxValue) {
483
- const adjust = getAdjustedDateFn(visibleDuration, locale, minValue, maxValue);
484
- if (!larger && !visibleDuration.days) {
485
- return adjust({
486
- focusedDate: focusedDate.subtract(getUnitDuration(visibleDuration)),
487
- startDate
488
- });
489
- }
490
- if (visibleDuration.days) {
491
- return getPreviousPage(focusedDate, startDate, visibleDuration, locale, minValue, maxValue);
492
- }
493
- if (visibleDuration.weeks) {
494
- return adjust({
495
- focusedDate: focusedDate.subtract({ months: 1 }),
496
- startDate
497
- });
498
- }
499
- if (visibleDuration.months || visibleDuration.years) {
500
- return adjust({
501
- focusedDate: focusedDate.subtract({ years: 1 }),
502
- startDate
503
- });
504
- }
505
- }
506
- var isValidYear = (year) => year != null && year.length === 4;
507
- var isValidMonth = (month) => month != null && parseFloat(month) <= 12;
508
- var isValidDay = (day) => day != null && parseFloat(day) <= 31;
509
- function parseDateString(date, locale, timeZone) {
510
- const regex = createRegex(locale, timeZone);
511
- let { year, month, day } = extract(regex, date) ?? {};
512
- const hasMatch = year != null || month != null || day != null;
513
- if (hasMatch) {
514
- const curr = /* @__PURE__ */ new Date();
515
- year || (year = curr.getFullYear().toString());
516
- month || (month = (curr.getMonth() + 1).toString());
517
- day || (day = curr.getDate().toString());
518
- }
519
- if (!isValidYear(year)) {
520
- year = normalizeYear(year);
521
- }
522
- if (isValidYear(year) && isValidMonth(month) && isValidDay(day)) {
523
- return new CalendarDate(+year, +month, +day);
524
- }
525
- const time = Date.parse(date);
526
- if (!isNaN(time)) {
527
- const date2 = new Date(time);
528
- return new CalendarDate(date2.getFullYear(), date2.getMonth() + 1, date2.getDate());
529
- }
530
- }
531
- function createRegex(locale, timeZone) {
532
- const formatter = new DateFormatter(locale, { day: "numeric", month: "numeric", year: "numeric", timeZone });
533
- const parts = formatter.formatToParts(new Date(2e3, 11, 25));
534
- return parts.map(({ type, value }) => type === "literal" ? `${value}?` : `((?!=<${type}>)\\d+)?`).join("");
535
- }
536
- function extract(pattern, str) {
537
- const matches = str.match(pattern);
538
- return pattern.toString().match(/<(.+?)>/g)?.map((group) => {
539
- const groupMatches = group.match(/<(.+)>/);
540
- if (!groupMatches || groupMatches.length <= 0) {
541
- return null;
542
- }
543
- return group.match(/<(.+)>/)?.[1];
544
- }).reduce((acc, curr, index) => {
545
- if (!curr) return acc;
546
- if (matches && matches.length > index) {
547
- acc[curr] = matches[index + 1];
548
- } else {
549
- acc[curr] = null;
550
- }
551
- return acc;
552
- }, {});
553
- }
554
- function getDateRangePreset(preset, locale, timeZone) {
555
- const today3 = toCalendarDate(now(timeZone));
556
- switch (preset) {
557
- case "thisWeek":
558
- return [startOfWeek(today3, locale), endOfWeek(today3, locale)];
559
- case "thisMonth":
560
- return [startOfMonth(today3), today3];
561
- case "thisQuarter":
562
- return [startOfMonth(today3).add({ months: -((today3.month - 1) % 3) }), today3];
563
- case "thisYear":
564
- return [startOfYear(today3), today3];
565
- case "last3Days":
566
- return [today3.add({ days: -2 }), today3];
567
- case "last7Days":
568
- return [today3.add({ days: -6 }), today3];
569
- case "last14Days":
570
- return [today3.add({ days: -13 }), today3];
571
- case "last30Days":
572
- return [today3.add({ days: -29 }), today3];
573
- case "last90Days":
574
- return [today3.add({ days: -89 }), today3];
575
- case "lastMonth":
576
- return [startOfMonth(today3.add({ months: -1 })), endOfMonth(today3.add({ months: -1 }))];
577
- case "lastQuarter":
578
- return [
579
- startOfMonth(today3.add({ months: -((today3.month - 1) % 3) - 3 })),
580
- endOfMonth(today3.add({ months: -((today3.month - 1) % 3) - 1 }))
581
- ];
582
- case "lastWeek":
583
- return [startOfWeek(today3, locale).add({ weeks: -1 }), endOfWeek(today3, locale).add({ weeks: -1 })];
584
- case "lastYear":
585
- return [startOfYear(today3.add({ years: -1 })), endOfYear(today3.add({ years: -1 }))];
586
- default:
587
- throw new Error(`Invalid date range preset: ${preset}`);
588
- }
589
- }
590
-
591
- export { alignCenter, alignDate, alignEnd, alignStart, alignStartDate, constrainStart, constrainValue, formatRange, formatSelectedDate, formatVisibleRange, getAdjustedDateFn, getDateRangePreset, getDayFormatter, getDaysInWeek, getDecadeRange, getEndDate, getEndOfWeek, getMonthDays, getMonthFormatter, getMonthNames, getNextPage, getNextRow, getNextSection, getPreviousAvailableDate, getPreviousPage, getPreviousRow, getPreviousSection, getSectionEnd, getSectionStart, getStartOfWeek, getTodayDate, getUnitDuration, getWeekDays, getWeekOfYear, getWeekdayFormats, getYearsRange, isDateEqual, isDateOutsideRange, isDateUnavailable, isNextRangeInvalid, isPreviousRangeInvalid, normalizeYear, parseDateString, setCalendar, setDate };
1
+ // src/index.ts
2
+ export * from "./align.mjs";
3
+ export * from "./assertion.mjs";
4
+ export * from "./constrain.mjs";
5
+ export * from "./duration.mjs";
6
+ export * from "./format.mjs";
7
+ export * from "./formatter.mjs";
8
+ export * from "./date-month.mjs";
9
+ export * from "./date-year.mjs";
10
+ export * from "./mutation.mjs";
11
+ export * from "./pagination.mjs";
12
+ export * from "./parse-date.mjs";
13
+ export * from "./preset.mjs";
@@ -0,0 +1,10 @@
1
+ import * as _internationalized_date from '@internationalized/date';
2
+ import { DateValue, Calendar } from '@internationalized/date';
3
+ import { DateAvailableFn } from './types.mjs';
4
+
5
+ declare function getTodayDate(timeZone?: string, calendar?: Calendar): _internationalized_date.CalendarDate;
6
+ declare function setCalendar(date: DateValue, calendar: Calendar): _internationalized_date.CalendarDateTime;
7
+ declare function setDate(date: DateValue, startDate: DateValue, isDateUnavailable: DateAvailableFn, locale: string, minValue: DateValue, maxValue: DateValue): DateValue | undefined;
8
+ declare function getPreviousAvailableDate(date: DateValue, minValue: DateValue, locale: string, isDateUnavailable?: DateAvailableFn): DateValue | undefined;
9
+
10
+ export { getPreviousAvailableDate, getTodayDate, setCalendar, setDate };
@@ -0,0 +1,10 @@
1
+ import * as _internationalized_date from '@internationalized/date';
2
+ import { DateValue, Calendar } from '@internationalized/date';
3
+ import { DateAvailableFn } from './types.js';
4
+
5
+ declare function getTodayDate(timeZone?: string, calendar?: Calendar): _internationalized_date.CalendarDate;
6
+ declare function setCalendar(date: DateValue, calendar: Calendar): _internationalized_date.CalendarDateTime;
7
+ declare function setDate(date: DateValue, startDate: DateValue, isDateUnavailable: DateAvailableFn, locale: string, minValue: DateValue, maxValue: DateValue): DateValue | undefined;
8
+ declare function getPreviousAvailableDate(date: DateValue, minValue: DateValue, locale: string, isDateUnavailable?: DateAvailableFn): DateValue | undefined;
9
+
10
+ export { getPreviousAvailableDate, getTodayDate, setCalendar, setDate };
@@ -0,0 +1,62 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/mutation.ts
21
+ var mutation_exports = {};
22
+ __export(mutation_exports, {
23
+ getPreviousAvailableDate: () => getPreviousAvailableDate,
24
+ getTodayDate: () => getTodayDate,
25
+ setCalendar: () => setCalendar,
26
+ setDate: () => setDate
27
+ });
28
+ module.exports = __toCommonJS(mutation_exports);
29
+ var import_date = require("@internationalized/date");
30
+ var import_constrain = require("./constrain.cjs");
31
+ function getTodayDate(timeZone, calendar) {
32
+ const tod = (0, import_date.today)(timeZone ?? (0, import_date.getLocalTimeZone)());
33
+ if (calendar) return (0, import_date.toCalendar)(tod, calendar);
34
+ return tod;
35
+ }
36
+ function setCalendar(date, calendar) {
37
+ return (0, import_date.toCalendar)((0, import_date.toCalendarDateTime)(date), calendar);
38
+ }
39
+ function setDate(date, startDate, isDateUnavailable, locale, minValue, maxValue) {
40
+ let result;
41
+ result = (0, import_constrain.constrainValue)(date, minValue, maxValue);
42
+ result = getPreviousAvailableDate(date, startDate, locale, isDateUnavailable);
43
+ return result;
44
+ }
45
+ function getPreviousAvailableDate(date, minValue, locale, isDateUnavailable) {
46
+ if (!isDateUnavailable) {
47
+ return date;
48
+ }
49
+ while (date.compare(minValue) >= 0 && isDateUnavailable(date, locale)) {
50
+ date = date.subtract({ days: 1 });
51
+ }
52
+ if (date.compare(minValue) >= 0) {
53
+ return date;
54
+ }
55
+ }
56
+ // Annotate the CommonJS export names for ESM import in node:
57
+ 0 && (module.exports = {
58
+ getPreviousAvailableDate,
59
+ getTodayDate,
60
+ setCalendar,
61
+ setDate
62
+ });