ts-time-utils 1.0.0 → 1.1.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 +226 -1
- package/dist/calculate.d.ts.map +1 -1
- package/dist/calculate.js +24 -10
- package/dist/countdown.d.ts +217 -0
- package/dist/countdown.d.ts.map +1 -0
- package/dist/countdown.js +298 -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.map +1 -1
- package/dist/esm/calculate.js +24 -10
- 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/dateRange.d.ts +266 -0
- package/dist/esm/dateRange.d.ts.map +1 -0
- package/dist/esm/dateRange.js +433 -0
- package/dist/esm/index.d.ts +5 -1
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +8 -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/recurrence.d.ts +149 -0
- package/dist/esm/recurrence.d.ts.map +1 -0
- package/dist/esm/recurrence.js +404 -0
- package/dist/esm/types.d.ts +21 -0
- package/dist/esm/types.d.ts.map +1 -1
- package/dist/index.d.ts +5 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8 -0
- package/dist/naturalLanguage.d.ts +107 -0
- package/dist/naturalLanguage.d.ts.map +1 -0
- package/dist/naturalLanguage.js +344 -0
- package/dist/recurrence.d.ts +149 -0
- package/dist/recurrence.d.ts.map +1 -0
- package/dist/recurrence.js +404 -0
- package/dist/types.d.ts +21 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +30 -2
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Countdown and timer utilities for tracking time until/since a target date
|
|
3
|
+
* Provides countdown timers, remaining time calculations, and progress tracking
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Creates a countdown timer to a target date
|
|
7
|
+
* @param targetDate - The date to count down to
|
|
8
|
+
* @param options - Countdown options and callbacks
|
|
9
|
+
* @returns A countdown instance with control methods
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* const countdown = createCountdown(
|
|
14
|
+
* new Date('2024-12-31T23:59:59'),
|
|
15
|
+
* {
|
|
16
|
+
* onTick: (remaining) => {
|
|
17
|
+
* console.log(`${remaining.days}d ${remaining.hours}h ${remaining.minutes}m ${remaining.seconds}s`);
|
|
18
|
+
* },
|
|
19
|
+
* onComplete: () => {
|
|
20
|
+
* console.log('Happy New Year!');
|
|
21
|
+
* }
|
|
22
|
+
* }
|
|
23
|
+
* );
|
|
24
|
+
*
|
|
25
|
+
* countdown.start();
|
|
26
|
+
* // Later...
|
|
27
|
+
* countdown.stop();
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
export function createCountdown(targetDate, options = {}) {
|
|
31
|
+
let target = new Date(targetDate);
|
|
32
|
+
let intervalId = null;
|
|
33
|
+
let running = false;
|
|
34
|
+
const { onTick, onComplete, onExpired, interval = 1000, immediate = true } = options;
|
|
35
|
+
const getRemaining = () => {
|
|
36
|
+
return getRemainingTime(target);
|
|
37
|
+
};
|
|
38
|
+
const tick = () => {
|
|
39
|
+
const remaining = getRemaining();
|
|
40
|
+
if (onTick) {
|
|
41
|
+
onTick(remaining);
|
|
42
|
+
}
|
|
43
|
+
if (remaining.isExpired) {
|
|
44
|
+
stop();
|
|
45
|
+
if (onComplete) {
|
|
46
|
+
onComplete();
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
const start = () => {
|
|
51
|
+
if (running)
|
|
52
|
+
return;
|
|
53
|
+
const remaining = getRemaining();
|
|
54
|
+
if (remaining.totalMilliseconds < 0) {
|
|
55
|
+
if (onExpired) {
|
|
56
|
+
onExpired();
|
|
57
|
+
}
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
running = true;
|
|
61
|
+
if (immediate) {
|
|
62
|
+
tick();
|
|
63
|
+
}
|
|
64
|
+
intervalId = setInterval(tick, interval);
|
|
65
|
+
};
|
|
66
|
+
const stop = () => {
|
|
67
|
+
if (!running)
|
|
68
|
+
return;
|
|
69
|
+
running = false;
|
|
70
|
+
if (intervalId !== null) {
|
|
71
|
+
clearInterval(intervalId);
|
|
72
|
+
intervalId = null;
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
const reset = (newTarget) => {
|
|
76
|
+
stop();
|
|
77
|
+
target = new Date(newTarget);
|
|
78
|
+
};
|
|
79
|
+
const isRunning = () => running;
|
|
80
|
+
const isExpiredCheck = () => {
|
|
81
|
+
return getRemaining().isExpired;
|
|
82
|
+
};
|
|
83
|
+
return {
|
|
84
|
+
start,
|
|
85
|
+
stop,
|
|
86
|
+
reset,
|
|
87
|
+
getRemaining,
|
|
88
|
+
isRunning,
|
|
89
|
+
isExpired: isExpiredCheck
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Gets the remaining time until/since a target date
|
|
94
|
+
* @param targetDate - The target date
|
|
95
|
+
* @param fromDate - The date to calculate from (defaults to now)
|
|
96
|
+
* @returns Object with remaining time broken down by units
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* ```ts
|
|
100
|
+
* const remaining = getRemainingTime(new Date('2024-12-31'));
|
|
101
|
+
* console.log(`${remaining.days} days, ${remaining.hours} hours remaining`);
|
|
102
|
+
*
|
|
103
|
+
* // Check if expired
|
|
104
|
+
* if (remaining.isExpired) {
|
|
105
|
+
* console.log('Target date has passed');
|
|
106
|
+
* }
|
|
107
|
+
* ```
|
|
108
|
+
*/
|
|
109
|
+
export function getRemainingTime(targetDate, fromDate = new Date()) {
|
|
110
|
+
const target = new Date(targetDate);
|
|
111
|
+
const from = new Date(fromDate);
|
|
112
|
+
const totalMilliseconds = target.getTime() - from.getTime();
|
|
113
|
+
const isExpired = totalMilliseconds <= 0;
|
|
114
|
+
// Use absolute values for calculations
|
|
115
|
+
const absTotalMs = Math.abs(totalMilliseconds);
|
|
116
|
+
const totalSeconds = Math.floor(absTotalMs / 1000);
|
|
117
|
+
const totalMinutes = Math.floor(totalSeconds / 60);
|
|
118
|
+
const totalHours = Math.floor(totalMinutes / 60);
|
|
119
|
+
const totalDays = Math.floor(totalHours / 24);
|
|
120
|
+
const milliseconds = Math.floor(absTotalMs % 1000);
|
|
121
|
+
const seconds = totalSeconds % 60;
|
|
122
|
+
const minutes = totalMinutes % 60;
|
|
123
|
+
const hours = totalHours % 24;
|
|
124
|
+
const days = totalDays; // Don't mod by 7 - let the formatter decide
|
|
125
|
+
const weeks = Math.floor(totalDays / 7);
|
|
126
|
+
return {
|
|
127
|
+
totalMilliseconds,
|
|
128
|
+
totalSeconds: totalMilliseconds >= 0 ? totalSeconds : -totalSeconds,
|
|
129
|
+
totalMinutes: totalMilliseconds >= 0 ? totalMinutes : -totalMinutes,
|
|
130
|
+
totalHours: totalMilliseconds >= 0 ? totalHours : -totalHours,
|
|
131
|
+
totalDays: totalMilliseconds >= 0 ? totalDays : -totalDays,
|
|
132
|
+
milliseconds,
|
|
133
|
+
seconds,
|
|
134
|
+
minutes,
|
|
135
|
+
hours,
|
|
136
|
+
days,
|
|
137
|
+
weeks,
|
|
138
|
+
isExpired
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Formats the remaining time as a human-readable string
|
|
143
|
+
* @param targetDate - The target date
|
|
144
|
+
* @param options - Formatting options
|
|
145
|
+
* @returns Formatted countdown string
|
|
146
|
+
*
|
|
147
|
+
* @example
|
|
148
|
+
* ```ts
|
|
149
|
+
* formatCountdown(new Date('2024-12-31'));
|
|
150
|
+
* // "45d 12h 30m 15s"
|
|
151
|
+
*
|
|
152
|
+
* formatCountdown(new Date('2024-12-31'), { units: ['days', 'hours'] });
|
|
153
|
+
* // "45 days, 12 hours"
|
|
154
|
+
*
|
|
155
|
+
* formatCountdown(new Date('2024-12-31'), { short: false });
|
|
156
|
+
* // "45 days 12 hours 30 minutes 15 seconds"
|
|
157
|
+
* ```
|
|
158
|
+
*/
|
|
159
|
+
export function formatCountdown(targetDate, options = {}) {
|
|
160
|
+
const { from, units = ['days', 'hours', 'minutes', 'seconds'], short = true, maxUnits, showZero = false, separator = ' ' } = options;
|
|
161
|
+
const remaining = getRemainingTime(targetDate, from);
|
|
162
|
+
if (remaining.isExpired) {
|
|
163
|
+
return 'Expired';
|
|
164
|
+
}
|
|
165
|
+
const parts = [];
|
|
166
|
+
for (const unit of units) {
|
|
167
|
+
const value = remaining[unit];
|
|
168
|
+
if (value === 0 && !showZero && parts.length === 0) {
|
|
169
|
+
continue;
|
|
170
|
+
}
|
|
171
|
+
if (value === 0 && !showZero) {
|
|
172
|
+
continue;
|
|
173
|
+
}
|
|
174
|
+
if (maxUnits && parts.length >= maxUnits) {
|
|
175
|
+
break;
|
|
176
|
+
}
|
|
177
|
+
if (short) {
|
|
178
|
+
const shortUnit = unit[0];
|
|
179
|
+
parts.push(`${value}${shortUnit}`);
|
|
180
|
+
}
|
|
181
|
+
else {
|
|
182
|
+
const unitName = value === 1 ? unit.slice(0, -1) : unit;
|
|
183
|
+
parts.push(`${value} ${unitName}`);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
return parts.length > 0 ? parts.join(separator) : '0s';
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* Checks if a date has expired (is in the past)
|
|
190
|
+
* @param date - The date to check
|
|
191
|
+
* @param fromDate - The reference date (defaults to now)
|
|
192
|
+
* @returns True if the date is in the past
|
|
193
|
+
*
|
|
194
|
+
* @example
|
|
195
|
+
* ```ts
|
|
196
|
+
* isExpired(new Date('2020-01-01')); // true
|
|
197
|
+
* isExpired(new Date('2030-01-01')); // false
|
|
198
|
+
* ```
|
|
199
|
+
*/
|
|
200
|
+
export function isExpired(date, fromDate = new Date()) {
|
|
201
|
+
const checkDate = new Date(date);
|
|
202
|
+
const from = new Date(fromDate);
|
|
203
|
+
return checkDate.getTime() < from.getTime();
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* Calculates the progress percentage between two dates
|
|
207
|
+
* @param startDate - The start date
|
|
208
|
+
* @param endDate - The end date
|
|
209
|
+
* @param currentDate - The current date (defaults to now)
|
|
210
|
+
* @returns Progress percentage (0-100), clamped to range
|
|
211
|
+
*
|
|
212
|
+
* @example
|
|
213
|
+
* ```ts
|
|
214
|
+
* const progress = getProgressPercentage(
|
|
215
|
+
* new Date('2024-01-01'),
|
|
216
|
+
* new Date('2024-12-31'),
|
|
217
|
+
* new Date('2024-07-01')
|
|
218
|
+
* );
|
|
219
|
+
* console.log(`${progress}% complete`); // ~50% complete
|
|
220
|
+
* ```
|
|
221
|
+
*/
|
|
222
|
+
export function getProgressPercentage(startDate, endDate, currentDate = new Date()) {
|
|
223
|
+
const start = new Date(startDate).getTime();
|
|
224
|
+
const end = new Date(endDate).getTime();
|
|
225
|
+
const current = new Date(currentDate).getTime();
|
|
226
|
+
if (start >= end) {
|
|
227
|
+
throw new Error('Start date must be before end date');
|
|
228
|
+
}
|
|
229
|
+
const total = end - start;
|
|
230
|
+
const elapsed = current - start;
|
|
231
|
+
const percentage = (elapsed / total) * 100;
|
|
232
|
+
// Clamp between 0 and 100
|
|
233
|
+
return Math.max(0, Math.min(100, percentage));
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* Gets time until a target date in a specific unit
|
|
237
|
+
* @param targetDate - The target date
|
|
238
|
+
* @param unit - The unit to return
|
|
239
|
+
* @param fromDate - The date to calculate from (defaults to now)
|
|
240
|
+
* @returns Time remaining in the specified unit
|
|
241
|
+
*
|
|
242
|
+
* @example
|
|
243
|
+
* ```ts
|
|
244
|
+
* getTimeUntil(new Date('2024-12-31'), 'days'); // 45.5
|
|
245
|
+
* getTimeUntil(new Date('2024-12-31'), 'hours'); // 1092
|
|
246
|
+
* getTimeUntil(new Date('2024-12-31'), 'weeks'); // 6.5
|
|
247
|
+
* ```
|
|
248
|
+
*/
|
|
249
|
+
export function getTimeUntil(targetDate, unit, fromDate = new Date()) {
|
|
250
|
+
const remaining = getRemainingTime(targetDate, fromDate);
|
|
251
|
+
switch (unit) {
|
|
252
|
+
case 'milliseconds':
|
|
253
|
+
return remaining.totalMilliseconds;
|
|
254
|
+
case 'seconds':
|
|
255
|
+
return remaining.totalSeconds;
|
|
256
|
+
case 'minutes':
|
|
257
|
+
return remaining.totalMinutes;
|
|
258
|
+
case 'hours':
|
|
259
|
+
return remaining.totalHours;
|
|
260
|
+
case 'days':
|
|
261
|
+
return remaining.totalDays;
|
|
262
|
+
case 'weeks':
|
|
263
|
+
return remaining.totalDays / 7;
|
|
264
|
+
default:
|
|
265
|
+
return remaining.totalMilliseconds;
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
/**
|
|
269
|
+
* Creates a deadline object with useful methods
|
|
270
|
+
* @param targetDate - The deadline date
|
|
271
|
+
* @returns An object with deadline-related methods
|
|
272
|
+
*
|
|
273
|
+
* @example
|
|
274
|
+
* ```ts
|
|
275
|
+
* const deadline = createDeadline(new Date('2024-12-31'));
|
|
276
|
+
*
|
|
277
|
+
* deadline.isExpired(); // false
|
|
278
|
+
* deadline.daysRemaining(); // 45
|
|
279
|
+
* deadline.hoursRemaining(); // 1092
|
|
280
|
+
* deadline.formatRemaining(); // "45d 12h 30m"
|
|
281
|
+
* deadline.progressFrom(new Date('2024-01-01')); // 67.5%
|
|
282
|
+
* ```
|
|
283
|
+
*/
|
|
284
|
+
export function createDeadline(targetDate) {
|
|
285
|
+
const target = new Date(targetDate);
|
|
286
|
+
return {
|
|
287
|
+
target,
|
|
288
|
+
isExpired: () => isExpired(target),
|
|
289
|
+
getRemaining: () => getRemainingTime(target),
|
|
290
|
+
daysRemaining: () => getTimeUntil(target, 'days'),
|
|
291
|
+
hoursRemaining: () => getTimeUntil(target, 'hours'),
|
|
292
|
+
minutesRemaining: () => getTimeUntil(target, 'minutes'),
|
|
293
|
+
secondsRemaining: () => getTimeUntil(target, 'seconds'),
|
|
294
|
+
formatRemaining: (options) => formatCountdown(target, options),
|
|
295
|
+
progressFrom: (startDate) => getProgressPercentage(startDate, target),
|
|
296
|
+
countdown: (options) => createCountdown(target, options)
|
|
297
|
+
};
|
|
298
|
+
}
|
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Extended date range operations and utilities
|
|
3
|
+
* Provides advanced operations for working with date ranges beyond basic intervals
|
|
4
|
+
*/
|
|
5
|
+
import type { DateRange, DateInput } from './types.js';
|
|
6
|
+
/**
|
|
7
|
+
* Checks if two date ranges overlap
|
|
8
|
+
* @param range1 - First date range
|
|
9
|
+
* @param range2 - Second date range
|
|
10
|
+
* @returns True if the ranges overlap
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* const range1 = { start: new Date('2024-01-01'), end: new Date('2024-01-10') };
|
|
15
|
+
* const range2 = { start: new Date('2024-01-05'), end: new Date('2024-01-15') };
|
|
16
|
+
*
|
|
17
|
+
* dateRangeOverlap(range1, range2); // true
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
export declare function dateRangeOverlap(range1: DateRange, range2: DateRange): boolean;
|
|
21
|
+
/**
|
|
22
|
+
* Checks if multiple date ranges have any overlaps
|
|
23
|
+
* @param ranges - Array of date ranges
|
|
24
|
+
* @returns True if any two ranges overlap
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```ts
|
|
28
|
+
* const ranges = [
|
|
29
|
+
* { start: new Date('2024-01-01'), end: new Date('2024-01-10') },
|
|
30
|
+
* { start: new Date('2024-01-05'), end: new Date('2024-01-15') }
|
|
31
|
+
* ];
|
|
32
|
+
*
|
|
33
|
+
* hasOverlappingRanges(ranges); // true
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
export declare function hasOverlappingRanges(ranges: DateRange[]): boolean;
|
|
37
|
+
/**
|
|
38
|
+
* Merges overlapping or adjacent date ranges
|
|
39
|
+
* @param ranges - Array of date ranges to merge
|
|
40
|
+
* @returns Array of merged, non-overlapping ranges
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```ts
|
|
44
|
+
* const ranges = [
|
|
45
|
+
* { start: new Date('2024-01-01'), end: new Date('2024-01-10') },
|
|
46
|
+
* { start: new Date('2024-01-05'), end: new Date('2024-01-15') },
|
|
47
|
+
* { start: new Date('2024-01-20'), end: new Date('2024-01-25') }
|
|
48
|
+
* ];
|
|
49
|
+
*
|
|
50
|
+
* mergeDateRanges(ranges);
|
|
51
|
+
* // [
|
|
52
|
+
* // { start: Date('2024-01-01'), end: Date('2024-01-15') },
|
|
53
|
+
* // { start: Date('2024-01-20'), end: Date('2024-01-25') }
|
|
54
|
+
* // ]
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
export declare function mergeDateRanges(ranges: DateRange[]): DateRange[];
|
|
58
|
+
/**
|
|
59
|
+
* Finds gaps between date ranges within specified bounds
|
|
60
|
+
* @param ranges - Array of date ranges
|
|
61
|
+
* @param bounds - Optional bounds to search within
|
|
62
|
+
* @returns Array of date ranges representing gaps
|
|
63
|
+
*
|
|
64
|
+
* @example
|
|
65
|
+
* ```ts
|
|
66
|
+
* const ranges = [
|
|
67
|
+
* { start: new Date('2024-01-01'), end: new Date('2024-01-05') },
|
|
68
|
+
* { start: new Date('2024-01-10'), end: new Date('2024-01-15') }
|
|
69
|
+
* ];
|
|
70
|
+
*
|
|
71
|
+
* findGaps(ranges, {
|
|
72
|
+
* start: new Date('2024-01-01'),
|
|
73
|
+
* end: new Date('2024-01-20')
|
|
74
|
+
* });
|
|
75
|
+
* // [
|
|
76
|
+
* // { start: Date('2024-01-06'), end: Date('2024-01-09') },
|
|
77
|
+
* // { start: Date('2024-01-16'), end: Date('2024-01-20') }
|
|
78
|
+
* // ]
|
|
79
|
+
* ```
|
|
80
|
+
*/
|
|
81
|
+
export declare function findGaps(ranges: DateRange[], bounds?: DateRange): DateRange[];
|
|
82
|
+
/**
|
|
83
|
+
* Splits a date range into smaller chunks
|
|
84
|
+
* @param range - The date range to split
|
|
85
|
+
* @param chunkSize - Size of each chunk
|
|
86
|
+
* @param unit - Unit for chunk size
|
|
87
|
+
* @returns Array of date ranges
|
|
88
|
+
*
|
|
89
|
+
* @example
|
|
90
|
+
* ```ts
|
|
91
|
+
* const range = {
|
|
92
|
+
* start: new Date('2024-01-01'),
|
|
93
|
+
* end: new Date('2024-01-10')
|
|
94
|
+
* };
|
|
95
|
+
*
|
|
96
|
+
* splitRange(range, 3, 'day');
|
|
97
|
+
* // Returns 4 ranges: 3 days, 3 days, 3 days, 1 day
|
|
98
|
+
* ```
|
|
99
|
+
*/
|
|
100
|
+
export declare function splitRange(range: DateRange, chunkSize: number, unit: 'millisecond' | 'second' | 'minute' | 'hour' | 'day' | 'week' | 'month' | 'year'): DateRange[];
|
|
101
|
+
/**
|
|
102
|
+
* Checks if a date falls within a date range
|
|
103
|
+
* @param range - The date range
|
|
104
|
+
* @param date - The date to check
|
|
105
|
+
* @param inclusive - Whether to include boundary dates (default: true)
|
|
106
|
+
* @returns True if date is within range
|
|
107
|
+
*
|
|
108
|
+
* @example
|
|
109
|
+
* ```ts
|
|
110
|
+
* const range = {
|
|
111
|
+
* start: new Date('2024-01-01'),
|
|
112
|
+
* end: new Date('2024-01-31')
|
|
113
|
+
* };
|
|
114
|
+
*
|
|
115
|
+
* containsDate(range, new Date('2024-01-15')); // true
|
|
116
|
+
* containsDate(range, new Date('2024-02-01')); // false
|
|
117
|
+
* ```
|
|
118
|
+
*/
|
|
119
|
+
export declare function containsDate(range: DateRange, date: DateInput, inclusive?: boolean): boolean;
|
|
120
|
+
/**
|
|
121
|
+
* Gets the intersection of two date ranges
|
|
122
|
+
* @param range1 - First date range
|
|
123
|
+
* @param range2 - Second date range
|
|
124
|
+
* @returns The overlapping range, or null if no overlap
|
|
125
|
+
*
|
|
126
|
+
* @example
|
|
127
|
+
* ```ts
|
|
128
|
+
* const range1 = { start: new Date('2024-01-01'), end: new Date('2024-01-15') };
|
|
129
|
+
* const range2 = { start: new Date('2024-01-10'), end: new Date('2024-01-20') };
|
|
130
|
+
*
|
|
131
|
+
* getIntersection(range1, range2);
|
|
132
|
+
* // { start: Date('2024-01-10'), end: Date('2024-01-15') }
|
|
133
|
+
* ```
|
|
134
|
+
*/
|
|
135
|
+
export declare function getIntersection(range1: DateRange, range2: DateRange): DateRange | null;
|
|
136
|
+
/**
|
|
137
|
+
* Gets the union (combined coverage) of two date ranges
|
|
138
|
+
* @param range1 - First date range
|
|
139
|
+
* @param range2 - Second date range
|
|
140
|
+
* @returns The combined range covering both inputs
|
|
141
|
+
*
|
|
142
|
+
* @example
|
|
143
|
+
* ```ts
|
|
144
|
+
* const range1 = { start: new Date('2024-01-01'), end: new Date('2024-01-15') };
|
|
145
|
+
* const range2 = { start: new Date('2024-01-10'), end: new Date('2024-01-20') };
|
|
146
|
+
*
|
|
147
|
+
* getUnion(range1, range2);
|
|
148
|
+
* // { start: Date('2024-01-01'), end: Date('2024-01-20') }
|
|
149
|
+
* ```
|
|
150
|
+
*/
|
|
151
|
+
export declare function getUnion(range1: DateRange, range2: DateRange): DateRange;
|
|
152
|
+
/**
|
|
153
|
+
* Subtracts one date range from another
|
|
154
|
+
* @param range - The range to subtract from
|
|
155
|
+
* @param subtract - The range to subtract
|
|
156
|
+
* @returns Array of remaining date ranges (0-2 ranges)
|
|
157
|
+
*
|
|
158
|
+
* @example
|
|
159
|
+
* ```ts
|
|
160
|
+
* const range = { start: new Date('2024-01-01'), end: new Date('2024-01-31') };
|
|
161
|
+
* const subtract = { start: new Date('2024-01-10'), end: new Date('2024-01-20') };
|
|
162
|
+
*
|
|
163
|
+
* subtractRange(range, subtract);
|
|
164
|
+
* // [
|
|
165
|
+
* // { start: Date('2024-01-01'), end: Date('2024-01-09') },
|
|
166
|
+
* // { start: Date('2024-01-21'), end: Date('2024-01-31') }
|
|
167
|
+
* // ]
|
|
168
|
+
* ```
|
|
169
|
+
*/
|
|
170
|
+
export declare function subtractRange(range: DateRange, subtract: DateRange): DateRange[];
|
|
171
|
+
/**
|
|
172
|
+
* Calculates the duration of a date range in milliseconds
|
|
173
|
+
* @param range - The date range
|
|
174
|
+
* @returns Duration in milliseconds
|
|
175
|
+
*
|
|
176
|
+
* @example
|
|
177
|
+
* ```ts
|
|
178
|
+
* const range = {
|
|
179
|
+
* start: new Date('2024-01-01'),
|
|
180
|
+
* end: new Date('2024-01-02')
|
|
181
|
+
* };
|
|
182
|
+
*
|
|
183
|
+
* getRangeDuration(range); // 86400000 (1 day in ms)
|
|
184
|
+
* ```
|
|
185
|
+
*/
|
|
186
|
+
export declare function getRangeDuration(range: DateRange): number;
|
|
187
|
+
/**
|
|
188
|
+
* Expands a date range by a specified amount
|
|
189
|
+
* @param range - The date range to expand
|
|
190
|
+
* @param amount - Amount to expand by
|
|
191
|
+
* @param unit - Unit for expansion
|
|
192
|
+
* @param options - Expansion options
|
|
193
|
+
* @returns Expanded date range
|
|
194
|
+
*
|
|
195
|
+
* @example
|
|
196
|
+
* ```ts
|
|
197
|
+
* const range = {
|
|
198
|
+
* start: new Date('2024-01-10'),
|
|
199
|
+
* end: new Date('2024-01-20')
|
|
200
|
+
* };
|
|
201
|
+
*
|
|
202
|
+
* expandRange(range, 5, 'day');
|
|
203
|
+
* // { start: Date('2024-01-05'), end: Date('2024-01-25') }
|
|
204
|
+
*
|
|
205
|
+
* expandRange(range, 5, 'day', { direction: 'before' });
|
|
206
|
+
* // { start: Date('2024-01-05'), end: Date('2024-01-20') }
|
|
207
|
+
* ```
|
|
208
|
+
*/
|
|
209
|
+
export declare function expandRange(range: DateRange, amount: number, unit: 'millisecond' | 'second' | 'minute' | 'hour' | 'day' | 'week' | 'month' | 'year', options?: {
|
|
210
|
+
direction?: 'both' | 'before' | 'after';
|
|
211
|
+
}): DateRange;
|
|
212
|
+
/**
|
|
213
|
+
* Shrinks a date range by a specified amount
|
|
214
|
+
* @param range - The date range to shrink
|
|
215
|
+
* @param amount - Amount to shrink by
|
|
216
|
+
* @param unit - Unit for shrinking
|
|
217
|
+
* @param options - Shrink options
|
|
218
|
+
* @returns Shrunk date range, or null if result would be invalid
|
|
219
|
+
*
|
|
220
|
+
* @example
|
|
221
|
+
* ```ts
|
|
222
|
+
* const range = {
|
|
223
|
+
* start: new Date('2024-01-01'),
|
|
224
|
+
* end: new Date('2024-01-31')
|
|
225
|
+
* };
|
|
226
|
+
*
|
|
227
|
+
* shrinkRange(range, 5, 'day');
|
|
228
|
+
* // { start: Date('2024-01-06'), end: Date('2024-01-26') }
|
|
229
|
+
* ```
|
|
230
|
+
*/
|
|
231
|
+
export declare function shrinkRange(range: DateRange, amount: number, unit: 'millisecond' | 'second' | 'minute' | 'hour' | 'day' | 'week' | 'month' | 'year', options?: {
|
|
232
|
+
direction?: 'both' | 'start' | 'end';
|
|
233
|
+
}): DateRange | null;
|
|
234
|
+
/**
|
|
235
|
+
* Checks if one date range completely contains another
|
|
236
|
+
* @param outer - The potentially containing range
|
|
237
|
+
* @param inner - The potentially contained range
|
|
238
|
+
* @returns True if outer completely contains inner
|
|
239
|
+
*
|
|
240
|
+
* @example
|
|
241
|
+
* ```ts
|
|
242
|
+
* const outer = { start: new Date('2024-01-01'), end: new Date('2024-01-31') };
|
|
243
|
+
* const inner = { start: new Date('2024-01-10'), end: new Date('2024-01-20') };
|
|
244
|
+
*
|
|
245
|
+
* rangeContains(outer, inner); // true
|
|
246
|
+
* ```
|
|
247
|
+
*/
|
|
248
|
+
export declare function rangeContains(outer: DateRange, inner: DateRange): boolean;
|
|
249
|
+
/**
|
|
250
|
+
* Sorts an array of date ranges by start date
|
|
251
|
+
* @param ranges - Array of date ranges
|
|
252
|
+
* @param order - Sort order ('asc' or 'desc')
|
|
253
|
+
* @returns Sorted array of date ranges
|
|
254
|
+
*
|
|
255
|
+
* @example
|
|
256
|
+
* ```ts
|
|
257
|
+
* const ranges = [
|
|
258
|
+
* { start: new Date('2024-01-15'), end: new Date('2024-01-20') },
|
|
259
|
+
* { start: new Date('2024-01-01'), end: new Date('2024-01-10') }
|
|
260
|
+
* ];
|
|
261
|
+
*
|
|
262
|
+
* sortRanges(ranges); // Sorted by start date ascending
|
|
263
|
+
* ```
|
|
264
|
+
*/
|
|
265
|
+
export declare function sortRanges(ranges: DateRange[], order?: 'asc' | 'desc'): DateRange[];
|
|
266
|
+
//# sourceMappingURL=dateRange.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dateRange.d.ts","sourceRoot":"","sources":["../src/dateRange.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAGvD;;;;;;;;;;;;;GAaG;AACH,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,SAAS,GAAG,OAAO,CAE9E;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,oBAAoB,CAAC,MAAM,EAAE,SAAS,EAAE,GAAG,OAAO,CASjE;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,eAAe,CAAC,MAAM,EAAE,SAAS,EAAE,GAAG,SAAS,EAAE,CA0BhE;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,QAAQ,CAAC,MAAM,EAAE,SAAS,EAAE,EAAE,MAAM,CAAC,EAAE,SAAS,GAAG,SAAS,EAAE,CA2C7E;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,UAAU,CACxB,KAAK,EAAE,SAAS,EAChB,SAAS,EAAE,MAAM,EACjB,IAAI,EAAE,aAAa,GAAG,QAAQ,GAAG,QAAQ,GAAG,MAAM,GAAG,KAAK,GAAG,MAAM,GAAG,OAAO,GAAG,MAAM,GACrF,SAAS,EAAE,CA2Bb;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,YAAY,CAC1B,KAAK,EAAE,SAAS,EAChB,IAAI,EAAE,SAAS,EACf,SAAS,UAAO,GACf,OAAO,CAQT;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,eAAe,CAAC,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,SAAS,GAAG,SAAS,GAAG,IAAI,CAStF;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,QAAQ,CAAC,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,SAAS,GAAG,SAAS,CAKxE;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,SAAS,EAAE,QAAQ,EAAE,SAAS,GAAG,SAAS,EAAE,CAyBhF;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,SAAS,GAAG,MAAM,CAEzD;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,WAAW,CACzB,KAAK,EAAE,SAAS,EAChB,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,aAAa,GAAG,QAAQ,GAAG,QAAQ,GAAG,MAAM,GAAG,KAAK,GAAG,MAAM,GAAG,OAAO,GAAG,MAAM,EACtF,OAAO,GAAE;IACP,SAAS,CAAC,EAAE,MAAM,GAAG,QAAQ,GAAG,OAAO,CAAA;CACnC,GACL,SAAS,CAkBX;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,WAAW,CACzB,KAAK,EAAE,SAAS,EAChB,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,aAAa,GAAG,QAAQ,GAAG,QAAQ,GAAG,MAAM,GAAG,KAAK,GAAG,MAAM,GAAG,OAAO,GAAG,MAAM,EACtF,OAAO,GAAE;IACP,SAAS,CAAC,EAAE,MAAM,GAAG,OAAO,GAAG,KAAK,CAAA;CAChC,GACL,SAAS,GAAG,IAAI,CAuBlB;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,SAAS,EAAE,KAAK,EAAE,SAAS,GAAG,OAAO,CAEzE;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,UAAU,CAAC,MAAM,EAAE,SAAS,EAAE,EAAE,KAAK,GAAE,KAAK,GAAG,MAAc,GAAG,SAAS,EAAE,CAO1F"}
|