ts-time-utils 1.1.0 → 3.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 (92) hide show
  1. package/README.md +567 -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/chain.d.ts +269 -0
  9. package/dist/chain.d.ts.map +1 -0
  10. package/dist/chain.js +422 -0
  11. package/dist/compare.d.ts +217 -0
  12. package/dist/compare.d.ts.map +1 -0
  13. package/dist/compare.js +417 -0
  14. package/dist/cron.d.ts +82 -0
  15. package/dist/cron.d.ts.map +1 -0
  16. package/dist/cron.js +294 -0
  17. package/dist/esm/calculate.d.ts +7 -2
  18. package/dist/esm/calculate.d.ts.map +1 -1
  19. package/dist/esm/calculate.js +13 -3
  20. package/dist/esm/calendar.d.ts +103 -0
  21. package/dist/esm/calendar.d.ts.map +1 -1
  22. package/dist/esm/calendar.js +224 -0
  23. package/dist/esm/chain.d.ts +269 -0
  24. package/dist/esm/chain.d.ts.map +1 -0
  25. package/dist/esm/chain.js +422 -0
  26. package/dist/esm/compare.d.ts +217 -0
  27. package/dist/esm/compare.d.ts.map +1 -0
  28. package/dist/esm/compare.js +417 -0
  29. package/dist/esm/cron.d.ts +82 -0
  30. package/dist/esm/cron.d.ts.map +1 -0
  31. package/dist/esm/cron.js +294 -0
  32. package/dist/esm/fiscal.d.ts +195 -0
  33. package/dist/esm/fiscal.d.ts.map +1 -0
  34. package/dist/esm/fiscal.js +295 -0
  35. package/dist/esm/format.d.ts +65 -0
  36. package/dist/esm/format.d.ts.map +1 -1
  37. package/dist/esm/format.js +202 -0
  38. package/dist/esm/holidays.d.ts +62 -0
  39. package/dist/esm/holidays.d.ts.map +1 -0
  40. package/dist/esm/holidays.js +793 -0
  41. package/dist/esm/index.d.ts +18 -6
  42. package/dist/esm/index.d.ts.map +1 -1
  43. package/dist/esm/index.js +20 -6
  44. package/dist/esm/iterate.d.ts +212 -0
  45. package/dist/esm/iterate.d.ts.map +1 -0
  46. package/dist/esm/iterate.js +409 -0
  47. package/dist/esm/parse.d.ts +45 -0
  48. package/dist/esm/parse.d.ts.map +1 -1
  49. package/dist/esm/parse.js +207 -0
  50. package/dist/esm/plugins.d.ts +129 -0
  51. package/dist/esm/plugins.d.ts.map +1 -0
  52. package/dist/esm/plugins.js +173 -0
  53. package/dist/esm/timezone.d.ts +52 -0
  54. package/dist/esm/timezone.d.ts.map +1 -1
  55. package/dist/esm/timezone.js +171 -0
  56. package/dist/esm/validate.d.ts +51 -0
  57. package/dist/esm/validate.d.ts.map +1 -1
  58. package/dist/esm/validate.js +92 -0
  59. package/dist/esm/workingHours.d.ts +70 -0
  60. package/dist/esm/workingHours.d.ts.map +1 -1
  61. package/dist/esm/workingHours.js +161 -0
  62. package/dist/fiscal.d.ts +195 -0
  63. package/dist/fiscal.d.ts.map +1 -0
  64. package/dist/fiscal.js +295 -0
  65. package/dist/format.d.ts +65 -0
  66. package/dist/format.d.ts.map +1 -1
  67. package/dist/format.js +202 -0
  68. package/dist/holidays.d.ts +62 -0
  69. package/dist/holidays.d.ts.map +1 -0
  70. package/dist/holidays.js +793 -0
  71. package/dist/index.d.ts +18 -6
  72. package/dist/index.d.ts.map +1 -1
  73. package/dist/index.js +20 -6
  74. package/dist/iterate.d.ts +212 -0
  75. package/dist/iterate.d.ts.map +1 -0
  76. package/dist/iterate.js +409 -0
  77. package/dist/parse.d.ts +45 -0
  78. package/dist/parse.d.ts.map +1 -1
  79. package/dist/parse.js +207 -0
  80. package/dist/plugins.d.ts +129 -0
  81. package/dist/plugins.d.ts.map +1 -0
  82. package/dist/plugins.js +173 -0
  83. package/dist/timezone.d.ts +52 -0
  84. package/dist/timezone.d.ts.map +1 -1
  85. package/dist/timezone.js +171 -0
  86. package/dist/validate.d.ts +51 -0
  87. package/dist/validate.d.ts.map +1 -1
  88. package/dist/validate.js +92 -0
  89. package/dist/workingHours.d.ts +70 -0
  90. package/dist/workingHours.d.ts.map +1 -1
  91. package/dist/workingHours.js +161 -0
  92. package/package.json +40 -1
@@ -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":"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"}
@@ -0,0 +1,417 @@
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 function compareDates(a, b) {
13
+ return a.getTime() - b.getTime();
14
+ }
15
+ /**
16
+ * Compare two dates in reverse order for sorting
17
+ * @param a - First date
18
+ * @param b - Second date
19
+ * @returns Positive if a < b, negative if a > b, 0 if equal
20
+ * @example
21
+ * [date1, date3, date2].sort(compareDatesDesc) // [date3, date2, date1]
22
+ */
23
+ export function compareDatesDesc(a, b) {
24
+ return b.getTime() - a.getTime();
25
+ }
26
+ /**
27
+ * Sort an array of dates
28
+ * @param dates - Array of dates to sort
29
+ * @param direction - Sort direction: 'asc' (oldest first) or 'desc' (newest first)
30
+ * @returns New sorted array (does not mutate original)
31
+ * @example
32
+ * sortDates([date3, date1, date2]) // [date1, date2, date3]
33
+ * sortDates([date1, date2, date3], 'desc') // [date3, date2, date1]
34
+ */
35
+ export function sortDates(dates, direction = 'asc') {
36
+ const sorted = [...dates];
37
+ sorted.sort(direction === 'asc' ? compareDates : compareDatesDesc);
38
+ return sorted;
39
+ }
40
+ /**
41
+ * Find the minimum (earliest) date in an array
42
+ * @param dates - Array of dates
43
+ * @returns The earliest date, or undefined if array is empty
44
+ * @example
45
+ * minDate([date2, date1, date3]) // date1
46
+ */
47
+ export function minDate(dates) {
48
+ if (dates.length === 0)
49
+ return undefined;
50
+ return dates.reduce((min, date) => (date < min ? date : min));
51
+ }
52
+ /**
53
+ * Find the maximum (latest) date in an array
54
+ * @param dates - Array of dates
55
+ * @returns The latest date, or undefined if array is empty
56
+ * @example
57
+ * maxDate([date1, date3, date2]) // date3
58
+ */
59
+ export function maxDate(dates) {
60
+ if (dates.length === 0)
61
+ return undefined;
62
+ return dates.reduce((max, date) => (date > max ? date : max));
63
+ }
64
+ /**
65
+ * Find the date range (min and max) in an array
66
+ * @param dates - Array of dates
67
+ * @returns Object with min and max dates, or undefined if array is empty
68
+ * @example
69
+ * dateRange([date2, date1, date3]) // { min: date1, max: date3 }
70
+ */
71
+ export function dateExtent(dates) {
72
+ if (dates.length === 0)
73
+ return undefined;
74
+ let min = dates[0];
75
+ let max = dates[0];
76
+ for (const date of dates) {
77
+ if (date < min)
78
+ min = date;
79
+ if (date > max)
80
+ max = date;
81
+ }
82
+ return { min, max };
83
+ }
84
+ /**
85
+ * Remove duplicate dates from an array
86
+ * @param dates - Array of dates
87
+ * @param precision - Precision for comparison: 'ms' (exact), 'second', 'minute', 'hour', 'day'
88
+ * @returns New array with duplicates removed (preserves first occurrence)
89
+ * @example
90
+ * uniqueDates([date1, date1Copy, date2]) // [date1, date2]
91
+ */
92
+ export function uniqueDates(dates, precision = 'ms') {
93
+ const seen = new Set();
94
+ const result = [];
95
+ for (const date of dates) {
96
+ const key = getDateKey(date, precision);
97
+ if (!seen.has(key)) {
98
+ seen.add(key);
99
+ result.push(date);
100
+ }
101
+ }
102
+ return result;
103
+ }
104
+ /**
105
+ * Get a numeric key for a date based on precision
106
+ */
107
+ function getDateKey(date, precision) {
108
+ switch (precision) {
109
+ case 'ms':
110
+ return date.getTime();
111
+ case 'second':
112
+ return Math.floor(date.getTime() / 1000);
113
+ case 'minute':
114
+ return Math.floor(date.getTime() / 60000);
115
+ case 'hour':
116
+ return Math.floor(date.getTime() / 3600000);
117
+ case 'day':
118
+ return Math.floor(date.getTime() / 86400000);
119
+ }
120
+ }
121
+ /**
122
+ * Find the closest date to a target from an array of candidates
123
+ * @param target - The target date
124
+ * @param candidates - Array of candidate dates
125
+ * @returns The closest date, or undefined if candidates is empty
126
+ * @example
127
+ * closestDate(targetDate, [date1, date2, date3]) // closest to target
128
+ */
129
+ export function closestDate(target, candidates) {
130
+ if (candidates.length === 0)
131
+ return undefined;
132
+ const targetTime = target.getTime();
133
+ let closest = candidates[0];
134
+ let minDiff = Math.abs(closest.getTime() - targetTime);
135
+ for (const candidate of candidates) {
136
+ const diff = Math.abs(candidate.getTime() - targetTime);
137
+ if (diff < minDiff) {
138
+ minDiff = diff;
139
+ closest = candidate;
140
+ }
141
+ }
142
+ return closest;
143
+ }
144
+ /**
145
+ * Find the closest future date to a target
146
+ * @param target - The target date
147
+ * @param candidates - Array of candidate dates
148
+ * @returns The closest future date, or undefined if none found
149
+ */
150
+ export function closestFutureDate(target, candidates) {
151
+ const targetTime = target.getTime();
152
+ const futureDates = candidates.filter(d => d.getTime() > targetTime);
153
+ return minDate(futureDates);
154
+ }
155
+ /**
156
+ * Find the closest past date to a target
157
+ * @param target - The target date
158
+ * @param candidates - Array of candidate dates
159
+ * @returns The closest past date, or undefined if none found
160
+ */
161
+ export function closestPastDate(target, candidates) {
162
+ const targetTime = target.getTime();
163
+ const pastDates = candidates.filter(d => d.getTime() < targetTime);
164
+ return maxDate(pastDates);
165
+ }
166
+ /**
167
+ * Clamp a date to be within a range
168
+ * @param date - The date to clamp
169
+ * @param min - Minimum allowed date
170
+ * @param max - Maximum allowed date
171
+ * @returns The clamped date
172
+ * @example
173
+ * clampDate(earlyDate, minDate, maxDate) // returns minDate
174
+ * clampDate(lateDate, minDate, maxDate) // returns maxDate
175
+ * clampDate(middleDate, minDate, maxDate) // returns middleDate
176
+ */
177
+ export function clampDate(date, min, max) {
178
+ if (date < min)
179
+ return new Date(min);
180
+ if (date > max)
181
+ return new Date(max);
182
+ return new Date(date);
183
+ }
184
+ /**
185
+ * Check if a date is within a range (inclusive)
186
+ * @param date - The date to check
187
+ * @param min - Start of range
188
+ * @param max - End of range
189
+ * @returns True if date is within range
190
+ */
191
+ export function isDateInRange(date, min, max) {
192
+ const time = date.getTime();
193
+ return time >= min.getTime() && time <= max.getTime();
194
+ }
195
+ /**
196
+ * Filter dates to only those within a range
197
+ * @param dates - Array of dates
198
+ * @param min - Start of range
199
+ * @param max - End of range
200
+ * @returns New array with only dates in range
201
+ */
202
+ export function filterDatesInRange(dates, min, max) {
203
+ return dates.filter(date => isDateInRange(date, min, max));
204
+ }
205
+ /**
206
+ * Group dates by a key function
207
+ * @param dates - Array of dates
208
+ * @param keyFn - Function to generate group key from date
209
+ * @returns Map of key to array of dates
210
+ * @example
211
+ * groupDates(dates, d => d.getFullYear()) // Map { 2023 => [...], 2024 => [...] }
212
+ * groupDates(dates, d => d.toISOString().slice(0, 7)) // Group by month
213
+ */
214
+ export function groupDates(dates, keyFn) {
215
+ const groups = new Map();
216
+ for (const date of dates) {
217
+ const key = keyFn(date);
218
+ const group = groups.get(key);
219
+ if (group) {
220
+ group.push(date);
221
+ }
222
+ else {
223
+ groups.set(key, [date]);
224
+ }
225
+ }
226
+ return groups;
227
+ }
228
+ /**
229
+ * Group dates by year
230
+ * @param dates - Array of dates
231
+ * @returns Map of year to array of dates
232
+ */
233
+ export function groupDatesByYear(dates) {
234
+ return groupDates(dates, d => d.getFullYear());
235
+ }
236
+ /**
237
+ * Group dates by month (YYYY-MM format)
238
+ * @param dates - Array of dates
239
+ * @returns Map of month key to array of dates
240
+ */
241
+ export function groupDatesByMonth(dates) {
242
+ return groupDates(dates, d => {
243
+ const year = d.getFullYear();
244
+ const month = String(d.getMonth() + 1).padStart(2, '0');
245
+ return `${year}-${month}`;
246
+ });
247
+ }
248
+ /**
249
+ * Group dates by day (YYYY-MM-DD format)
250
+ * @param dates - Array of dates
251
+ * @returns Map of day key to array of dates
252
+ */
253
+ export function groupDatesByDay(dates) {
254
+ return groupDates(dates, d => d.toISOString().slice(0, 10));
255
+ }
256
+ /**
257
+ * Group dates by day of week (0-6, Sunday-Saturday)
258
+ * @param dates - Array of dates
259
+ * @returns Map of day of week to array of dates
260
+ */
261
+ export function groupDatesByDayOfWeek(dates) {
262
+ return groupDates(dates, d => d.getDay());
263
+ }
264
+ /**
265
+ * Calculate the median date from an array
266
+ * @param dates - Array of dates
267
+ * @returns The median date, or undefined if array is empty
268
+ */
269
+ export function medianDate(dates) {
270
+ if (dates.length === 0)
271
+ return undefined;
272
+ const sorted = sortDates(dates);
273
+ const mid = Math.floor(sorted.length / 2);
274
+ if (sorted.length % 2 === 0) {
275
+ // Average of two middle values
276
+ const time1 = sorted[mid - 1].getTime();
277
+ const time2 = sorted[mid].getTime();
278
+ return new Date((time1 + time2) / 2);
279
+ }
280
+ return new Date(sorted[mid]);
281
+ }
282
+ /**
283
+ * Calculate the average/mean date from an array
284
+ * @param dates - Array of dates
285
+ * @returns The average date, or undefined if array is empty
286
+ */
287
+ export function averageDate(dates) {
288
+ if (dates.length === 0)
289
+ return undefined;
290
+ const sum = dates.reduce((acc, date) => acc + date.getTime(), 0);
291
+ return new Date(sum / dates.length);
292
+ }
293
+ /**
294
+ * Round a date to the nearest unit
295
+ * @param date - The date to round
296
+ * @param unit - Unit to round to
297
+ * @returns New rounded date
298
+ * @example
299
+ * roundDate(new Date('2024-03-15T14:37:00'), 'hour') // 2024-03-15T15:00:00
300
+ * roundDate(new Date('2024-03-15T14:22:00'), 'hour') // 2024-03-15T14:00:00
301
+ */
302
+ export function roundDate(date, unit) {
303
+ const d = new Date(date);
304
+ switch (unit) {
305
+ case 'minute':
306
+ if (d.getSeconds() >= 30) {
307
+ d.setMinutes(d.getMinutes() + 1);
308
+ }
309
+ d.setSeconds(0, 0);
310
+ break;
311
+ case 'hour':
312
+ if (d.getMinutes() >= 30) {
313
+ d.setHours(d.getHours() + 1);
314
+ }
315
+ d.setMinutes(0, 0, 0);
316
+ break;
317
+ case 'day':
318
+ if (d.getHours() >= 12) {
319
+ d.setDate(d.getDate() + 1);
320
+ }
321
+ d.setHours(0, 0, 0, 0);
322
+ break;
323
+ }
324
+ return d;
325
+ }
326
+ /**
327
+ * Snap a date to a grid interval
328
+ * @param date - The date to snap
329
+ * @param intervalMinutes - Interval in minutes (e.g., 15 for quarter-hour)
330
+ * @param mode - Snap mode: 'floor' (down), 'ceil' (up), or 'round' (nearest)
331
+ * @returns New snapped date
332
+ * @example
333
+ * snapDate(new Date('2024-03-15T14:37:00'), 15) // 2024-03-15T14:30:00
334
+ * snapDate(new Date('2024-03-15T14:37:00'), 15, 'ceil') // 2024-03-15T14:45:00
335
+ */
336
+ export function snapDate(date, intervalMinutes, mode = 'round') {
337
+ const ms = date.getTime();
338
+ const intervalMs = intervalMinutes * 60 * 1000;
339
+ let snapped;
340
+ switch (mode) {
341
+ case 'floor':
342
+ snapped = Math.floor(ms / intervalMs) * intervalMs;
343
+ break;
344
+ case 'ceil':
345
+ snapped = Math.ceil(ms / intervalMs) * intervalMs;
346
+ break;
347
+ case 'round':
348
+ snapped = Math.round(ms / intervalMs) * intervalMs;
349
+ break;
350
+ }
351
+ return new Date(snapped);
352
+ }
353
+ /**
354
+ * Check if dates are in chronological order
355
+ * @param dates - Array of dates
356
+ * @param strict - If true, requires strictly increasing (no duplicates)
357
+ * @returns True if dates are in order
358
+ */
359
+ export function isChronological(dates, strict = false) {
360
+ for (let i = 1; i < dates.length; i++) {
361
+ const prev = dates[i - 1].getTime();
362
+ const curr = dates[i].getTime();
363
+ if (strict ? curr <= prev : curr < prev) {
364
+ return false;
365
+ }
366
+ }
367
+ return true;
368
+ }
369
+ /**
370
+ * Get the span (duration) between min and max dates
371
+ * @param dates - Array of dates
372
+ * @returns Duration in milliseconds, or 0 if less than 2 dates
373
+ */
374
+ export function dateSpan(dates) {
375
+ const extent = dateExtent(dates);
376
+ if (!extent)
377
+ return 0;
378
+ return extent.max.getTime() - extent.min.getTime();
379
+ }
380
+ /**
381
+ * Partition dates into buckets based on a predicate
382
+ * @param dates - Array of dates
383
+ * @param predicate - Function that returns true for dates in first partition
384
+ * @returns Tuple of [matching, non-matching] date arrays
385
+ */
386
+ export function partitionDates(dates, predicate) {
387
+ const matching = [];
388
+ const nonMatching = [];
389
+ for (const date of dates) {
390
+ if (predicate(date)) {
391
+ matching.push(date);
392
+ }
393
+ else {
394
+ nonMatching.push(date);
395
+ }
396
+ }
397
+ return [matching, nonMatching];
398
+ }
399
+ /**
400
+ * Get the nth date from an array (supports negative indices)
401
+ * @param dates - Array of dates (will be sorted)
402
+ * @param n - Index (0-based, negative counts from end)
403
+ * @returns The nth date, or undefined if out of bounds
404
+ * @example
405
+ * nthDate(dates, 0) // earliest
406
+ * nthDate(dates, -1) // latest
407
+ * nthDate(dates, 2) // third earliest
408
+ */
409
+ export function nthDate(dates, n) {
410
+ if (dates.length === 0)
411
+ return undefined;
412
+ const sorted = sortDates(dates);
413
+ const index = n < 0 ? sorted.length + n : n;
414
+ if (index < 0 || index >= sorted.length)
415
+ return undefined;
416
+ return sorted[index];
417
+ }