ts-time-utils 1.0.0 → 2.0.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.
- package/README.md +682 -11
- package/dist/calculate.d.ts +7 -2
- package/dist/calculate.d.ts.map +1 -1
- package/dist/calculate.js +37 -13
- package/dist/calendar.d.ts +103 -0
- package/dist/calendar.d.ts.map +1 -1
- package/dist/calendar.js +224 -0
- package/dist/compare.d.ts +217 -0
- package/dist/compare.d.ts.map +1 -0
- package/dist/compare.js +417 -0
- package/dist/countdown.d.ts +217 -0
- package/dist/countdown.d.ts.map +1 -0
- package/dist/countdown.js +298 -0
- package/dist/cron.d.ts +82 -0
- package/dist/cron.d.ts.map +1 -0
- package/dist/cron.js +294 -0
- package/dist/dateRange.d.ts +266 -0
- package/dist/dateRange.d.ts.map +1 -0
- package/dist/dateRange.js +433 -0
- package/dist/esm/calculate.d.ts +7 -2
- package/dist/esm/calculate.d.ts.map +1 -1
- package/dist/esm/calculate.js +37 -13
- package/dist/esm/calendar.d.ts +103 -0
- package/dist/esm/calendar.d.ts.map +1 -1
- package/dist/esm/calendar.js +224 -0
- package/dist/esm/compare.d.ts +217 -0
- package/dist/esm/compare.d.ts.map +1 -0
- package/dist/esm/compare.js +417 -0
- package/dist/esm/countdown.d.ts +217 -0
- package/dist/esm/countdown.d.ts.map +1 -0
- package/dist/esm/countdown.js +298 -0
- package/dist/esm/cron.d.ts +82 -0
- package/dist/esm/cron.d.ts.map +1 -0
- package/dist/esm/cron.js +294 -0
- package/dist/esm/dateRange.d.ts +266 -0
- package/dist/esm/dateRange.d.ts.map +1 -0
- package/dist/esm/dateRange.js +433 -0
- package/dist/esm/fiscal.d.ts +195 -0
- package/dist/esm/fiscal.d.ts.map +1 -0
- package/dist/esm/fiscal.js +295 -0
- package/dist/esm/format.d.ts +65 -0
- package/dist/esm/format.d.ts.map +1 -1
- package/dist/esm/format.js +202 -0
- package/dist/esm/index.d.ts +18 -7
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +22 -6
- package/dist/esm/iterate.d.ts +212 -0
- package/dist/esm/iterate.d.ts.map +1 -0
- package/dist/esm/iterate.js +409 -0
- package/dist/esm/naturalLanguage.d.ts +107 -0
- package/dist/esm/naturalLanguage.d.ts.map +1 -0
- package/dist/esm/naturalLanguage.js +344 -0
- package/dist/esm/parse.d.ts +45 -0
- package/dist/esm/parse.d.ts.map +1 -1
- package/dist/esm/parse.js +207 -0
- package/dist/esm/recurrence.d.ts +149 -0
- package/dist/esm/recurrence.d.ts.map +1 -0
- package/dist/esm/recurrence.js +404 -0
- package/dist/esm/timezone.d.ts +52 -0
- package/dist/esm/timezone.d.ts.map +1 -1
- package/dist/esm/timezone.js +171 -0
- package/dist/esm/types.d.ts +21 -0
- package/dist/esm/types.d.ts.map +1 -1
- package/dist/esm/validate.d.ts +51 -0
- package/dist/esm/validate.d.ts.map +1 -1
- package/dist/esm/validate.js +92 -0
- package/dist/esm/workingHours.d.ts +70 -0
- package/dist/esm/workingHours.d.ts.map +1 -1
- package/dist/esm/workingHours.js +161 -0
- package/dist/fiscal.d.ts +195 -0
- package/dist/fiscal.d.ts.map +1 -0
- package/dist/fiscal.js +295 -0
- package/dist/format.d.ts +65 -0
- package/dist/format.d.ts.map +1 -1
- package/dist/format.js +202 -0
- package/dist/index.d.ts +18 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +22 -6
- package/dist/iterate.d.ts +212 -0
- package/dist/iterate.d.ts.map +1 -0
- package/dist/iterate.js +409 -0
- package/dist/naturalLanguage.d.ts +107 -0
- package/dist/naturalLanguage.d.ts.map +1 -0
- package/dist/naturalLanguage.js +344 -0
- package/dist/parse.d.ts +45 -0
- package/dist/parse.d.ts.map +1 -1
- package/dist/parse.js +207 -0
- package/dist/recurrence.d.ts +149 -0
- package/dist/recurrence.d.ts.map +1 -0
- package/dist/recurrence.js +404 -0
- package/dist/timezone.d.ts +52 -0
- package/dist/timezone.d.ts.map +1 -1
- package/dist/timezone.js +171 -0
- package/dist/types.d.ts +21 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/validate.d.ts +51 -0
- package/dist/validate.d.ts.map +1 -1
- package/dist/validate.js +92 -0
- package/dist/workingHours.d.ts +70 -0
- package/dist/workingHours.d.ts.map +1 -1
- package/dist/workingHours.js +161 -0
- package/package.json +59 -12
|
@@ -0,0 +1,409 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Date iteration utilities for generating date sequences
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Generate an array of dates for each day in a range
|
|
6
|
+
* @param start - Start date (inclusive)
|
|
7
|
+
* @param end - End date (inclusive)
|
|
8
|
+
* @returns Array of dates, one per day
|
|
9
|
+
* @example
|
|
10
|
+
* eachDay(new Date('2024-01-01'), new Date('2024-01-05'))
|
|
11
|
+
* // [Jan 1, Jan 2, Jan 3, Jan 4, Jan 5]
|
|
12
|
+
*/
|
|
13
|
+
export function eachDay(start, end) {
|
|
14
|
+
const result = [];
|
|
15
|
+
const current = new Date(start.getFullYear(), start.getMonth(), start.getDate());
|
|
16
|
+
const endDate = new Date(end.getFullYear(), end.getMonth(), end.getDate());
|
|
17
|
+
while (current <= endDate) {
|
|
18
|
+
result.push(new Date(current));
|
|
19
|
+
current.setDate(current.getDate() + 1);
|
|
20
|
+
}
|
|
21
|
+
return result;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Generate an array of dates for each weekday (Mon-Fri) in a range
|
|
25
|
+
* @param start - Start date (inclusive)
|
|
26
|
+
* @param end - End date (inclusive)
|
|
27
|
+
* @returns Array of weekday dates
|
|
28
|
+
* @example
|
|
29
|
+
* eachWeekday(new Date('2024-01-01'), new Date('2024-01-07'))
|
|
30
|
+
* // [Jan 1 (Mon), Jan 2 (Tue), Jan 3 (Wed), Jan 4 (Thu), Jan 5 (Fri)]
|
|
31
|
+
*/
|
|
32
|
+
export function eachWeekday(start, end) {
|
|
33
|
+
const result = [];
|
|
34
|
+
const current = new Date(start.getFullYear(), start.getMonth(), start.getDate());
|
|
35
|
+
const endDate = new Date(end.getFullYear(), end.getMonth(), end.getDate());
|
|
36
|
+
while (current <= endDate) {
|
|
37
|
+
const day = current.getDay();
|
|
38
|
+
if (day !== 0 && day !== 6) {
|
|
39
|
+
result.push(new Date(current));
|
|
40
|
+
}
|
|
41
|
+
current.setDate(current.getDate() + 1);
|
|
42
|
+
}
|
|
43
|
+
return result;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Generate an array of dates for each weekend day (Sat-Sun) in a range
|
|
47
|
+
* @param start - Start date (inclusive)
|
|
48
|
+
* @param end - End date (inclusive)
|
|
49
|
+
* @returns Array of weekend dates
|
|
50
|
+
*/
|
|
51
|
+
export function eachWeekend(start, end) {
|
|
52
|
+
const result = [];
|
|
53
|
+
const current = new Date(start.getFullYear(), start.getMonth(), start.getDate());
|
|
54
|
+
const endDate = new Date(end.getFullYear(), end.getMonth(), end.getDate());
|
|
55
|
+
while (current <= endDate) {
|
|
56
|
+
const day = current.getDay();
|
|
57
|
+
if (day === 0 || day === 6) {
|
|
58
|
+
result.push(new Date(current));
|
|
59
|
+
}
|
|
60
|
+
current.setDate(current.getDate() + 1);
|
|
61
|
+
}
|
|
62
|
+
return result;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Generate an array of dates for each week start in a range
|
|
66
|
+
* @param start - Start date (inclusive)
|
|
67
|
+
* @param end - End date (inclusive)
|
|
68
|
+
* @param weekStartsOn - Day week starts on (0=Sunday, 1=Monday, default: 0)
|
|
69
|
+
* @returns Array of week start dates
|
|
70
|
+
* @example
|
|
71
|
+
* eachWeek(new Date('2024-01-01'), new Date('2024-01-31'))
|
|
72
|
+
* // [Jan 7, Jan 14, Jan 21, Jan 28] (Sundays)
|
|
73
|
+
*/
|
|
74
|
+
export function eachWeek(start, end, weekStartsOn = 0) {
|
|
75
|
+
const result = [];
|
|
76
|
+
const current = new Date(start.getFullYear(), start.getMonth(), start.getDate());
|
|
77
|
+
const endDate = new Date(end.getFullYear(), end.getMonth(), end.getDate());
|
|
78
|
+
// Move to first week start on or after start date
|
|
79
|
+
const dayDiff = (current.getDay() - weekStartsOn + 7) % 7;
|
|
80
|
+
if (dayDiff > 0) {
|
|
81
|
+
current.setDate(current.getDate() + (7 - dayDiff));
|
|
82
|
+
}
|
|
83
|
+
while (current <= endDate) {
|
|
84
|
+
result.push(new Date(current));
|
|
85
|
+
current.setDate(current.getDate() + 7);
|
|
86
|
+
}
|
|
87
|
+
return result;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Generate an array of dates for the first day of each month in a range
|
|
91
|
+
* @param start - Start date (inclusive)
|
|
92
|
+
* @param end - End date (inclusive)
|
|
93
|
+
* @returns Array of first-of-month dates
|
|
94
|
+
* @example
|
|
95
|
+
* eachMonth(new Date('2024-01-15'), new Date('2024-04-15'))
|
|
96
|
+
* // [Feb 1, Mar 1, Apr 1]
|
|
97
|
+
*/
|
|
98
|
+
export function eachMonth(start, end) {
|
|
99
|
+
const result = [];
|
|
100
|
+
const current = new Date(start.getFullYear(), start.getMonth() + 1, 1);
|
|
101
|
+
const endDate = new Date(end.getFullYear(), end.getMonth(), end.getDate());
|
|
102
|
+
while (current <= endDate) {
|
|
103
|
+
result.push(new Date(current));
|
|
104
|
+
current.setMonth(current.getMonth() + 1);
|
|
105
|
+
}
|
|
106
|
+
return result;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Generate an array of dates for the first day of each quarter in a range
|
|
110
|
+
* @param start - Start date (inclusive)
|
|
111
|
+
* @param end - End date (inclusive)
|
|
112
|
+
* @returns Array of quarter start dates
|
|
113
|
+
*/
|
|
114
|
+
export function eachQuarter(start, end) {
|
|
115
|
+
const result = [];
|
|
116
|
+
// Start from beginning of next quarter after start date
|
|
117
|
+
const startMonth = start.getMonth();
|
|
118
|
+
const nextQuarterMonth = Math.ceil((startMonth + 1) / 3) * 3;
|
|
119
|
+
const current = new Date(nextQuarterMonth > 11 ? start.getFullYear() + 1 : start.getFullYear(), nextQuarterMonth % 12, 1);
|
|
120
|
+
const endDate = new Date(end.getFullYear(), end.getMonth(), end.getDate());
|
|
121
|
+
while (current <= endDate) {
|
|
122
|
+
result.push(new Date(current));
|
|
123
|
+
current.setMonth(current.getMonth() + 3);
|
|
124
|
+
}
|
|
125
|
+
return result;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Generate an array of dates for January 1st of each year in a range
|
|
129
|
+
* @param start - Start date (inclusive)
|
|
130
|
+
* @param end - End date (inclusive)
|
|
131
|
+
* @returns Array of year start dates
|
|
132
|
+
*/
|
|
133
|
+
export function eachYear(start, end) {
|
|
134
|
+
const result = [];
|
|
135
|
+
const startYear = start.getMonth() === 0 && start.getDate() === 1
|
|
136
|
+
? start.getFullYear()
|
|
137
|
+
: start.getFullYear() + 1;
|
|
138
|
+
const endYear = end.getFullYear();
|
|
139
|
+
for (let year = startYear; year <= endYear; year++) {
|
|
140
|
+
result.push(new Date(year, 0, 1));
|
|
141
|
+
}
|
|
142
|
+
return result;
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Generate an array of dates at regular hour intervals
|
|
146
|
+
* @param start - Start date/time (inclusive)
|
|
147
|
+
* @param end - End date/time (inclusive)
|
|
148
|
+
* @param step - Hour interval (default: 1)
|
|
149
|
+
* @returns Array of dates at each interval
|
|
150
|
+
* @example
|
|
151
|
+
* eachHour(new Date('2024-01-01T09:00'), new Date('2024-01-01T12:00'))
|
|
152
|
+
* // [9:00, 10:00, 11:00, 12:00]
|
|
153
|
+
*/
|
|
154
|
+
export function eachHour(start, end, step = 1) {
|
|
155
|
+
const result = [];
|
|
156
|
+
const current = new Date(start);
|
|
157
|
+
current.setMinutes(0, 0, 0);
|
|
158
|
+
while (current <= end) {
|
|
159
|
+
result.push(new Date(current));
|
|
160
|
+
current.setHours(current.getHours() + step);
|
|
161
|
+
}
|
|
162
|
+
return result;
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Generate an array of dates at regular minute intervals
|
|
166
|
+
* @param start - Start date/time (inclusive)
|
|
167
|
+
* @param end - End date/time (inclusive)
|
|
168
|
+
* @param step - Minute interval (default: 1)
|
|
169
|
+
* @returns Array of dates at each interval
|
|
170
|
+
* @example
|
|
171
|
+
* eachMinute(new Date('2024-01-01T09:00'), new Date('2024-01-01T09:05'))
|
|
172
|
+
* // [9:00, 9:01, 9:02, 9:03, 9:04, 9:05]
|
|
173
|
+
*/
|
|
174
|
+
export function eachMinute(start, end, step = 1) {
|
|
175
|
+
const result = [];
|
|
176
|
+
const current = new Date(start);
|
|
177
|
+
current.setSeconds(0, 0);
|
|
178
|
+
while (current <= end) {
|
|
179
|
+
result.push(new Date(current));
|
|
180
|
+
current.setMinutes(current.getMinutes() + step);
|
|
181
|
+
}
|
|
182
|
+
return result;
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Generate an array of dates for a specific day of the week in a range
|
|
186
|
+
* @param start - Start date (inclusive)
|
|
187
|
+
* @param end - End date (inclusive)
|
|
188
|
+
* @param dayOfWeek - Day of week (0=Sunday, 1=Monday, ..., 6=Saturday)
|
|
189
|
+
* @returns Array of dates for that day of week
|
|
190
|
+
* @example
|
|
191
|
+
* eachDayOfWeek(new Date('2024-01-01'), new Date('2024-01-31'), 1)
|
|
192
|
+
* // All Mondays in January 2024
|
|
193
|
+
*/
|
|
194
|
+
export function eachDayOfWeek(start, end, dayOfWeek) {
|
|
195
|
+
const result = [];
|
|
196
|
+
const current = new Date(start.getFullYear(), start.getMonth(), start.getDate());
|
|
197
|
+
const endDate = new Date(end.getFullYear(), end.getMonth(), end.getDate());
|
|
198
|
+
// Move to first occurrence of dayOfWeek
|
|
199
|
+
const diff = (dayOfWeek - current.getDay() + 7) % 7;
|
|
200
|
+
current.setDate(current.getDate() + diff);
|
|
201
|
+
while (current <= endDate) {
|
|
202
|
+
result.push(new Date(current));
|
|
203
|
+
current.setDate(current.getDate() + 7);
|
|
204
|
+
}
|
|
205
|
+
return result;
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* Generate dates at a custom interval
|
|
209
|
+
* @param start - Start date (inclusive)
|
|
210
|
+
* @param end - End date (inclusive)
|
|
211
|
+
* @param interval - Interval configuration
|
|
212
|
+
* @returns Array of dates at each interval
|
|
213
|
+
* @example
|
|
214
|
+
* eachInterval(start, end, { days: 3 }) // Every 3 days
|
|
215
|
+
* eachInterval(start, end, { weeks: 2 }) // Every 2 weeks
|
|
216
|
+
* eachInterval(start, end, { hours: 6 }) // Every 6 hours
|
|
217
|
+
*/
|
|
218
|
+
export function eachInterval(start, end, interval) {
|
|
219
|
+
const result = [];
|
|
220
|
+
const current = new Date(start);
|
|
221
|
+
while (current <= end) {
|
|
222
|
+
result.push(new Date(current));
|
|
223
|
+
if (interval.years) {
|
|
224
|
+
current.setFullYear(current.getFullYear() + interval.years);
|
|
225
|
+
}
|
|
226
|
+
if (interval.months) {
|
|
227
|
+
current.setMonth(current.getMonth() + interval.months);
|
|
228
|
+
}
|
|
229
|
+
if (interval.weeks) {
|
|
230
|
+
current.setDate(current.getDate() + interval.weeks * 7);
|
|
231
|
+
}
|
|
232
|
+
if (interval.days) {
|
|
233
|
+
current.setDate(current.getDate() + interval.days);
|
|
234
|
+
}
|
|
235
|
+
if (interval.hours) {
|
|
236
|
+
current.setHours(current.getHours() + interval.hours);
|
|
237
|
+
}
|
|
238
|
+
if (interval.minutes) {
|
|
239
|
+
current.setMinutes(current.getMinutes() + interval.minutes);
|
|
240
|
+
}
|
|
241
|
+
if (interval.seconds) {
|
|
242
|
+
current.setSeconds(current.getSeconds() + interval.seconds);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
return result;
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* Count the number of days in a range
|
|
249
|
+
* @param start - Start date (inclusive)
|
|
250
|
+
* @param end - End date (inclusive)
|
|
251
|
+
* @returns Number of days
|
|
252
|
+
*/
|
|
253
|
+
export function countDays(start, end) {
|
|
254
|
+
const startDay = new Date(start.getFullYear(), start.getMonth(), start.getDate());
|
|
255
|
+
const endDay = new Date(end.getFullYear(), end.getMonth(), end.getDate());
|
|
256
|
+
return Math.round((endDay.getTime() - startDay.getTime()) / 86400000) + 1;
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* Count the number of weekdays in a range
|
|
260
|
+
* @param start - Start date (inclusive)
|
|
261
|
+
* @param end - End date (inclusive)
|
|
262
|
+
* @returns Number of weekdays
|
|
263
|
+
*/
|
|
264
|
+
export function countWeekdays(start, end) {
|
|
265
|
+
return eachWeekday(start, end).length;
|
|
266
|
+
}
|
|
267
|
+
/**
|
|
268
|
+
* Count the number of weekend days in a range
|
|
269
|
+
* @param start - Start date (inclusive)
|
|
270
|
+
* @param end - End date (inclusive)
|
|
271
|
+
* @returns Number of weekend days
|
|
272
|
+
*/
|
|
273
|
+
export function countWeekendDays(start, end) {
|
|
274
|
+
return eachWeekend(start, end).length;
|
|
275
|
+
}
|
|
276
|
+
/**
|
|
277
|
+
* Count the number of weeks (complete or partial) in a range
|
|
278
|
+
* @param start - Start date (inclusive)
|
|
279
|
+
* @param end - End date (inclusive)
|
|
280
|
+
* @returns Number of weeks
|
|
281
|
+
*/
|
|
282
|
+
export function countWeeks(start, end) {
|
|
283
|
+
const days = countDays(start, end);
|
|
284
|
+
return Math.ceil(days / 7);
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* Count the number of months in a range
|
|
288
|
+
* @param start - Start date (inclusive)
|
|
289
|
+
* @param end - End date (inclusive)
|
|
290
|
+
* @returns Number of months (partial months count as 1)
|
|
291
|
+
*/
|
|
292
|
+
export function countMonths(start, end) {
|
|
293
|
+
const yearDiff = end.getFullYear() - start.getFullYear();
|
|
294
|
+
const monthDiff = end.getMonth() - start.getMonth();
|
|
295
|
+
return yearDiff * 12 + monthDiff + 1;
|
|
296
|
+
}
|
|
297
|
+
/**
|
|
298
|
+
* Iterator for lazy date generation (memory efficient for large ranges)
|
|
299
|
+
* @param start - Start date (inclusive)
|
|
300
|
+
* @param end - End date (inclusive)
|
|
301
|
+
* @param step - Step function to advance the date
|
|
302
|
+
* @yields Date objects
|
|
303
|
+
*/
|
|
304
|
+
export function* iterateDates(start, end, step = (d) => d.setDate(d.getDate() + 1)) {
|
|
305
|
+
const current = new Date(start);
|
|
306
|
+
while (current <= end) {
|
|
307
|
+
yield new Date(current);
|
|
308
|
+
step(current);
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
/**
|
|
312
|
+
* Create a lazy day iterator
|
|
313
|
+
* @param start - Start date
|
|
314
|
+
* @param end - End date
|
|
315
|
+
* @yields Each day in the range
|
|
316
|
+
*/
|
|
317
|
+
export function* iterateDays(start, end) {
|
|
318
|
+
yield* iterateDates(start, end, (d) => d.setDate(d.getDate() + 1));
|
|
319
|
+
}
|
|
320
|
+
/**
|
|
321
|
+
* Create a lazy weekday iterator
|
|
322
|
+
* @param start - Start date
|
|
323
|
+
* @param end - End date
|
|
324
|
+
* @yields Each weekday in the range
|
|
325
|
+
*/
|
|
326
|
+
export function* iterateWeekdays(start, end) {
|
|
327
|
+
const current = new Date(start.getFullYear(), start.getMonth(), start.getDate());
|
|
328
|
+
while (current <= end) {
|
|
329
|
+
const day = current.getDay();
|
|
330
|
+
if (day !== 0 && day !== 6) {
|
|
331
|
+
yield new Date(current);
|
|
332
|
+
}
|
|
333
|
+
current.setDate(current.getDate() + 1);
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
/**
|
|
337
|
+
* Create a lazy month iterator
|
|
338
|
+
* @param start - Start date
|
|
339
|
+
* @param end - End date
|
|
340
|
+
* @yields First day of each month in the range
|
|
341
|
+
*/
|
|
342
|
+
export function* iterateMonths(start, end) {
|
|
343
|
+
const current = new Date(start.getFullYear(), start.getMonth(), 1);
|
|
344
|
+
while (current <= end) {
|
|
345
|
+
yield new Date(current);
|
|
346
|
+
current.setMonth(current.getMonth() + 1);
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
/**
|
|
350
|
+
* Generate dates by applying a filter to a range
|
|
351
|
+
* @param start - Start date
|
|
352
|
+
* @param end - End date
|
|
353
|
+
* @param filter - Filter function (return true to include date)
|
|
354
|
+
* @returns Array of filtered dates
|
|
355
|
+
* @example
|
|
356
|
+
* // Get all 15th days of each month
|
|
357
|
+
* filterDays(start, end, d => d.getDate() === 15)
|
|
358
|
+
*/
|
|
359
|
+
export function filterDays(start, end, filter) {
|
|
360
|
+
const result = [];
|
|
361
|
+
for (const date of iterateDays(start, end)) {
|
|
362
|
+
if (filter(date)) {
|
|
363
|
+
result.push(date);
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
return result;
|
|
367
|
+
}
|
|
368
|
+
/**
|
|
369
|
+
* Generate the last day of each month in a range
|
|
370
|
+
* @param start - Start date
|
|
371
|
+
* @param end - End date
|
|
372
|
+
* @returns Array of last-of-month dates
|
|
373
|
+
*/
|
|
374
|
+
export function eachMonthEnd(start, end) {
|
|
375
|
+
const result = [];
|
|
376
|
+
const current = new Date(start.getFullYear(), start.getMonth() + 1, 0);
|
|
377
|
+
const endDate = new Date(end.getFullYear(), end.getMonth(), end.getDate());
|
|
378
|
+
while (current <= endDate) {
|
|
379
|
+
result.push(new Date(current));
|
|
380
|
+
current.setMonth(current.getMonth() + 2);
|
|
381
|
+
current.setDate(0); // Last day of previous month
|
|
382
|
+
}
|
|
383
|
+
return result;
|
|
384
|
+
}
|
|
385
|
+
/**
|
|
386
|
+
* Generate specific day of each month in a range
|
|
387
|
+
* @param start - Start date
|
|
388
|
+
* @param end - End date
|
|
389
|
+
* @param dayOfMonth - Day of month (1-31, will cap at month's max)
|
|
390
|
+
* @returns Array of dates
|
|
391
|
+
* @example
|
|
392
|
+
* eachNthDayOfMonth(start, end, 15) // 15th of each month
|
|
393
|
+
* eachNthDayOfMonth(start, end, 31) // Last day of short months, 31st otherwise
|
|
394
|
+
*/
|
|
395
|
+
export function eachNthDayOfMonth(start, end, dayOfMonth) {
|
|
396
|
+
const result = [];
|
|
397
|
+
let current = new Date(start.getFullYear(), start.getMonth(), 1);
|
|
398
|
+
const endDate = new Date(end.getFullYear(), end.getMonth(), end.getDate());
|
|
399
|
+
while (current <= endDate) {
|
|
400
|
+
const daysInMonth = new Date(current.getFullYear(), current.getMonth() + 1, 0).getDate();
|
|
401
|
+
const actualDay = Math.min(dayOfMonth, daysInMonth);
|
|
402
|
+
const date = new Date(current.getFullYear(), current.getMonth(), actualDay);
|
|
403
|
+
if (date >= start && date <= endDate) {
|
|
404
|
+
result.push(date);
|
|
405
|
+
}
|
|
406
|
+
current.setMonth(current.getMonth() + 1);
|
|
407
|
+
}
|
|
408
|
+
return result;
|
|
409
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Natural language date parsing utilities
|
|
3
|
+
* Provides intelligent parsing of human-readable date strings
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Options for natural language parsing
|
|
7
|
+
*/
|
|
8
|
+
export interface NaturalParseOptions {
|
|
9
|
+
/** Reference date for relative parsing (defaults to now) */
|
|
10
|
+
referenceDate?: Date;
|
|
11
|
+
/** Preferred time for dates without time specified */
|
|
12
|
+
defaultTime?: {
|
|
13
|
+
hour: number;
|
|
14
|
+
minute: number;
|
|
15
|
+
second?: number;
|
|
16
|
+
};
|
|
17
|
+
/** Locale for language-specific parsing */
|
|
18
|
+
locale?: string;
|
|
19
|
+
/** Whether to use strict parsing */
|
|
20
|
+
strict?: boolean;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Extracted date information from text
|
|
24
|
+
*/
|
|
25
|
+
export interface ExtractedDate {
|
|
26
|
+
/** The parsed date */
|
|
27
|
+
date: Date;
|
|
28
|
+
/** The original text that was matched */
|
|
29
|
+
text: string;
|
|
30
|
+
/** Starting position in the original text */
|
|
31
|
+
index: number;
|
|
32
|
+
/** Type of date pattern matched */
|
|
33
|
+
type: 'absolute' | 'relative' | 'range' | 'time';
|
|
34
|
+
/** Confidence score (0-1) */
|
|
35
|
+
confidence: number;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Parses natural language date strings into Date objects
|
|
39
|
+
* @param input - Natural language date string
|
|
40
|
+
* @param options - Parsing options
|
|
41
|
+
* @returns Parsed date or null if unable to parse
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* ```ts
|
|
45
|
+
* parseNaturalDate('tomorrow at 3pm');
|
|
46
|
+
* // Date for tomorrow at 15:00
|
|
47
|
+
*
|
|
48
|
+
* parseNaturalDate('next Friday');
|
|
49
|
+
* // Date for next Friday
|
|
50
|
+
*
|
|
51
|
+
* parseNaturalDate('in 2 weeks');
|
|
52
|
+
* // Date 2 weeks from now
|
|
53
|
+
*
|
|
54
|
+
* parseNaturalDate('December 25th');
|
|
55
|
+
* // Date for Dec 25 this year (or next if passed)
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
58
|
+
export declare function parseNaturalDate(input: string, options?: NaturalParseOptions): Date | null;
|
|
59
|
+
/**
|
|
60
|
+
* Parses a relative time phrase into a Date
|
|
61
|
+
* @param input - Relative time phrase
|
|
62
|
+
* @param referenceDate - Reference date (defaults to now)
|
|
63
|
+
* @returns Parsed date or null
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* ```ts
|
|
67
|
+
* parseRelativePhrase('in 2 hours'); // 2 hours from now
|
|
68
|
+
* parseRelativePhrase('5 minutes ago'); // 5 minutes ago
|
|
69
|
+
* parseRelativePhrase('next week'); // 7 days from now
|
|
70
|
+
* ```
|
|
71
|
+
*/
|
|
72
|
+
export declare function parseRelativePhrase(input: string, referenceDate?: Date): Date | null;
|
|
73
|
+
/**
|
|
74
|
+
* Extracts all dates from a text string
|
|
75
|
+
* @param text - Text to extract dates from
|
|
76
|
+
* @param options - Extraction options
|
|
77
|
+
* @returns Array of extracted date information
|
|
78
|
+
*
|
|
79
|
+
* @example
|
|
80
|
+
* ```ts
|
|
81
|
+
* const text = "Meeting tomorrow at 3pm and dinner next Friday at 7pm";
|
|
82
|
+
* const dates = extractDatesFromText(text);
|
|
83
|
+
* // [
|
|
84
|
+
* // { date: Date(...), text: 'tomorrow at 3pm', index: 8, ... },
|
|
85
|
+
* // { date: Date(...), text: 'next Friday at 7pm', index: 37, ... }
|
|
86
|
+
* // ]
|
|
87
|
+
* ```
|
|
88
|
+
*/
|
|
89
|
+
export declare function extractDatesFromText(text: string, options?: NaturalParseOptions): ExtractedDate[];
|
|
90
|
+
/**
|
|
91
|
+
* Suggests possible dates based on context
|
|
92
|
+
* @param context - Context string to analyze
|
|
93
|
+
* @param options - Parsing options
|
|
94
|
+
* @returns Array of suggested dates with confidence scores
|
|
95
|
+
*
|
|
96
|
+
* @example
|
|
97
|
+
* ```ts
|
|
98
|
+
* suggestDateFromContext('deadline is end of month');
|
|
99
|
+
* // [{ date: Date(last day of current month), confidence: 0.8, ... }]
|
|
100
|
+
* ```
|
|
101
|
+
*/
|
|
102
|
+
export declare function suggestDateFromContext(context: string, options?: NaturalParseOptions): Array<{
|
|
103
|
+
date: Date;
|
|
104
|
+
text: string;
|
|
105
|
+
confidence: number;
|
|
106
|
+
}>;
|
|
107
|
+
//# sourceMappingURL=naturalLanguage.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"naturalLanguage.d.ts","sourceRoot":"","sources":["../../src/naturalLanguage.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAKH;;GAEG;AACH,MAAM,WAAW,mBAAmB;IAClC,4DAA4D;IAC5D,aAAa,CAAC,EAAE,IAAI,CAAC;IACrB,sDAAsD;IACtD,WAAW,CAAC,EAAE;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,MAAM,EAAE,MAAM,CAAC;QAAC,MAAM,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC;IAChE,2CAA2C;IAC3C,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,oCAAoC;IACpC,MAAM,CAAC,EAAE,OAAO,CAAC;CAClB;AAED;;GAEG;AACH,MAAM,WAAW,aAAa;IAC5B,sBAAsB;IACtB,IAAI,EAAE,IAAI,CAAC;IACX,yCAAyC;IACzC,IAAI,EAAE,MAAM,CAAC;IACb,6CAA6C;IAC7C,KAAK,EAAE,MAAM,CAAC;IACd,mCAAmC;IACnC,IAAI,EAAE,UAAU,GAAG,UAAU,GAAG,OAAO,GAAG,MAAM,CAAC;IACjD,6BAA6B;IAC7B,UAAU,EAAE,MAAM,CAAC;CACpB;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,GAAE,mBAAwB,GAAG,IAAI,GAAG,IAAI,CA8C9F;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,MAAM,EAAE,aAAa,GAAE,IAAiB,GAAG,IAAI,GAAG,IAAI,CA6DhG;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,oBAAoB,CAClC,IAAI,EAAE,MAAM,EACZ,OAAO,GAAE,mBAAwB,GAChC,aAAa,EAAE,CAkDjB;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,sBAAsB,CACpC,OAAO,EAAE,MAAM,EACf,OAAO,GAAE,mBAAwB,GAChC,KAAK,CAAC;IAAE,IAAI,EAAE,IAAI,CAAC;IAAC,IAAI,EAAE,MAAM,CAAC;IAAC,UAAU,EAAE,MAAM,CAAA;CAAE,CAAC,CAkDzD"}
|