@zag-js/date-utils 0.10.4 → 0.11.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 (73) hide show
  1. package/dist/index.d.mts +145 -0
  2. package/dist/index.d.ts +145 -20
  3. package/dist/index.js +744 -74
  4. package/dist/index.js.map +1 -0
  5. package/dist/index.mjs +687 -19
  6. package/dist/index.mjs.map +1 -0
  7. package/package.json +2 -2
  8. package/src/parse-date.ts +12 -9
  9. package/dist/align.d.ts +0 -4
  10. package/dist/align.js +0 -29
  11. package/dist/align.mjs +0 -24
  12. package/dist/assertion.d.ts +0 -10
  13. package/dist/assertion.js +0 -47
  14. package/dist/assertion.mjs +0 -36
  15. package/dist/constrain.d.ts +0 -6
  16. package/dist/constrain.js +0 -66
  17. package/dist/constrain.mjs +0 -58
  18. package/dist/duration.d.ts +0 -8
  19. package/dist/duration.js +0 -23
  20. package/dist/duration.mjs +0 -18
  21. package/dist/format-date.d.ts +0 -6
  22. package/dist/format-date.js +0 -57
  23. package/dist/format-date.mjs +0 -53
  24. package/dist/format-range.d.ts +0 -2
  25. package/dist/format-range.js +0 -28
  26. package/dist/format-range.mjs +0 -24
  27. package/dist/format-selected-date.d.ts +0 -2
  28. package/dist/format-selected-date.js +0 -19
  29. package/dist/format-selected-date.mjs +0 -15
  30. package/dist/format-visible-range.d.ts +0 -2
  31. package/dist/format-visible-range.js +0 -26
  32. package/dist/format-visible-range.mjs +0 -22
  33. package/dist/get-day-formatter.d.ts +0 -2
  34. package/dist/get-day-formatter.js +0 -20
  35. package/dist/get-day-formatter.mjs +0 -16
  36. package/dist/get-decade-range.d.ts +0 -1
  37. package/dist/get-decade-range.js +0 -15
  38. package/dist/get-decade-range.mjs +0 -11
  39. package/dist/get-end-of-week.d.ts +0 -2
  40. package/dist/get-era-format.d.ts +0 -2
  41. package/dist/get-era-format.js +0 -9
  42. package/dist/get-era-format.mjs +0 -5
  43. package/dist/get-month-days.d.ts +0 -3
  44. package/dist/get-month-days.js +0 -29
  45. package/dist/get-month-days.mjs +0 -24
  46. package/dist/get-month-formatter.d.ts +0 -2
  47. package/dist/get-month-formatter.js +0 -19
  48. package/dist/get-month-formatter.mjs +0 -15
  49. package/dist/get-month-names.d.ts +0 -1
  50. package/dist/get-month-names.js +0 -15
  51. package/dist/get-month-names.mjs +0 -11
  52. package/dist/get-start-of-week.d.ts +0 -2
  53. package/dist/get-start-of-week.js +0 -13
  54. package/dist/get-start-of-week.mjs +0 -9
  55. package/dist/get-week-days.d.ts +0 -7
  56. package/dist/get-week-days.js +0 -15
  57. package/dist/get-week-days.mjs +0 -11
  58. package/dist/get-weekday-formats.d.ts +0 -7
  59. package/dist/get-weekday-formats.js +0 -22
  60. package/dist/get-weekday-formats.mjs +0 -18
  61. package/dist/get-year-range.d.ts +0 -5
  62. package/dist/get-year-range.js +0 -13
  63. package/dist/get-year-range.mjs +0 -9
  64. package/dist/mutation.d.ts +0 -10
  65. package/dist/mutation.js +0 -51
  66. package/dist/mutation.mjs +0 -40
  67. package/dist/pagination.d.ts +0 -49
  68. package/dist/pagination.js +0 -191
  69. package/dist/pagination.mjs +0 -179
  70. package/dist/parse-date.d.ts +0 -2
  71. package/dist/parse-date.js +0 -44
  72. package/dist/parse-date.mjs +0 -40
  73. package/dist/types.d.ts +0 -18
@@ -1,49 +0,0 @@
1
- import { CalendarDate, CalendarDateTime, ZonedDateTime, type DateDuration, type DateValue } from '@internationalized/date';
2
- export declare function getAdjustedDateFn(visibleDuration: DateDuration, locale: string, minValue?: DateValue, maxValue?: DateValue): (options: {
3
- startDate: DateValue;
4
- focusedDate: DateValue;
5
- }) => {
6
- startDate: DateValue;
7
- focusedDate: DateValue;
8
- endDate: CalendarDate | CalendarDateTime | ZonedDateTime;
9
- };
10
- export declare function getNextPage(focusedDate: DateValue, startDate: DateValue, visibleDuration: DateDuration, locale: string, minValue?: DateValue, maxValue?: DateValue): {
11
- startDate: DateValue;
12
- focusedDate: DateValue;
13
- endDate: CalendarDate | CalendarDateTime | ZonedDateTime;
14
- };
15
- export declare function getPreviousPage(focusedDate: DateValue, startDate: DateValue, visibleDuration: DateDuration, locale: string, minValue?: DateValue, maxValue?: DateValue): {
16
- startDate: DateValue;
17
- focusedDate: DateValue;
18
- endDate: CalendarDate | CalendarDateTime | ZonedDateTime;
19
- };
20
- export declare function getNextRow(focusedDate: DateValue, startDate: DateValue, visibleDuration: DateDuration, locale: string, minValue?: DateValue, maxValue?: DateValue): {
21
- startDate: DateValue;
22
- focusedDate: DateValue;
23
- endDate: CalendarDate | CalendarDateTime | ZonedDateTime;
24
- } | undefined;
25
- export declare function getPreviousRow(focusedDate: DateValue, startDate: DateValue, visibleDuration: DateDuration, locale: string, minValue?: DateValue, maxValue?: DateValue): {
26
- startDate: DateValue;
27
- focusedDate: DateValue;
28
- endDate: CalendarDate | CalendarDateTime | ZonedDateTime;
29
- } | undefined;
30
- export declare function getSectionStart(focusedDate: DateValue, startDate: DateValue, visibleDuration: DateDuration, locale: string, minValue?: DateValue, maxValue?: DateValue): {
31
- startDate: DateValue;
32
- focusedDate: DateValue;
33
- endDate: CalendarDate | CalendarDateTime | ZonedDateTime;
34
- } | undefined;
35
- export declare function getSectionEnd(focusedDate: DateValue, startDate: DateValue, visibleDuration: DateDuration, locale: string, minValue?: DateValue, maxValue?: DateValue): {
36
- startDate: DateValue;
37
- focusedDate: DateValue;
38
- endDate: CalendarDate | CalendarDateTime | ZonedDateTime;
39
- } | undefined;
40
- export declare function getNextSection(focusedDate: DateValue, startDate: DateValue, larger: boolean, visibleDuration: DateDuration, locale: string, minValue?: DateValue, maxValue?: DateValue): {
41
- startDate: DateValue;
42
- focusedDate: DateValue;
43
- endDate: CalendarDate | CalendarDateTime | ZonedDateTime;
44
- } | undefined;
45
- export declare function getPreviousSection(focusedDate: DateValue, startDate: DateValue, larger: boolean, visibleDuration: DateDuration, locale: string, minValue?: DateValue, maxValue?: DateValue): {
46
- startDate: DateValue;
47
- focusedDate: DateValue;
48
- endDate: CalendarDate | CalendarDateTime | ZonedDateTime;
49
- } | undefined;
@@ -1,191 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
-
5
- const date = require('@internationalized/date');
6
- const assertion = require('./assertion.js');
7
- const constrain = require('./constrain.js');
8
- const duration = require('./duration.js');
9
-
10
- function getAdjustedDateFn(visibleDuration, locale, minValue, maxValue) {
11
- return function getDate(options) {
12
- const { startDate, focusedDate } = options;
13
- const endDate = duration.getEndDate(startDate, visibleDuration);
14
- if (assertion.isDateInvalid(focusedDate, minValue, maxValue)) {
15
- return {
16
- startDate,
17
- focusedDate: constrain.constrainValue(focusedDate, minValue, maxValue),
18
- endDate
19
- };
20
- }
21
- if (focusedDate.compare(startDate) < 0) {
22
- return {
23
- startDate: constrain.alignEnd(focusedDate, visibleDuration, locale, minValue, maxValue),
24
- endDate,
25
- focusedDate: constrain.constrainValue(focusedDate, minValue, maxValue)
26
- };
27
- }
28
- if (focusedDate.compare(endDate) > 0) {
29
- return {
30
- startDate: constrain.alignStart(focusedDate, visibleDuration, locale, minValue, maxValue),
31
- endDate,
32
- focusedDate: constrain.constrainValue(focusedDate, minValue, maxValue)
33
- };
34
- }
35
- return {
36
- startDate,
37
- endDate,
38
- focusedDate: constrain.constrainValue(focusedDate, minValue, maxValue)
39
- };
40
- };
41
- }
42
- function getNextPage(focusedDate, startDate, visibleDuration, locale, minValue, maxValue) {
43
- const adjust = getAdjustedDateFn(visibleDuration, locale, minValue, maxValue);
44
- const start = startDate.add(visibleDuration);
45
- return adjust({
46
- focusedDate: focusedDate.add(visibleDuration),
47
- startDate: constrain.alignStart(
48
- constrain.constrainStart(focusedDate, start, visibleDuration, locale, minValue, maxValue),
49
- visibleDuration,
50
- locale
51
- )
52
- });
53
- }
54
- function getPreviousPage(focusedDate, startDate, visibleDuration, locale, minValue, maxValue) {
55
- const adjust = getAdjustedDateFn(visibleDuration, locale, minValue, maxValue);
56
- let start = startDate.subtract(visibleDuration);
57
- return adjust({
58
- focusedDate: focusedDate.subtract(visibleDuration),
59
- startDate: constrain.alignStart(
60
- constrain.constrainStart(focusedDate, start, visibleDuration, locale, minValue, maxValue),
61
- visibleDuration,
62
- locale
63
- )
64
- });
65
- }
66
- function getNextRow(focusedDate, startDate, visibleDuration, locale, minValue, maxValue) {
67
- const adjust = getAdjustedDateFn(visibleDuration, locale, minValue, maxValue);
68
- if (visibleDuration.days) {
69
- return getNextPage(focusedDate, startDate, visibleDuration, locale, minValue, maxValue);
70
- }
71
- if (visibleDuration.weeks || visibleDuration.months || visibleDuration.years) {
72
- return adjust({
73
- focusedDate: focusedDate.add({ weeks: 1 }),
74
- startDate
75
- });
76
- }
77
- }
78
- function getPreviousRow(focusedDate, startDate, visibleDuration, locale, minValue, maxValue) {
79
- const adjust = getAdjustedDateFn(visibleDuration, locale, minValue, maxValue);
80
- if (visibleDuration.days) {
81
- return getPreviousPage(focusedDate, startDate, visibleDuration, locale, minValue, maxValue);
82
- }
83
- if (visibleDuration.weeks || visibleDuration.months || visibleDuration.years) {
84
- return adjust({
85
- focusedDate: focusedDate.subtract({ weeks: 1 }),
86
- startDate
87
- });
88
- }
89
- }
90
- function getSectionStart(focusedDate, startDate, visibleDuration, locale, minValue, maxValue) {
91
- const adjust = getAdjustedDateFn(visibleDuration, locale, minValue, maxValue);
92
- if (visibleDuration.days) {
93
- return adjust({
94
- focusedDate: startDate,
95
- startDate
96
- });
97
- }
98
- if (visibleDuration.weeks) {
99
- return adjust({
100
- focusedDate: date.startOfWeek(focusedDate, locale),
101
- startDate
102
- });
103
- }
104
- if (visibleDuration.months || visibleDuration.years) {
105
- return adjust({
106
- focusedDate: date.startOfMonth(focusedDate),
107
- startDate
108
- });
109
- }
110
- }
111
- function getSectionEnd(focusedDate, startDate, visibleDuration, locale, minValue, maxValue) {
112
- const adjust = getAdjustedDateFn(visibleDuration, locale, minValue, maxValue);
113
- const endDate = duration.getEndDate(startDate, visibleDuration);
114
- if (visibleDuration.days) {
115
- return adjust({
116
- focusedDate: endDate,
117
- startDate
118
- });
119
- }
120
- if (visibleDuration.weeks) {
121
- return adjust({
122
- //@ts-expect-error - endOfWeek is loosely typed
123
- focusedDate: date.endOfWeek(focusedDate, locale),
124
- startDate
125
- });
126
- }
127
- if (visibleDuration.months || visibleDuration.years) {
128
- return adjust({
129
- focusedDate: date.endOfMonth(focusedDate),
130
- startDate
131
- });
132
- }
133
- }
134
- function getNextSection(focusedDate, startDate, larger, visibleDuration, locale, minValue, maxValue) {
135
- const adjust = getAdjustedDateFn(visibleDuration, locale, minValue, maxValue);
136
- if (!larger && !visibleDuration.days) {
137
- return adjust({
138
- focusedDate: focusedDate.add(duration.getUnitDuration(visibleDuration)),
139
- startDate
140
- });
141
- }
142
- if (visibleDuration.days) {
143
- return getNextPage(focusedDate, startDate, visibleDuration, locale, minValue, maxValue);
144
- }
145
- if (visibleDuration.weeks) {
146
- return adjust({
147
- focusedDate: focusedDate.add({ months: 1 }),
148
- startDate
149
- });
150
- }
151
- if (visibleDuration.months || visibleDuration.years) {
152
- return adjust({
153
- focusedDate: focusedDate.add({ years: 1 }),
154
- startDate
155
- });
156
- }
157
- }
158
- function getPreviousSection(focusedDate, startDate, larger, visibleDuration, locale, minValue, maxValue) {
159
- const adjust = getAdjustedDateFn(visibleDuration, locale, minValue, maxValue);
160
- if (!larger && !visibleDuration.days) {
161
- return adjust({
162
- focusedDate: focusedDate.subtract(duration.getUnitDuration(visibleDuration)),
163
- startDate
164
- });
165
- }
166
- if (visibleDuration.days) {
167
- return getPreviousPage(focusedDate, startDate, visibleDuration, locale, minValue, maxValue);
168
- }
169
- if (visibleDuration.weeks) {
170
- return adjust({
171
- focusedDate: focusedDate.subtract({ months: 1 }),
172
- startDate
173
- });
174
- }
175
- if (visibleDuration.months || visibleDuration.years) {
176
- return adjust({
177
- focusedDate: focusedDate.subtract({ years: 1 }),
178
- startDate
179
- });
180
- }
181
- }
182
-
183
- exports.getAdjustedDateFn = getAdjustedDateFn;
184
- exports.getNextPage = getNextPage;
185
- exports.getNextRow = getNextRow;
186
- exports.getNextSection = getNextSection;
187
- exports.getPreviousPage = getPreviousPage;
188
- exports.getPreviousRow = getPreviousRow;
189
- exports.getPreviousSection = getPreviousSection;
190
- exports.getSectionEnd = getSectionEnd;
191
- exports.getSectionStart = getSectionStart;
@@ -1,179 +0,0 @@
1
- import { startOfWeek, startOfMonth, endOfWeek, endOfMonth } from '@internationalized/date';
2
- import { isDateInvalid } from './assertion.mjs';
3
- import { constrainValue, alignEnd, alignStart, constrainStart } from './constrain.mjs';
4
- import { getEndDate, getUnitDuration } from './duration.mjs';
5
-
6
- function getAdjustedDateFn(visibleDuration, locale, minValue, maxValue) {
7
- return function getDate(options) {
8
- const { startDate, focusedDate } = options;
9
- const endDate = getEndDate(startDate, visibleDuration);
10
- if (isDateInvalid(focusedDate, minValue, maxValue)) {
11
- return {
12
- startDate,
13
- focusedDate: constrainValue(focusedDate, minValue, maxValue),
14
- endDate
15
- };
16
- }
17
- if (focusedDate.compare(startDate) < 0) {
18
- return {
19
- startDate: alignEnd(focusedDate, visibleDuration, locale, minValue, maxValue),
20
- endDate,
21
- focusedDate: constrainValue(focusedDate, minValue, maxValue)
22
- };
23
- }
24
- if (focusedDate.compare(endDate) > 0) {
25
- return {
26
- startDate: alignStart(focusedDate, visibleDuration, locale, minValue, maxValue),
27
- endDate,
28
- focusedDate: constrainValue(focusedDate, minValue, maxValue)
29
- };
30
- }
31
- return {
32
- startDate,
33
- endDate,
34
- focusedDate: constrainValue(focusedDate, minValue, maxValue)
35
- };
36
- };
37
- }
38
- function getNextPage(focusedDate, startDate, visibleDuration, locale, minValue, maxValue) {
39
- const adjust = getAdjustedDateFn(visibleDuration, locale, minValue, maxValue);
40
- const start = startDate.add(visibleDuration);
41
- return adjust({
42
- focusedDate: focusedDate.add(visibleDuration),
43
- startDate: alignStart(
44
- constrainStart(focusedDate, start, visibleDuration, locale, minValue, maxValue),
45
- visibleDuration,
46
- locale
47
- )
48
- });
49
- }
50
- function getPreviousPage(focusedDate, startDate, visibleDuration, locale, minValue, maxValue) {
51
- const adjust = getAdjustedDateFn(visibleDuration, locale, minValue, maxValue);
52
- let start = startDate.subtract(visibleDuration);
53
- return adjust({
54
- focusedDate: focusedDate.subtract(visibleDuration),
55
- startDate: alignStart(
56
- constrainStart(focusedDate, start, visibleDuration, locale, minValue, maxValue),
57
- visibleDuration,
58
- locale
59
- )
60
- });
61
- }
62
- function getNextRow(focusedDate, startDate, visibleDuration, locale, minValue, maxValue) {
63
- const adjust = getAdjustedDateFn(visibleDuration, locale, minValue, maxValue);
64
- if (visibleDuration.days) {
65
- return getNextPage(focusedDate, startDate, visibleDuration, locale, minValue, maxValue);
66
- }
67
- if (visibleDuration.weeks || visibleDuration.months || visibleDuration.years) {
68
- return adjust({
69
- focusedDate: focusedDate.add({ weeks: 1 }),
70
- startDate
71
- });
72
- }
73
- }
74
- function getPreviousRow(focusedDate, startDate, visibleDuration, locale, minValue, maxValue) {
75
- const adjust = getAdjustedDateFn(visibleDuration, locale, minValue, maxValue);
76
- if (visibleDuration.days) {
77
- return getPreviousPage(focusedDate, startDate, visibleDuration, locale, minValue, maxValue);
78
- }
79
- if (visibleDuration.weeks || visibleDuration.months || visibleDuration.years) {
80
- return adjust({
81
- focusedDate: focusedDate.subtract({ weeks: 1 }),
82
- startDate
83
- });
84
- }
85
- }
86
- function getSectionStart(focusedDate, startDate, visibleDuration, locale, minValue, maxValue) {
87
- const adjust = getAdjustedDateFn(visibleDuration, locale, minValue, maxValue);
88
- if (visibleDuration.days) {
89
- return adjust({
90
- focusedDate: startDate,
91
- startDate
92
- });
93
- }
94
- if (visibleDuration.weeks) {
95
- return adjust({
96
- focusedDate: startOfWeek(focusedDate, locale),
97
- startDate
98
- });
99
- }
100
- if (visibleDuration.months || visibleDuration.years) {
101
- return adjust({
102
- focusedDate: startOfMonth(focusedDate),
103
- startDate
104
- });
105
- }
106
- }
107
- function getSectionEnd(focusedDate, startDate, visibleDuration, locale, minValue, maxValue) {
108
- const adjust = getAdjustedDateFn(visibleDuration, locale, minValue, maxValue);
109
- const endDate = getEndDate(startDate, visibleDuration);
110
- if (visibleDuration.days) {
111
- return adjust({
112
- focusedDate: endDate,
113
- startDate
114
- });
115
- }
116
- if (visibleDuration.weeks) {
117
- return adjust({
118
- //@ts-expect-error - endOfWeek is loosely typed
119
- focusedDate: endOfWeek(focusedDate, locale),
120
- startDate
121
- });
122
- }
123
- if (visibleDuration.months || visibleDuration.years) {
124
- return adjust({
125
- focusedDate: endOfMonth(focusedDate),
126
- startDate
127
- });
128
- }
129
- }
130
- function getNextSection(focusedDate, startDate, larger, visibleDuration, locale, minValue, maxValue) {
131
- const adjust = getAdjustedDateFn(visibleDuration, locale, minValue, maxValue);
132
- if (!larger && !visibleDuration.days) {
133
- return adjust({
134
- focusedDate: focusedDate.add(getUnitDuration(visibleDuration)),
135
- startDate
136
- });
137
- }
138
- if (visibleDuration.days) {
139
- return getNextPage(focusedDate, startDate, visibleDuration, locale, minValue, maxValue);
140
- }
141
- if (visibleDuration.weeks) {
142
- return adjust({
143
- focusedDate: focusedDate.add({ months: 1 }),
144
- startDate
145
- });
146
- }
147
- if (visibleDuration.months || visibleDuration.years) {
148
- return adjust({
149
- focusedDate: focusedDate.add({ years: 1 }),
150
- startDate
151
- });
152
- }
153
- }
154
- function getPreviousSection(focusedDate, startDate, larger, visibleDuration, locale, minValue, maxValue) {
155
- const adjust = getAdjustedDateFn(visibleDuration, locale, minValue, maxValue);
156
- if (!larger && !visibleDuration.days) {
157
- return adjust({
158
- focusedDate: focusedDate.subtract(getUnitDuration(visibleDuration)),
159
- startDate
160
- });
161
- }
162
- if (visibleDuration.days) {
163
- return getPreviousPage(focusedDate, startDate, visibleDuration, locale, minValue, maxValue);
164
- }
165
- if (visibleDuration.weeks) {
166
- return adjust({
167
- focusedDate: focusedDate.subtract({ months: 1 }),
168
- startDate
169
- });
170
- }
171
- if (visibleDuration.months || visibleDuration.years) {
172
- return adjust({
173
- focusedDate: focusedDate.subtract({ years: 1 }),
174
- startDate
175
- });
176
- }
177
- }
178
-
179
- export { getAdjustedDateFn, getNextPage, getNextRow, getNextSection, getPreviousPage, getPreviousRow, getPreviousSection, getSectionEnd, getSectionStart };
@@ -1,2 +0,0 @@
1
- import { CalendarDateTime } from "@internationalized/date";
2
- export declare function parseDateString(date: string, locale: string, timeZone: string): CalendarDateTime | undefined;
@@ -1,44 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
-
5
- const date = require('@internationalized/date');
6
-
7
- function parseDateString(date$1, locale, timeZone) {
8
- const regex = createRegex(locale, timeZone);
9
- const { year, month, day } = extract(regex, date$1) ?? {};
10
- if (year != null && year.length === 4 && month != null && +month <= 12 && day != null && +day <= 31) {
11
- return new date.CalendarDateTime(+year, +month, +day);
12
- }
13
- const time = Date.parse(date$1);
14
- if (!isNaN(time)) {
15
- const date2 = new Date(time);
16
- return new date.CalendarDateTime(date2.getFullYear(), date2.getMonth() + 1, date2.getDate());
17
- }
18
- }
19
- function createRegex(locale, timeZone) {
20
- const formatter = new date.DateFormatter(locale, { day: "numeric", month: "numeric", year: "numeric", timeZone });
21
- const parts = formatter.formatToParts(new Date(2e3, 11, 25));
22
- return parts.map(({ type, value }) => type === "literal" ? value : `((?!=<${type}>)\\d+)`).join("");
23
- }
24
- function extract(pattern, str) {
25
- const matches = str.match(pattern);
26
- return pattern.toString().match(/<(.+?)>/g)?.map((group) => {
27
- const groupMatches = group.match(/<(.+)>/);
28
- if (!groupMatches || groupMatches.length <= 0) {
29
- return null;
30
- }
31
- return group.match(/<(.+)>/)?.[1];
32
- }).reduce((acc, curr, index) => {
33
- if (!curr)
34
- return acc;
35
- if (matches && matches.length > index) {
36
- acc[curr] = matches[index + 1];
37
- } else {
38
- acc[curr] = null;
39
- }
40
- return acc;
41
- }, {});
42
- }
43
-
44
- exports.parseDateString = parseDateString;
@@ -1,40 +0,0 @@
1
- import { CalendarDateTime, DateFormatter } from '@internationalized/date';
2
-
3
- function parseDateString(date, locale, timeZone) {
4
- const regex = createRegex(locale, timeZone);
5
- const { year, month, day } = extract(regex, date) ?? {};
6
- if (year != null && year.length === 4 && month != null && +month <= 12 && day != null && +day <= 31) {
7
- return new CalendarDateTime(+year, +month, +day);
8
- }
9
- const time = Date.parse(date);
10
- if (!isNaN(time)) {
11
- const date2 = new Date(time);
12
- return new CalendarDateTime(date2.getFullYear(), date2.getMonth() + 1, date2.getDate());
13
- }
14
- }
15
- function createRegex(locale, timeZone) {
16
- const formatter = new DateFormatter(locale, { day: "numeric", month: "numeric", year: "numeric", timeZone });
17
- const parts = formatter.formatToParts(new Date(2e3, 11, 25));
18
- return parts.map(({ type, value }) => type === "literal" ? value : `((?!=<${type}>)\\d+)`).join("");
19
- }
20
- function extract(pattern, str) {
21
- const matches = str.match(pattern);
22
- return pattern.toString().match(/<(.+?)>/g)?.map((group) => {
23
- const groupMatches = group.match(/<(.+)>/);
24
- if (!groupMatches || groupMatches.length <= 0) {
25
- return null;
26
- }
27
- return group.match(/<(.+)>/)?.[1];
28
- }).reduce((acc, curr, index) => {
29
- if (!curr)
30
- return acc;
31
- if (matches && matches.length > index) {
32
- acc[curr] = matches[index + 1];
33
- } else {
34
- acc[curr] = null;
35
- }
36
- return acc;
37
- }, {});
38
- }
39
-
40
- export { parseDateString };
package/dist/types.d.ts DELETED
@@ -1,18 +0,0 @@
1
- import type { DateFormatter, DateValue } from "@internationalized/date";
2
- export type DateGranularity = "day" | "hour" | "minute" | "second" | "year" | "month";
3
- export type DateAlignment = "start" | "end" | "center";
4
- export type GetFormatterFn = (options: Intl.DateTimeFormatOptions) => DateFormatter;
5
- export type DateAvailableFn = (date: DateValue, locale: string) => boolean;
6
- export type GetPlaceholderFn = (options: {
7
- field: string;
8
- locale: string;
9
- }) => string;
10
- export type DateAdjustFn = (options: {
11
- startDate: DateValue;
12
- focusedDate: DateValue;
13
- }) => {
14
- startDate: DateValue;
15
- focusedDate: DateValue;
16
- endDate: DateValue;
17
- };
18
- export type DateFormatOptions = Intl.ResolvedDateTimeFormatOptions;