ts-time-utils 1.1.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.
Files changed (74) hide show
  1. package/README.md +458 -12
  2. package/dist/calculate.d.ts +7 -2
  3. package/dist/calculate.d.ts.map +1 -1
  4. package/dist/calculate.js +13 -3
  5. package/dist/calendar.d.ts +103 -0
  6. package/dist/calendar.d.ts.map +1 -1
  7. package/dist/calendar.js +224 -0
  8. package/dist/compare.d.ts +217 -0
  9. package/dist/compare.d.ts.map +1 -0
  10. package/dist/compare.js +417 -0
  11. package/dist/cron.d.ts +82 -0
  12. package/dist/cron.d.ts.map +1 -0
  13. package/dist/cron.js +294 -0
  14. package/dist/esm/calculate.d.ts +7 -2
  15. package/dist/esm/calculate.d.ts.map +1 -1
  16. package/dist/esm/calculate.js +13 -3
  17. package/dist/esm/calendar.d.ts +103 -0
  18. package/dist/esm/calendar.d.ts.map +1 -1
  19. package/dist/esm/calendar.js +224 -0
  20. package/dist/esm/compare.d.ts +217 -0
  21. package/dist/esm/compare.d.ts.map +1 -0
  22. package/dist/esm/compare.js +417 -0
  23. package/dist/esm/cron.d.ts +82 -0
  24. package/dist/esm/cron.d.ts.map +1 -0
  25. package/dist/esm/cron.js +294 -0
  26. package/dist/esm/fiscal.d.ts +195 -0
  27. package/dist/esm/fiscal.d.ts.map +1 -0
  28. package/dist/esm/fiscal.js +295 -0
  29. package/dist/esm/format.d.ts +65 -0
  30. package/dist/esm/format.d.ts.map +1 -1
  31. package/dist/esm/format.js +202 -0
  32. package/dist/esm/index.d.ts +13 -6
  33. package/dist/esm/index.d.ts.map +1 -1
  34. package/dist/esm/index.js +14 -6
  35. package/dist/esm/iterate.d.ts +212 -0
  36. package/dist/esm/iterate.d.ts.map +1 -0
  37. package/dist/esm/iterate.js +409 -0
  38. package/dist/esm/parse.d.ts +45 -0
  39. package/dist/esm/parse.d.ts.map +1 -1
  40. package/dist/esm/parse.js +207 -0
  41. package/dist/esm/timezone.d.ts +52 -0
  42. package/dist/esm/timezone.d.ts.map +1 -1
  43. package/dist/esm/timezone.js +171 -0
  44. package/dist/esm/validate.d.ts +51 -0
  45. package/dist/esm/validate.d.ts.map +1 -1
  46. package/dist/esm/validate.js +92 -0
  47. package/dist/esm/workingHours.d.ts +70 -0
  48. package/dist/esm/workingHours.d.ts.map +1 -1
  49. package/dist/esm/workingHours.js +161 -0
  50. package/dist/fiscal.d.ts +195 -0
  51. package/dist/fiscal.d.ts.map +1 -0
  52. package/dist/fiscal.js +295 -0
  53. package/dist/format.d.ts +65 -0
  54. package/dist/format.d.ts.map +1 -1
  55. package/dist/format.js +202 -0
  56. package/dist/index.d.ts +13 -6
  57. package/dist/index.d.ts.map +1 -1
  58. package/dist/index.js +14 -6
  59. package/dist/iterate.d.ts +212 -0
  60. package/dist/iterate.d.ts.map +1 -0
  61. package/dist/iterate.js +409 -0
  62. package/dist/parse.d.ts +45 -0
  63. package/dist/parse.d.ts.map +1 -1
  64. package/dist/parse.js +207 -0
  65. package/dist/timezone.d.ts +52 -0
  66. package/dist/timezone.d.ts.map +1 -1
  67. package/dist/timezone.js +171 -0
  68. package/dist/validate.d.ts +51 -0
  69. package/dist/validate.d.ts.map +1 -1
  70. package/dist/validate.js +92 -0
  71. package/dist/workingHours.d.ts +70 -0
  72. package/dist/workingHours.d.ts.map +1 -1
  73. package/dist/workingHours.js +161 -0
  74. package/package.json +30 -11
@@ -79,4 +79,107 @@ export declare function getFirstDayOfYear(year: number): Date;
79
79
  * @param year - year
80
80
  */
81
81
  export declare function getLastDayOfYear(year: number): Date;
82
+ /**
83
+ * Get the nth occurrence of a day in a month (e.g., 2nd Monday)
84
+ * @param year - year
85
+ * @param month - month (0-11)
86
+ * @param dayOfWeek - day of week (0=Sunday, 6=Saturday)
87
+ * @param n - occurrence (1-5, or -1 for last)
88
+ */
89
+ export declare function getNthDayOfMonth(year: number, month: number, dayOfWeek: number, n: number): Date | null;
90
+ /**
91
+ * US Holiday type
92
+ */
93
+ export interface USHoliday {
94
+ name: string;
95
+ date: Date;
96
+ type: 'federal' | 'observance';
97
+ }
98
+ /**
99
+ * Get New Year's Day
100
+ * @param year - year
101
+ */
102
+ export declare function getNewYearsDay(year: number): Date;
103
+ /**
104
+ * Get Martin Luther King Jr. Day (3rd Monday of January)
105
+ * @param year - year
106
+ */
107
+ export declare function getMLKDay(year: number): Date | null;
108
+ /**
109
+ * Get Presidents' Day (3rd Monday of February)
110
+ * @param year - year
111
+ */
112
+ export declare function getPresidentsDay(year: number): Date | null;
113
+ /**
114
+ * Get Memorial Day (last Monday of May)
115
+ * @param year - year
116
+ */
117
+ export declare function getMemorialDay(year: number): Date | null;
118
+ /**
119
+ * Get Independence Day (July 4th)
120
+ * @param year - year
121
+ */
122
+ export declare function getIndependenceDay(year: number): Date;
123
+ /**
124
+ * Get Labor Day (1st Monday of September)
125
+ * @param year - year
126
+ */
127
+ export declare function getLaborDay(year: number): Date | null;
128
+ /**
129
+ * Get Columbus Day (2nd Monday of October)
130
+ * @param year - year
131
+ */
132
+ export declare function getColumbusDay(year: number): Date | null;
133
+ /**
134
+ * Get Veterans Day (November 11th)
135
+ * @param year - year
136
+ */
137
+ export declare function getVeteransDay(year: number): Date;
138
+ /**
139
+ * Get Thanksgiving Day (4th Thursday of November)
140
+ * @param year - year
141
+ */
142
+ export declare function getThanksgivingDay(year: number): Date | null;
143
+ /**
144
+ * Get Christmas Day (December 25th)
145
+ * @param year - year
146
+ */
147
+ export declare function getChristmasDay(year: number): Date;
148
+ /**
149
+ * Get Good Friday (Friday before Easter)
150
+ * @param year - year
151
+ */
152
+ export declare function getGoodFriday(year: number): Date;
153
+ /**
154
+ * Get all US federal holidays for a year
155
+ * @param year - year
156
+ */
157
+ export declare function getUSHolidays(year: number): USHoliday[];
158
+ /**
159
+ * Check if a date is a US federal holiday
160
+ * @param date - date to check
161
+ */
162
+ export declare function isUSHoliday(date: Date): boolean;
163
+ /**
164
+ * Get the name of a US holiday for a given date
165
+ * @param date - date to check
166
+ * @returns holiday name or null if not a holiday
167
+ */
168
+ export declare function getUSHolidayName(date: Date): string | null;
169
+ /**
170
+ * Get the start of the week for a date (Monday)
171
+ * @param date - any date
172
+ */
173
+ export declare function getStartOfWeek(date: Date): Date;
174
+ /**
175
+ * Get the end of the week for a date (Sunday)
176
+ * @param date - any date
177
+ */
178
+ export declare function getEndOfWeek(date: Date): Date;
179
+ /**
180
+ * Get all weeks in a month as arrays of dates
181
+ * @param year - year
182
+ * @param month - month (0-11)
183
+ */
184
+ export declare function getWeeksInMonth(year: number, month: number): Date[][];
82
185
  //# sourceMappingURL=calendar.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"calendar.d.ts","sourceRoot":"","sources":["../src/calendar.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH;;;GAGG;AACH,wBAAgB,aAAa,CAAC,IAAI,EAAE,IAAI,GAAG,MAAM,CAMhD;AAED;;;GAGG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,IAAI,GAAG,MAAM,CAKjD;AAED;;;GAGG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAAE,IAAI,GAAG,MAAM,CAE7C;AAED;;;GAGG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,IAAI,GAAG,MAAM,CAI/C;AAED;;;GAGG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAYnD;AAED;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,CAElE;AAED;;;GAGG;AACH,wBAAgB,aAAa,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAElD;AAUD;;;GAGG;AACH,wBAAgB,SAAS,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAkB5C;AAED;;;GAGG;AACH,wBAAgB,eAAe,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,EAAE,CAEpD;AAED;;;;GAIG;AACH,wBAAg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I,GAAG,IAAI,CAExD;AAED;;;GAGG;AACH,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAErD;AAED;;;GAGG;AACH,wBAAgB,WAAW,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,GAAG,IAAI,CAErD;AAED;;;GAGG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,GAAG,IAAI,CAExD;AAED;;;GAGG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAEjD;AAED;;;GAGG;AACH,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,GAAG,IAAI,CAE5D;AAED;;;GAGG;AACH,wBAAgB,eAAe,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAElD;AAED;;;GAGG;AACH,wBAAgB,aAAa,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAKhD;AAED;;;GAGG;AACH,wBAAgB,aAAa,CAAC,IAAI,EAAE,MAAM,GAAG,SAAS,EAAE,CA8BvD;AAED;;;GAGG;AACH,wBAAgB,WAAW,CAAC,IAAI,EAAE,IAAI,GAAG,OAAO,CAO/C;AAED;;;;GAIG;AACH,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,IAAI,GAAG,MAAM,GAAG,IAAI,CAQ1D;AAED;;;GAGG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,IAAI,GAAG,IAAI,CAO/C;AAED;;;GAGG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,IAAI,GAAG,IAAI,CAM7C;AAED;;;;GAIG;AACH,wBAAgB,eAAe,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,IAAI,EAAE,EAAE,CAkCrE"}
package/dist/calendar.js CHANGED
@@ -152,3 +152,227 @@ export function getFirstDayOfYear(year) {
152
152
  export function getLastDayOfYear(year) {
153
153
  return new Date(year, 11, 31);
154
154
  }
155
+ /**
156
+ * Get the nth occurrence of a day in a month (e.g., 2nd Monday)
157
+ * @param year - year
158
+ * @param month - month (0-11)
159
+ * @param dayOfWeek - day of week (0=Sunday, 6=Saturday)
160
+ * @param n - occurrence (1-5, or -1 for last)
161
+ */
162
+ export function getNthDayOfMonth(year, month, dayOfWeek, n) {
163
+ if (n === 0 || n < -1 || n > 5)
164
+ return null;
165
+ const firstDay = new Date(year, month, 1);
166
+ const lastDay = new Date(year, month + 1, 0);
167
+ if (n === -1) {
168
+ // Last occurrence
169
+ let date = lastDay.getDate();
170
+ while (date > 0) {
171
+ const d = new Date(year, month, date);
172
+ if (d.getDay() === dayOfWeek)
173
+ return d;
174
+ date--;
175
+ }
176
+ return null;
177
+ }
178
+ // Find nth occurrence
179
+ let count = 0;
180
+ for (let date = 1; date <= lastDay.getDate(); date++) {
181
+ const d = new Date(year, month, date);
182
+ if (d.getDay() === dayOfWeek) {
183
+ count++;
184
+ if (count === n)
185
+ return d;
186
+ }
187
+ }
188
+ return null;
189
+ }
190
+ /**
191
+ * Get New Year's Day
192
+ * @param year - year
193
+ */
194
+ export function getNewYearsDay(year) {
195
+ return new Date(year, 0, 1);
196
+ }
197
+ /**
198
+ * Get Martin Luther King Jr. Day (3rd Monday of January)
199
+ * @param year - year
200
+ */
201
+ export function getMLKDay(year) {
202
+ return getNthDayOfMonth(year, 0, 1, 3);
203
+ }
204
+ /**
205
+ * Get Presidents' Day (3rd Monday of February)
206
+ * @param year - year
207
+ */
208
+ export function getPresidentsDay(year) {
209
+ return getNthDayOfMonth(year, 1, 1, 3);
210
+ }
211
+ /**
212
+ * Get Memorial Day (last Monday of May)
213
+ * @param year - year
214
+ */
215
+ export function getMemorialDay(year) {
216
+ return getNthDayOfMonth(year, 4, 1, -1);
217
+ }
218
+ /**
219
+ * Get Independence Day (July 4th)
220
+ * @param year - year
221
+ */
222
+ export function getIndependenceDay(year) {
223
+ return new Date(year, 6, 4);
224
+ }
225
+ /**
226
+ * Get Labor Day (1st Monday of September)
227
+ * @param year - year
228
+ */
229
+ export function getLaborDay(year) {
230
+ return getNthDayOfMonth(year, 8, 1, 1);
231
+ }
232
+ /**
233
+ * Get Columbus Day (2nd Monday of October)
234
+ * @param year - year
235
+ */
236
+ export function getColumbusDay(year) {
237
+ return getNthDayOfMonth(year, 9, 1, 2);
238
+ }
239
+ /**
240
+ * Get Veterans Day (November 11th)
241
+ * @param year - year
242
+ */
243
+ export function getVeteransDay(year) {
244
+ return new Date(year, 10, 11);
245
+ }
246
+ /**
247
+ * Get Thanksgiving Day (4th Thursday of November)
248
+ * @param year - year
249
+ */
250
+ export function getThanksgivingDay(year) {
251
+ return getNthDayOfMonth(year, 10, 4, 4);
252
+ }
253
+ /**
254
+ * Get Christmas Day (December 25th)
255
+ * @param year - year
256
+ */
257
+ export function getChristmasDay(year) {
258
+ return new Date(year, 11, 25);
259
+ }
260
+ /**
261
+ * Get Good Friday (Friday before Easter)
262
+ * @param year - year
263
+ */
264
+ export function getGoodFriday(year) {
265
+ const easter = getEaster(year);
266
+ const goodFriday = new Date(easter);
267
+ goodFriday.setDate(easter.getDate() - 2);
268
+ return goodFriday;
269
+ }
270
+ /**
271
+ * Get all US federal holidays for a year
272
+ * @param year - year
273
+ */
274
+ export function getUSHolidays(year) {
275
+ const holidays = [];
276
+ holidays.push({ name: "New Year's Day", date: getNewYearsDay(year), type: 'federal' });
277
+ const mlk = getMLKDay(year);
278
+ if (mlk)
279
+ holidays.push({ name: "Martin Luther King Jr. Day", date: mlk, type: 'federal' });
280
+ const presidents = getPresidentsDay(year);
281
+ if (presidents)
282
+ holidays.push({ name: "Presidents' Day", date: presidents, type: 'federal' });
283
+ const memorial = getMemorialDay(year);
284
+ if (memorial)
285
+ holidays.push({ name: "Memorial Day", date: memorial, type: 'federal' });
286
+ holidays.push({ name: "Independence Day", date: getIndependenceDay(year), type: 'federal' });
287
+ const labor = getLaborDay(year);
288
+ if (labor)
289
+ holidays.push({ name: "Labor Day", date: labor, type: 'federal' });
290
+ const columbus = getColumbusDay(year);
291
+ if (columbus)
292
+ holidays.push({ name: "Columbus Day", date: columbus, type: 'federal' });
293
+ holidays.push({ name: "Veterans Day", date: getVeteransDay(year), type: 'federal' });
294
+ const thanksgiving = getThanksgivingDay(year);
295
+ if (thanksgiving)
296
+ holidays.push({ name: "Thanksgiving Day", date: thanksgiving, type: 'federal' });
297
+ holidays.push({ name: "Christmas Day", date: getChristmasDay(year), type: 'federal' });
298
+ return holidays.sort((a, b) => a.date.getTime() - b.date.getTime());
299
+ }
300
+ /**
301
+ * Check if a date is a US federal holiday
302
+ * @param date - date to check
303
+ */
304
+ export function isUSHoliday(date) {
305
+ const holidays = getUSHolidays(date.getFullYear());
306
+ return holidays.some(h => h.date.getFullYear() === date.getFullYear() &&
307
+ h.date.getMonth() === date.getMonth() &&
308
+ h.date.getDate() === date.getDate());
309
+ }
310
+ /**
311
+ * Get the name of a US holiday for a given date
312
+ * @param date - date to check
313
+ * @returns holiday name or null if not a holiday
314
+ */
315
+ export function getUSHolidayName(date) {
316
+ const holidays = getUSHolidays(date.getFullYear());
317
+ const holiday = holidays.find(h => h.date.getFullYear() === date.getFullYear() &&
318
+ h.date.getMonth() === date.getMonth() &&
319
+ h.date.getDate() === date.getDate());
320
+ return holiday ? holiday.name : null;
321
+ }
322
+ /**
323
+ * Get the start of the week for a date (Monday)
324
+ * @param date - any date
325
+ */
326
+ export function getStartOfWeek(date) {
327
+ const d = new Date(date);
328
+ const day = d.getDay();
329
+ const diff = d.getDate() - day + (day === 0 ? -6 : 1); // Adjust for Monday start
330
+ d.setDate(diff);
331
+ d.setHours(0, 0, 0, 0);
332
+ return d;
333
+ }
334
+ /**
335
+ * Get the end of the week for a date (Sunday)
336
+ * @param date - any date
337
+ */
338
+ export function getEndOfWeek(date) {
339
+ const start = getStartOfWeek(date);
340
+ const end = new Date(start);
341
+ end.setDate(start.getDate() + 6);
342
+ end.setHours(23, 59, 59, 999);
343
+ return end;
344
+ }
345
+ /**
346
+ * Get all weeks in a month as arrays of dates
347
+ * @param year - year
348
+ * @param month - month (0-11)
349
+ */
350
+ export function getWeeksInMonth(year, month) {
351
+ const weeks = [];
352
+ const firstDay = new Date(year, month, 1);
353
+ const lastDay = new Date(year, month + 1, 0);
354
+ let currentWeek = [];
355
+ const startDayOfWeek = firstDay.getDay() || 7; // Convert Sunday from 0 to 7 for ISO
356
+ // Add days from previous month to fill the first week
357
+ for (let i = 1; i < startDayOfWeek; i++) {
358
+ const prevDate = new Date(year, month, 1 - (startDayOfWeek - i));
359
+ currentWeek.push(prevDate);
360
+ }
361
+ // Add days of the current month
362
+ for (let day = 1; day <= lastDay.getDate(); day++) {
363
+ currentWeek.push(new Date(year, month, day));
364
+ if (currentWeek.length === 7) {
365
+ weeks.push(currentWeek);
366
+ currentWeek = [];
367
+ }
368
+ }
369
+ // Add days from next month to fill the last week
370
+ if (currentWeek.length > 0) {
371
+ let nextDay = 1;
372
+ while (currentWeek.length < 7) {
373
+ currentWeek.push(new Date(year, month + 1, nextDay++));
374
+ }
375
+ weeks.push(currentWeek);
376
+ }
377
+ return weeks;
378
+ }
@@ -0,0 +1,217 @@
1
+ /**
2
+ * @fileoverview Date comparison, sorting, and array manipulation utilities
3
+ */
4
+ /**
5
+ * Compare two dates for sorting
6
+ * @param a - First date
7
+ * @param b - Second date
8
+ * @returns Negative if a < b, positive if a > b, 0 if equal
9
+ * @example
10
+ * [date3, date1, date2].sort(compareDates) // [date1, date2, date3]
11
+ */
12
+ export declare function compareDates(a: Date, b: Date): number;
13
+ /**
14
+ * Compare two dates in reverse order for sorting
15
+ * @param a - First date
16
+ * @param b - Second date
17
+ * @returns Positive if a < b, negative if a > b, 0 if equal
18
+ * @example
19
+ * [date1, date3, date2].sort(compareDatesDesc) // [date3, date2, date1]
20
+ */
21
+ export declare function compareDatesDesc(a: Date, b: Date): number;
22
+ /**
23
+ * Sort an array of dates
24
+ * @param dates - Array of dates to sort
25
+ * @param direction - Sort direction: 'asc' (oldest first) or 'desc' (newest first)
26
+ * @returns New sorted array (does not mutate original)
27
+ * @example
28
+ * sortDates([date3, date1, date2]) // [date1, date2, date3]
29
+ * sortDates([date1, date2, date3], 'desc') // [date3, date2, date1]
30
+ */
31
+ export declare function sortDates(dates: Date[], direction?: 'asc' | 'desc'): Date[];
32
+ /**
33
+ * Find the minimum (earliest) date in an array
34
+ * @param dates - Array of dates
35
+ * @returns The earliest date, or undefined if array is empty
36
+ * @example
37
+ * minDate([date2, date1, date3]) // date1
38
+ */
39
+ export declare function minDate(dates: Date[]): Date | undefined;
40
+ /**
41
+ * Find the maximum (latest) date in an array
42
+ * @param dates - Array of dates
43
+ * @returns The latest date, or undefined if array is empty
44
+ * @example
45
+ * maxDate([date1, date3, date2]) // date3
46
+ */
47
+ export declare function maxDate(dates: Date[]): Date | undefined;
48
+ /**
49
+ * Find the date range (min and max) in an array
50
+ * @param dates - Array of dates
51
+ * @returns Object with min and max dates, or undefined if array is empty
52
+ * @example
53
+ * dateRange([date2, date1, date3]) // { min: date1, max: date3 }
54
+ */
55
+ export declare function dateExtent(dates: Date[]): {
56
+ min: Date;
57
+ max: Date;
58
+ } | undefined;
59
+ /**
60
+ * Remove duplicate dates from an array
61
+ * @param dates - Array of dates
62
+ * @param precision - Precision for comparison: 'ms' (exact), 'second', 'minute', 'hour', 'day'
63
+ * @returns New array with duplicates removed (preserves first occurrence)
64
+ * @example
65
+ * uniqueDates([date1, date1Copy, date2]) // [date1, date2]
66
+ */
67
+ export declare function uniqueDates(dates: Date[], precision?: 'ms' | 'second' | 'minute' | 'hour' | 'day'): Date[];
68
+ /**
69
+ * Find the closest date to a target from an array of candidates
70
+ * @param target - The target date
71
+ * @param candidates - Array of candidate dates
72
+ * @returns The closest date, or undefined if candidates is empty
73
+ * @example
74
+ * closestDate(targetDate, [date1, date2, date3]) // closest to target
75
+ */
76
+ export declare function closestDate(target: Date, candidates: Date[]): Date | undefined;
77
+ /**
78
+ * Find the closest future date to a target
79
+ * @param target - The target date
80
+ * @param candidates - Array of candidate dates
81
+ * @returns The closest future date, or undefined if none found
82
+ */
83
+ export declare function closestFutureDate(target: Date, candidates: Date[]): Date | undefined;
84
+ /**
85
+ * Find the closest past date to a target
86
+ * @param target - The target date
87
+ * @param candidates - Array of candidate dates
88
+ * @returns The closest past date, or undefined if none found
89
+ */
90
+ export declare function closestPastDate(target: Date, candidates: Date[]): Date | undefined;
91
+ /**
92
+ * Clamp a date to be within a range
93
+ * @param date - The date to clamp
94
+ * @param min - Minimum allowed date
95
+ * @param max - Maximum allowed date
96
+ * @returns The clamped date
97
+ * @example
98
+ * clampDate(earlyDate, minDate, maxDate) // returns minDate
99
+ * clampDate(lateDate, minDate, maxDate) // returns maxDate
100
+ * clampDate(middleDate, minDate, maxDate) // returns middleDate
101
+ */
102
+ export declare function clampDate(date: Date, min: Date, max: Date): Date;
103
+ /**
104
+ * Check if a date is within a range (inclusive)
105
+ * @param date - The date to check
106
+ * @param min - Start of range
107
+ * @param max - End of range
108
+ * @returns True if date is within range
109
+ */
110
+ export declare function isDateInRange(date: Date, min: Date, max: Date): boolean;
111
+ /**
112
+ * Filter dates to only those within a range
113
+ * @param dates - Array of dates
114
+ * @param min - Start of range
115
+ * @param max - End of range
116
+ * @returns New array with only dates in range
117
+ */
118
+ export declare function filterDatesInRange(dates: Date[], min: Date, max: Date): Date[];
119
+ /**
120
+ * Group dates by a key function
121
+ * @param dates - Array of dates
122
+ * @param keyFn - Function to generate group key from date
123
+ * @returns Map of key to array of dates
124
+ * @example
125
+ * groupDates(dates, d => d.getFullYear()) // Map { 2023 => [...], 2024 => [...] }
126
+ * groupDates(dates, d => d.toISOString().slice(0, 7)) // Group by month
127
+ */
128
+ export declare function groupDates<K>(dates: Date[], keyFn: (date: Date) => K): Map<K, Date[]>;
129
+ /**
130
+ * Group dates by year
131
+ * @param dates - Array of dates
132
+ * @returns Map of year to array of dates
133
+ */
134
+ export declare function groupDatesByYear(dates: Date[]): Map<number, Date[]>;
135
+ /**
136
+ * Group dates by month (YYYY-MM format)
137
+ * @param dates - Array of dates
138
+ * @returns Map of month key to array of dates
139
+ */
140
+ export declare function groupDatesByMonth(dates: Date[]): Map<string, Date[]>;
141
+ /**
142
+ * Group dates by day (YYYY-MM-DD format)
143
+ * @param dates - Array of dates
144
+ * @returns Map of day key to array of dates
145
+ */
146
+ export declare function groupDatesByDay(dates: Date[]): Map<string, Date[]>;
147
+ /**
148
+ * Group dates by day of week (0-6, Sunday-Saturday)
149
+ * @param dates - Array of dates
150
+ * @returns Map of day of week to array of dates
151
+ */
152
+ export declare function groupDatesByDayOfWeek(dates: Date[]): Map<number, Date[]>;
153
+ /**
154
+ * Calculate the median date from an array
155
+ * @param dates - Array of dates
156
+ * @returns The median date, or undefined if array is empty
157
+ */
158
+ export declare function medianDate(dates: Date[]): Date | undefined;
159
+ /**
160
+ * Calculate the average/mean date from an array
161
+ * @param dates - Array of dates
162
+ * @returns The average date, or undefined if array is empty
163
+ */
164
+ export declare function averageDate(dates: Date[]): Date | undefined;
165
+ /**
166
+ * Round a date to the nearest unit
167
+ * @param date - The date to round
168
+ * @param unit - Unit to round to
169
+ * @returns New rounded date
170
+ * @example
171
+ * roundDate(new Date('2024-03-15T14:37:00'), 'hour') // 2024-03-15T15:00:00
172
+ * roundDate(new Date('2024-03-15T14:22:00'), 'hour') // 2024-03-15T14:00:00
173
+ */
174
+ export declare function roundDate(date: Date, unit: 'minute' | 'hour' | 'day'): Date;
175
+ /**
176
+ * Snap a date to a grid interval
177
+ * @param date - The date to snap
178
+ * @param intervalMinutes - Interval in minutes (e.g., 15 for quarter-hour)
179
+ * @param mode - Snap mode: 'floor' (down), 'ceil' (up), or 'round' (nearest)
180
+ * @returns New snapped date
181
+ * @example
182
+ * snapDate(new Date('2024-03-15T14:37:00'), 15) // 2024-03-15T14:30:00
183
+ * snapDate(new Date('2024-03-15T14:37:00'), 15, 'ceil') // 2024-03-15T14:45:00
184
+ */
185
+ export declare function snapDate(date: Date, intervalMinutes: number, mode?: 'floor' | 'ceil' | 'round'): Date;
186
+ /**
187
+ * Check if dates are in chronological order
188
+ * @param dates - Array of dates
189
+ * @param strict - If true, requires strictly increasing (no duplicates)
190
+ * @returns True if dates are in order
191
+ */
192
+ export declare function isChronological(dates: Date[], strict?: boolean): boolean;
193
+ /**
194
+ * Get the span (duration) between min and max dates
195
+ * @param dates - Array of dates
196
+ * @returns Duration in milliseconds, or 0 if less than 2 dates
197
+ */
198
+ export declare function dateSpan(dates: Date[]): number;
199
+ /**
200
+ * Partition dates into buckets based on a predicate
201
+ * @param dates - Array of dates
202
+ * @param predicate - Function that returns true for dates in first partition
203
+ * @returns Tuple of [matching, non-matching] date arrays
204
+ */
205
+ export declare function partitionDates(dates: Date[], predicate: (date: Date) => boolean): [Date[], Date[]];
206
+ /**
207
+ * Get the nth date from an array (supports negative indices)
208
+ * @param dates - Array of dates (will be sorted)
209
+ * @param n - Index (0-based, negative counts from end)
210
+ * @returns The nth date, or undefined if out of bounds
211
+ * @example
212
+ * nthDate(dates, 0) // earliest
213
+ * nthDate(dates, -1) // latest
214
+ * nthDate(dates, 2) // third earliest
215
+ */
216
+ export declare function nthDate(dates: Date[], n: number): Date | undefined;
217
+ //# sourceMappingURL=compare.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"compare.d.ts","sourceRoot":"","sources":["../src/compare.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH;;;;;;;GAOG;AACH,wBAAgB,YAAY,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,EAAE,IAAI,GAAG,MAAM,CAErD;AAED;;;;;;;GAOG;AACH,wBAAgB,gBAAgB,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,EAAE,IAAI,GAAG,MAAM,CAEzD;AAED;;;;;;;;GAQG;AACH,wBAAgB,SAAS,CACvB,KAAK,EAAE,IAAI,EAAE,EACb,SAAS,GAAE,KAAK,GAAG,MAAc,GAChC,IAAI,EAAE,CAIR;AAED;;;;;;GAMG;AACH,wBAAgB,OAAO,CAAC,KAAK,EAAE,IAAI,EA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