ts-time-utils 4.0.1 → 4.4.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 +175 -30
- package/dist/{age.js → age.cjs} +14 -6
- package/dist/{calculate.js → calculate.cjs} +30 -18
- package/dist/{calendar.js → calendar.cjs} +80 -39
- package/dist/{calendars.js → calendars.cjs} +48 -23
- package/dist/{chain.js → chain.cjs} +41 -40
- package/dist/{compare.js → compare.cjs} +58 -28
- package/dist/constants.cjs +19 -0
- package/dist/{countdown.js → countdown.cjs} +16 -7
- package/dist/{cron.js → cron.cjs} +20 -9
- package/dist/{dateRange.js → dateRange.cjs} +42 -26
- package/dist/{duration.js → duration.cjs} +56 -44
- package/dist/esm/chain.js +0 -5
- package/dist/esm/finance.d.ts +236 -0
- package/dist/esm/finance.d.ts.map +1 -0
- package/dist/esm/finance.js +495 -0
- package/dist/esm/healthcare.d.ts +260 -0
- package/dist/esm/healthcare.d.ts.map +1 -0
- package/dist/esm/healthcare.js +447 -0
- package/dist/esm/index.d.ts +6 -0
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +6 -0
- package/dist/esm/naturalLanguage.d.ts +1 -3
- package/dist/esm/naturalLanguage.d.ts.map +1 -1
- package/dist/esm/naturalLanguage.js +9 -2
- package/dist/esm/plugins.d.ts +0 -6
- package/dist/esm/plugins.d.ts.map +1 -1
- package/dist/esm/plugins.js +36 -42
- package/dist/esm/recurrence.d.ts.map +1 -1
- package/dist/esm/recurrence.js +3 -5
- package/dist/esm/scheduling.d.ts +206 -0
- package/dist/esm/scheduling.d.ts.map +1 -0
- package/dist/esm/scheduling.js +329 -0
- package/dist/esm/timezone.d.ts +6 -1
- package/dist/esm/timezone.d.ts.map +1 -1
- package/dist/esm/timezone.js +106 -66
- package/dist/esm/types.d.ts +0 -4
- package/dist/esm/types.d.ts.map +1 -1
- package/dist/finance.cjs +512 -0
- package/dist/finance.d.ts +236 -0
- package/dist/finance.d.ts.map +1 -0
- package/dist/{fiscal.js → fiscal.cjs} +36 -17
- package/dist/{format.js → format.cjs} +83 -70
- package/dist/healthcare.cjs +462 -0
- package/dist/healthcare.d.ts +260 -0
- package/dist/healthcare.d.ts.map +1 -0
- package/dist/{holidays.js → holidays.cjs} +52 -25
- package/dist/index.cjs +595 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/{interval.js → interval.cjs} +24 -11
- package/dist/{iterate.js → iterate.cjs} +84 -41
- package/dist/{locale.js → locale.cjs} +54 -26
- package/dist/{naturalLanguage.js → naturalLanguage.cjs} +36 -23
- package/dist/naturalLanguage.d.ts +1 -3
- package/dist/naturalLanguage.d.ts.map +1 -1
- package/dist/{parse.js → parse.cjs} +24 -11
- package/dist/{performance.js → performance.cjs} +23 -10
- package/dist/{plugins.js → plugins.cjs} +48 -47
- package/dist/plugins.d.ts +0 -6
- package/dist/plugins.d.ts.map +1 -1
- package/dist/{precision.js → precision.cjs} +74 -37
- package/dist/{rangePresets.js → rangePresets.cjs} +40 -19
- package/dist/{recurrence.js → recurrence.cjs} +27 -21
- package/dist/recurrence.d.ts.map +1 -1
- package/dist/scheduling.cjs +344 -0
- package/dist/scheduling.d.ts +206 -0
- package/dist/scheduling.d.ts.map +1 -0
- package/dist/{serialize.js → serialize.cjs} +36 -17
- package/dist/{temporal.js → temporal.cjs} +28 -13
- package/dist/{timezone.js → timezone.cjs} +140 -82
- package/dist/timezone.d.ts +6 -1
- package/dist/timezone.d.ts.map +1 -1
- package/dist/{types.js → types.cjs} +9 -3
- package/dist/types.d.ts +0 -4
- package/dist/types.d.ts.map +1 -1
- package/dist/{validate.js → validate.cjs} +54 -26
- package/dist/{workingHours.js → workingHours.cjs} +36 -17
- package/package.json +52 -34
- package/dist/constants.js +0 -16
- package/dist/index.js +0 -66
|
@@ -1,11 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
1
2
|
/**
|
|
2
3
|
* Cron expression utilities for scheduling
|
|
3
4
|
*/
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.CRON_PRESETS = void 0;
|
|
7
|
+
exports.parseCronExpression = parseCronExpression;
|
|
8
|
+
exports.parseCronField = parseCronField;
|
|
9
|
+
exports.matchesCron = matchesCron;
|
|
10
|
+
exports.getNextCronDate = getNextCronDate;
|
|
11
|
+
exports.getNextCronDates = getNextCronDates;
|
|
12
|
+
exports.getPreviousCronDate = getPreviousCronDate;
|
|
13
|
+
exports.isValidCron = isValidCron;
|
|
14
|
+
exports.describeCron = describeCron;
|
|
4
15
|
/**
|
|
5
16
|
* Parse a cron expression into its parts
|
|
6
17
|
* @param expression - cron expression (5 fields: minute hour dayOfMonth month dayOfWeek)
|
|
7
18
|
*/
|
|
8
|
-
|
|
19
|
+
function parseCronExpression(expression) {
|
|
9
20
|
const parts = expression.trim().split(/\s+/);
|
|
10
21
|
if (parts.length !== 5) {
|
|
11
22
|
return null;
|
|
@@ -24,7 +35,7 @@ export function parseCronExpression(expression) {
|
|
|
24
35
|
* @param min - minimum valid value
|
|
25
36
|
* @param max - maximum valid value
|
|
26
37
|
*/
|
|
27
|
-
|
|
38
|
+
function parseCronField(field, min, max) {
|
|
28
39
|
const values = [];
|
|
29
40
|
// Handle wildcard
|
|
30
41
|
if (field === '*') {
|
|
@@ -84,7 +95,7 @@ export function parseCronField(field, min, max) {
|
|
|
84
95
|
* @param date - date to check
|
|
85
96
|
* @param expression - cron expression
|
|
86
97
|
*/
|
|
87
|
-
|
|
98
|
+
function matchesCron(date, expression) {
|
|
88
99
|
const parts = parseCronExpression(expression);
|
|
89
100
|
if (!parts)
|
|
90
101
|
return false;
|
|
@@ -108,7 +119,7 @@ export function matchesCron(date, expression) {
|
|
|
108
119
|
* @param after - start searching after this date (default: now)
|
|
109
120
|
* @param maxIterations - maximum iterations to prevent infinite loops
|
|
110
121
|
*/
|
|
111
|
-
|
|
122
|
+
function getNextCronDate(expression, after = new Date(), maxIterations = 525600 // Max 1 year in minutes
|
|
112
123
|
) {
|
|
113
124
|
const parts = parseCronExpression(expression);
|
|
114
125
|
if (!parts)
|
|
@@ -142,7 +153,7 @@ export function getNextCronDate(expression, after = new Date(), maxIterations =
|
|
|
142
153
|
* @param count - number of dates to get
|
|
143
154
|
* @param after - start searching after this date
|
|
144
155
|
*/
|
|
145
|
-
|
|
156
|
+
function getNextCronDates(expression, count, after = new Date()) {
|
|
146
157
|
const dates = [];
|
|
147
158
|
let currentAfter = after;
|
|
148
159
|
for (let i = 0; i < count; i++) {
|
|
@@ -160,7 +171,7 @@ export function getNextCronDates(expression, count, after = new Date()) {
|
|
|
160
171
|
* @param before - start searching before this date
|
|
161
172
|
* @param maxIterations - maximum iterations to prevent infinite loops
|
|
162
173
|
*/
|
|
163
|
-
|
|
174
|
+
function getPreviousCronDate(expression, before = new Date(), maxIterations = 525600) {
|
|
164
175
|
const parts = parseCronExpression(expression);
|
|
165
176
|
if (!parts)
|
|
166
177
|
return null;
|
|
@@ -191,7 +202,7 @@ export function getPreviousCronDate(expression, before = new Date(), maxIteratio
|
|
|
191
202
|
* Validate a cron expression
|
|
192
203
|
* @param expression - cron expression to validate
|
|
193
204
|
*/
|
|
194
|
-
|
|
205
|
+
function isValidCron(expression) {
|
|
195
206
|
const parts = parseCronExpression(expression);
|
|
196
207
|
if (!parts)
|
|
197
208
|
return false;
|
|
@@ -206,7 +217,7 @@ export function isValidCron(expression) {
|
|
|
206
217
|
* Convert a cron expression to a human-readable description
|
|
207
218
|
* @param expression - cron expression
|
|
208
219
|
*/
|
|
209
|
-
|
|
220
|
+
function describeCron(expression) {
|
|
210
221
|
const parts = parseCronExpression(expression);
|
|
211
222
|
if (!parts)
|
|
212
223
|
return null;
|
|
@@ -277,7 +288,7 @@ export function describeCron(expression) {
|
|
|
277
288
|
/**
|
|
278
289
|
* Common cron expressions
|
|
279
290
|
*/
|
|
280
|
-
|
|
291
|
+
exports.CRON_PRESETS = {
|
|
281
292
|
everyMinute: '* * * * *',
|
|
282
293
|
everyHour: '0 * * * *',
|
|
283
294
|
everyDay: '0 0 * * *',
|
|
@@ -1,8 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
1
2
|
/**
|
|
2
3
|
* @fileoverview Extended date range operations and utilities
|
|
3
4
|
* Provides advanced operations for working with date ranges beyond basic intervals
|
|
4
5
|
*/
|
|
5
|
-
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.dateRangeOverlap = dateRangeOverlap;
|
|
8
|
+
exports.hasOverlappingRanges = hasOverlappingRanges;
|
|
9
|
+
exports.mergeDateRanges = mergeDateRanges;
|
|
10
|
+
exports.findGaps = findGaps;
|
|
11
|
+
exports.splitRange = splitRange;
|
|
12
|
+
exports.containsDate = containsDate;
|
|
13
|
+
exports.getIntersection = getIntersection;
|
|
14
|
+
exports.getUnion = getUnion;
|
|
15
|
+
exports.subtractRange = subtractRange;
|
|
16
|
+
exports.getRangeDuration = getRangeDuration;
|
|
17
|
+
exports.expandRange = expandRange;
|
|
18
|
+
exports.shrinkRange = shrinkRange;
|
|
19
|
+
exports.rangeContains = rangeContains;
|
|
20
|
+
exports.sortRanges = sortRanges;
|
|
21
|
+
const calculate_js_1 = require("./calculate.cjs");
|
|
6
22
|
/**
|
|
7
23
|
* Checks if two date ranges overlap
|
|
8
24
|
* @param range1 - First date range
|
|
@@ -17,7 +33,7 @@ import { addTime } from './calculate.js';
|
|
|
17
33
|
* dateRangeOverlap(range1, range2); // true
|
|
18
34
|
* ```
|
|
19
35
|
*/
|
|
20
|
-
|
|
36
|
+
function dateRangeOverlap(range1, range2) {
|
|
21
37
|
return range1.start <= range2.end && range2.start <= range1.end;
|
|
22
38
|
}
|
|
23
39
|
/**
|
|
@@ -35,7 +51,7 @@ export function dateRangeOverlap(range1, range2) {
|
|
|
35
51
|
* hasOverlappingRanges(ranges); // true
|
|
36
52
|
* ```
|
|
37
53
|
*/
|
|
38
|
-
|
|
54
|
+
function hasOverlappingRanges(ranges) {
|
|
39
55
|
for (let i = 0; i < ranges.length; i++) {
|
|
40
56
|
for (let j = i + 1; j < ranges.length; j++) {
|
|
41
57
|
if (dateRangeOverlap(ranges[i], ranges[j])) {
|
|
@@ -65,7 +81,7 @@ export function hasOverlappingRanges(ranges) {
|
|
|
65
81
|
* // ]
|
|
66
82
|
* ```
|
|
67
83
|
*/
|
|
68
|
-
|
|
84
|
+
function mergeDateRanges(ranges) {
|
|
69
85
|
if (ranges.length === 0)
|
|
70
86
|
return [];
|
|
71
87
|
// Sort ranges by start date
|
|
@@ -112,7 +128,7 @@ export function mergeDateRanges(ranges) {
|
|
|
112
128
|
* // ]
|
|
113
129
|
* ```
|
|
114
130
|
*/
|
|
115
|
-
|
|
131
|
+
function findGaps(ranges, bounds) {
|
|
116
132
|
if (ranges.length === 0) {
|
|
117
133
|
return bounds ? [{ ...bounds }] : [];
|
|
118
134
|
}
|
|
@@ -125,7 +141,7 @@ export function findGaps(ranges, bounds) {
|
|
|
125
141
|
if (bounds && sorted[0].start > bounds.start) {
|
|
126
142
|
gaps.push({
|
|
127
143
|
start: new Date(bounds.start),
|
|
128
|
-
end: addTime(sorted[0].start, -1, 'millisecond')
|
|
144
|
+
end: (0, calculate_js_1.addTime)(sorted[0].start, -1, 'millisecond')
|
|
129
145
|
});
|
|
130
146
|
}
|
|
131
147
|
// Find gaps between ranges
|
|
@@ -134,15 +150,15 @@ export function findGaps(ranges, bounds) {
|
|
|
134
150
|
const nextStart = sorted[i + 1].start;
|
|
135
151
|
if (nextStart > currentEnd) {
|
|
136
152
|
gaps.push({
|
|
137
|
-
start: addTime(currentEnd, 1, 'day'),
|
|
138
|
-
end: addTime(nextStart, -1, 'day')
|
|
153
|
+
start: (0, calculate_js_1.addTime)(currentEnd, 1, 'day'),
|
|
154
|
+
end: (0, calculate_js_1.addTime)(nextStart, -1, 'day')
|
|
139
155
|
});
|
|
140
156
|
}
|
|
141
157
|
}
|
|
142
158
|
// Check gap after last range if bounds provided
|
|
143
159
|
if (bounds && sorted[sorted.length - 1].end < bounds.end) {
|
|
144
160
|
gaps.push({
|
|
145
|
-
start: addTime(sorted[sorted.length - 1].end, 1, 'millisecond'),
|
|
161
|
+
start: (0, calculate_js_1.addTime)(sorted[sorted.length - 1].end, 1, 'millisecond'),
|
|
146
162
|
end: new Date(bounds.end)
|
|
147
163
|
});
|
|
148
164
|
}
|
|
@@ -166,13 +182,13 @@ export function findGaps(ranges, bounds) {
|
|
|
166
182
|
* // Returns 4 ranges: 3 days, 3 days, 3 days, 1 day
|
|
167
183
|
* ```
|
|
168
184
|
*/
|
|
169
|
-
|
|
185
|
+
function splitRange(range, chunkSize, unit) {
|
|
170
186
|
const chunks = [];
|
|
171
187
|
let current = new Date(range.start);
|
|
172
188
|
const rangeEnd = new Date(range.end);
|
|
173
189
|
// Keep looping while current hasn't passed the end
|
|
174
190
|
while (current.getTime() <= rangeEnd.getTime()) {
|
|
175
|
-
const chunkEnd = addTime(current, chunkSize, unit);
|
|
191
|
+
const chunkEnd = (0, calculate_js_1.addTime)(current, chunkSize, unit);
|
|
176
192
|
// Don't go past the range end
|
|
177
193
|
const effectiveEnd = chunkEnd > rangeEnd ? new Date(rangeEnd) : new Date(chunkEnd);
|
|
178
194
|
chunks.push({
|
|
@@ -206,7 +222,7 @@ export function splitRange(range, chunkSize, unit) {
|
|
|
206
222
|
* containsDate(range, new Date('2024-02-01')); // false
|
|
207
223
|
* ```
|
|
208
224
|
*/
|
|
209
|
-
|
|
225
|
+
function containsDate(range, date, inclusive = true) {
|
|
210
226
|
const checkDate = new Date(date);
|
|
211
227
|
if (inclusive) {
|
|
212
228
|
return checkDate >= range.start && checkDate <= range.end;
|
|
@@ -228,7 +244,7 @@ export function containsDate(range, date, inclusive = true) {
|
|
|
228
244
|
* // { start: Date('2024-01-10'), end: Date('2024-01-15') }
|
|
229
245
|
* ```
|
|
230
246
|
*/
|
|
231
|
-
|
|
247
|
+
function getIntersection(range1, range2) {
|
|
232
248
|
if (!dateRangeOverlap(range1, range2)) {
|
|
233
249
|
return null;
|
|
234
250
|
}
|
|
@@ -252,7 +268,7 @@ export function getIntersection(range1, range2) {
|
|
|
252
268
|
* // { start: Date('2024-01-01'), end: Date('2024-01-20') }
|
|
253
269
|
* ```
|
|
254
270
|
*/
|
|
255
|
-
|
|
271
|
+
function getUnion(range1, range2) {
|
|
256
272
|
return {
|
|
257
273
|
start: new Date(Math.min(range1.start.getTime(), range2.start.getTime())),
|
|
258
274
|
end: new Date(Math.max(range1.end.getTime(), range2.end.getTime()))
|
|
@@ -276,7 +292,7 @@ export function getUnion(range1, range2) {
|
|
|
276
292
|
* // ]
|
|
277
293
|
* ```
|
|
278
294
|
*/
|
|
279
|
-
|
|
295
|
+
function subtractRange(range, subtract) {
|
|
280
296
|
// No overlap, return original range
|
|
281
297
|
if (!dateRangeOverlap(range, subtract)) {
|
|
282
298
|
return [{ ...range }];
|
|
@@ -286,13 +302,13 @@ export function subtractRange(range, subtract) {
|
|
|
286
302
|
if (range.start < subtract.start) {
|
|
287
303
|
result.push({
|
|
288
304
|
start: new Date(range.start),
|
|
289
|
-
end: addTime(subtract.start, -1, 'day')
|
|
305
|
+
end: (0, calculate_js_1.addTime)(subtract.start, -1, 'day')
|
|
290
306
|
});
|
|
291
307
|
}
|
|
292
308
|
// Check if there's a range after the subtraction
|
|
293
309
|
if (range.end > subtract.end) {
|
|
294
310
|
result.push({
|
|
295
|
-
start: addTime(subtract.end, 1, 'day'),
|
|
311
|
+
start: (0, calculate_js_1.addTime)(subtract.end, 1, 'day'),
|
|
296
312
|
end: new Date(range.end)
|
|
297
313
|
});
|
|
298
314
|
}
|
|
@@ -313,7 +329,7 @@ export function subtractRange(range, subtract) {
|
|
|
313
329
|
* getRangeDuration(range); // 86400000 (1 day in ms)
|
|
314
330
|
* ```
|
|
315
331
|
*/
|
|
316
|
-
|
|
332
|
+
function getRangeDuration(range) {
|
|
317
333
|
return range.end.getTime() - range.start.getTime();
|
|
318
334
|
}
|
|
319
335
|
/**
|
|
@@ -338,15 +354,15 @@ export function getRangeDuration(range) {
|
|
|
338
354
|
* // { start: Date('2024-01-05'), end: Date('2024-01-20') }
|
|
339
355
|
* ```
|
|
340
356
|
*/
|
|
341
|
-
|
|
357
|
+
function expandRange(range, amount, unit, options = {}) {
|
|
342
358
|
const { direction = 'both' } = options;
|
|
343
359
|
let newStart = new Date(range.start);
|
|
344
360
|
let newEnd = new Date(range.end);
|
|
345
361
|
if (direction === 'both' || direction === 'before') {
|
|
346
|
-
newStart = addTime(newStart, -amount, unit);
|
|
362
|
+
newStart = (0, calculate_js_1.addTime)(newStart, -amount, unit);
|
|
347
363
|
}
|
|
348
364
|
if (direction === 'both' || direction === 'after') {
|
|
349
|
-
newEnd = addTime(newEnd, amount, unit);
|
|
365
|
+
newEnd = (0, calculate_js_1.addTime)(newEnd, amount, unit);
|
|
350
366
|
}
|
|
351
367
|
return {
|
|
352
368
|
start: newStart,
|
|
@@ -372,15 +388,15 @@ export function expandRange(range, amount, unit, options = {}) {
|
|
|
372
388
|
* // { start: Date('2024-01-06'), end: Date('2024-01-26') }
|
|
373
389
|
* ```
|
|
374
390
|
*/
|
|
375
|
-
|
|
391
|
+
function shrinkRange(range, amount, unit, options = {}) {
|
|
376
392
|
const { direction = 'both' } = options;
|
|
377
393
|
let newStart = new Date(range.start);
|
|
378
394
|
let newEnd = new Date(range.end);
|
|
379
395
|
if (direction === 'both' || direction === 'start') {
|
|
380
|
-
newStart = addTime(newStart, amount, unit);
|
|
396
|
+
newStart = (0, calculate_js_1.addTime)(newStart, amount, unit);
|
|
381
397
|
}
|
|
382
398
|
if (direction === 'both' || direction === 'end') {
|
|
383
|
-
newEnd = addTime(newEnd, -amount, unit);
|
|
399
|
+
newEnd = (0, calculate_js_1.addTime)(newEnd, -amount, unit);
|
|
384
400
|
}
|
|
385
401
|
// Check if result is valid
|
|
386
402
|
if (newStart >= newEnd) {
|
|
@@ -405,7 +421,7 @@ export function shrinkRange(range, amount, unit, options = {}) {
|
|
|
405
421
|
* rangeContains(outer, inner); // true
|
|
406
422
|
* ```
|
|
407
423
|
*/
|
|
408
|
-
|
|
424
|
+
function rangeContains(outer, inner) {
|
|
409
425
|
return outer.start <= inner.start && outer.end >= inner.end;
|
|
410
426
|
}
|
|
411
427
|
/**
|
|
@@ -424,7 +440,7 @@ export function rangeContains(outer, inner) {
|
|
|
424
440
|
* sortRanges(ranges); // Sorted by start date ascending
|
|
425
441
|
* ```
|
|
426
442
|
*/
|
|
427
|
-
|
|
443
|
+
function sortRanges(ranges, order = 'asc') {
|
|
428
444
|
const sorted = [...ranges].sort((a, b) => {
|
|
429
445
|
const diff = a.start.getTime() - b.start.getTime();
|
|
430
446
|
return order === 'asc' ? diff : -diff;
|
|
@@ -1,8 +1,19 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Duration = void 0;
|
|
4
|
+
exports.createDuration = createDuration;
|
|
5
|
+
exports.isValidDuration = isValidDuration;
|
|
6
|
+
exports.parseDurationString = parseDurationString;
|
|
7
|
+
exports.formatDurationString = formatDurationString;
|
|
8
|
+
exports.maxDuration = maxDuration;
|
|
9
|
+
exports.minDuration = minDuration;
|
|
10
|
+
exports.sumDurations = sumDurations;
|
|
11
|
+
exports.averageDuration = averageDuration;
|
|
12
|
+
const constants_js_1 = require("./constants.cjs");
|
|
2
13
|
/**
|
|
3
14
|
* Represents a duration of time with arithmetic and conversion capabilities
|
|
4
15
|
*/
|
|
5
|
-
|
|
16
|
+
class Duration {
|
|
6
17
|
constructor(input) {
|
|
7
18
|
if (typeof input === 'number') {
|
|
8
19
|
this._milliseconds = input;
|
|
@@ -24,31 +35,31 @@ export class Duration {
|
|
|
24
35
|
* Create Duration from seconds
|
|
25
36
|
*/
|
|
26
37
|
static fromSeconds(seconds) {
|
|
27
|
-
return new Duration(seconds * MILLISECONDS_PER_SECOND);
|
|
38
|
+
return new Duration(seconds * constants_js_1.MILLISECONDS_PER_SECOND);
|
|
28
39
|
}
|
|
29
40
|
/**
|
|
30
41
|
* Create Duration from minutes
|
|
31
42
|
*/
|
|
32
43
|
static fromMinutes(minutes) {
|
|
33
|
-
return new Duration(minutes * MILLISECONDS_PER_MINUTE);
|
|
44
|
+
return new Duration(minutes * constants_js_1.MILLISECONDS_PER_MINUTE);
|
|
34
45
|
}
|
|
35
46
|
/**
|
|
36
47
|
* Create Duration from hours
|
|
37
48
|
*/
|
|
38
49
|
static fromHours(hours) {
|
|
39
|
-
return new Duration(hours * MILLISECONDS_PER_HOUR);
|
|
50
|
+
return new Duration(hours * constants_js_1.MILLISECONDS_PER_HOUR);
|
|
40
51
|
}
|
|
41
52
|
/**
|
|
42
53
|
* Create Duration from days
|
|
43
54
|
*/
|
|
44
55
|
static fromDays(days) {
|
|
45
|
-
return new Duration(days * MILLISECONDS_PER_DAY);
|
|
56
|
+
return new Duration(days * constants_js_1.MILLISECONDS_PER_DAY);
|
|
46
57
|
}
|
|
47
58
|
/**
|
|
48
59
|
* Create Duration from weeks
|
|
49
60
|
*/
|
|
50
61
|
static fromWeeks(weeks) {
|
|
51
|
-
return new Duration(weeks * MILLISECONDS_PER_WEEK);
|
|
62
|
+
return new Duration(weeks * constants_js_1.MILLISECONDS_PER_WEEK);
|
|
52
63
|
}
|
|
53
64
|
/**
|
|
54
65
|
* Create Duration from a string (e.g., "1h 30m", "2.5 hours", "90 seconds")
|
|
@@ -72,31 +83,31 @@ export class Duration {
|
|
|
72
83
|
* Get duration in seconds
|
|
73
84
|
*/
|
|
74
85
|
get seconds() {
|
|
75
|
-
return this._milliseconds / MILLISECONDS_PER_SECOND;
|
|
86
|
+
return this._milliseconds / constants_js_1.MILLISECONDS_PER_SECOND;
|
|
76
87
|
}
|
|
77
88
|
/**
|
|
78
89
|
* Get duration in minutes
|
|
79
90
|
*/
|
|
80
91
|
get minutes() {
|
|
81
|
-
return this._milliseconds / MILLISECONDS_PER_MINUTE;
|
|
92
|
+
return this._milliseconds / constants_js_1.MILLISECONDS_PER_MINUTE;
|
|
82
93
|
}
|
|
83
94
|
/**
|
|
84
95
|
* Get duration in hours
|
|
85
96
|
*/
|
|
86
97
|
get hours() {
|
|
87
|
-
return this._milliseconds / MILLISECONDS_PER_HOUR;
|
|
98
|
+
return this._milliseconds / constants_js_1.MILLISECONDS_PER_HOUR;
|
|
88
99
|
}
|
|
89
100
|
/**
|
|
90
101
|
* Get duration in days
|
|
91
102
|
*/
|
|
92
103
|
get days() {
|
|
93
|
-
return this._milliseconds / MILLISECONDS_PER_DAY;
|
|
104
|
+
return this._milliseconds / constants_js_1.MILLISECONDS_PER_DAY;
|
|
94
105
|
}
|
|
95
106
|
/**
|
|
96
107
|
* Get duration in weeks
|
|
97
108
|
*/
|
|
98
109
|
get weeks() {
|
|
99
|
-
return this._milliseconds / MILLISECONDS_PER_WEEK;
|
|
110
|
+
return this._milliseconds / constants_js_1.MILLISECONDS_PER_WEEK;
|
|
100
111
|
}
|
|
101
112
|
/**
|
|
102
113
|
* Add another duration
|
|
@@ -197,10 +208,10 @@ export class Duration {
|
|
|
197
208
|
const parts = [];
|
|
198
209
|
let remaining = Math.abs(this._milliseconds);
|
|
199
210
|
const units = [
|
|
200
|
-
{ name: 'd', value: MILLISECONDS_PER_DAY },
|
|
201
|
-
{ name: 'h', value: MILLISECONDS_PER_HOUR },
|
|
202
|
-
{ name: 'm', value: MILLISECONDS_PER_MINUTE },
|
|
203
|
-
{ name: 's', value: MILLISECONDS_PER_SECOND },
|
|
211
|
+
{ name: 'd', value: constants_js_1.MILLISECONDS_PER_DAY },
|
|
212
|
+
{ name: 'h', value: constants_js_1.MILLISECONDS_PER_HOUR },
|
|
213
|
+
{ name: 'm', value: constants_js_1.MILLISECONDS_PER_MINUTE },
|
|
214
|
+
{ name: 's', value: constants_js_1.MILLISECONDS_PER_SECOND },
|
|
204
215
|
{ name: 'ms', value: 1 }
|
|
205
216
|
];
|
|
206
217
|
for (const unit of units) {
|
|
@@ -218,11 +229,11 @@ export class Duration {
|
|
|
218
229
|
*/
|
|
219
230
|
toObject() {
|
|
220
231
|
const ms = Math.abs(this._milliseconds);
|
|
221
|
-
const days = Math.floor(ms / MILLISECONDS_PER_DAY);
|
|
222
|
-
const hours = Math.floor((ms % MILLISECONDS_PER_DAY) / MILLISECONDS_PER_HOUR);
|
|
223
|
-
const minutes = Math.floor((ms % MILLISECONDS_PER_HOUR) / MILLISECONDS_PER_MINUTE);
|
|
224
|
-
const seconds = Math.floor((ms % MILLISECONDS_PER_MINUTE) / MILLISECONDS_PER_SECOND);
|
|
225
|
-
const milliseconds = ms % MILLISECONDS_PER_SECOND;
|
|
232
|
+
const days = Math.floor(ms / constants_js_1.MILLISECONDS_PER_DAY);
|
|
233
|
+
const hours = Math.floor((ms % constants_js_1.MILLISECONDS_PER_DAY) / constants_js_1.MILLISECONDS_PER_HOUR);
|
|
234
|
+
const minutes = Math.floor((ms % constants_js_1.MILLISECONDS_PER_HOUR) / constants_js_1.MILLISECONDS_PER_MINUTE);
|
|
235
|
+
const seconds = Math.floor((ms % constants_js_1.MILLISECONDS_PER_MINUTE) / constants_js_1.MILLISECONDS_PER_SECOND);
|
|
236
|
+
const milliseconds = ms % constants_js_1.MILLISECONDS_PER_SECOND;
|
|
226
237
|
return {
|
|
227
238
|
years: 0, // Years/months require complex calendar calculations
|
|
228
239
|
months: 0,
|
|
@@ -251,20 +262,20 @@ export class Duration {
|
|
|
251
262
|
if (input.milliseconds)
|
|
252
263
|
total += input.milliseconds;
|
|
253
264
|
if (input.seconds)
|
|
254
|
-
total += input.seconds * MILLISECONDS_PER_SECOND;
|
|
265
|
+
total += input.seconds * constants_js_1.MILLISECONDS_PER_SECOND;
|
|
255
266
|
if (input.minutes)
|
|
256
|
-
total += input.minutes * MILLISECONDS_PER_MINUTE;
|
|
267
|
+
total += input.minutes * constants_js_1.MILLISECONDS_PER_MINUTE;
|
|
257
268
|
if (input.hours)
|
|
258
|
-
total += input.hours * MILLISECONDS_PER_HOUR;
|
|
269
|
+
total += input.hours * constants_js_1.MILLISECONDS_PER_HOUR;
|
|
259
270
|
if (input.days)
|
|
260
|
-
total += input.days * MILLISECONDS_PER_DAY;
|
|
271
|
+
total += input.days * constants_js_1.MILLISECONDS_PER_DAY;
|
|
261
272
|
if (input.weeks)
|
|
262
|
-
total += input.weeks * MILLISECONDS_PER_WEEK;
|
|
273
|
+
total += input.weeks * constants_js_1.MILLISECONDS_PER_WEEK;
|
|
263
274
|
// Approximate conversions for months and years
|
|
264
275
|
if (input.months)
|
|
265
|
-
total += input.months * MILLISECONDS_PER_DAY * 30.44; // Average month
|
|
276
|
+
total += input.months * constants_js_1.MILLISECONDS_PER_DAY * 30.44; // Average month
|
|
266
277
|
if (input.years)
|
|
267
|
-
total += input.years * MILLISECONDS_PER_DAY * 365.25; // Average year
|
|
278
|
+
total += input.years * constants_js_1.MILLISECONDS_PER_DAY * 365.25; // Average year
|
|
268
279
|
return total;
|
|
269
280
|
}
|
|
270
281
|
parseString(str) {
|
|
@@ -275,14 +286,14 @@ export class Duration {
|
|
|
275
286
|
}
|
|
276
287
|
// Handle complex formats like "1h 30m 45s"
|
|
277
288
|
const patterns = [
|
|
278
|
-
{ regex: /(\d+(?:\.\d+)?)\s*y(?:ears?)?(?!\w)/g, multiplier: MILLISECONDS_PER_DAY * 365.25 },
|
|
279
|
-
{ regex: /(\d+(?:\.\d+)?)\s*mo(?:nths?)?(?!\w)/g, multiplier: MILLISECONDS_PER_DAY * 30.44 },
|
|
280
|
-
{ regex: /(\d+(?:\.\d+)?)\s*w(?:eeks?)?(?!\w)/g, multiplier: MILLISECONDS_PER_WEEK },
|
|
281
|
-
{ regex: /(\d+(?:\.\d+)?)\s*d(?:ays?)?(?!\w)/g, multiplier: MILLISECONDS_PER_DAY },
|
|
282
|
-
{ regex: /(\d+(?:\.\d+)?)\s*h(?:ours?)?(?!\w)/g, multiplier: MILLISECONDS_PER_HOUR },
|
|
283
|
-
{ regex: /(\d+(?:\.\d+)?)\s*min(?:utes?)?(?!\w)/g, multiplier: MILLISECONDS_PER_MINUTE },
|
|
284
|
-
{ regex: /(\d+(?:\.\d+)?)\s*m(?!s|o)(?!\w)/g, multiplier: MILLISECONDS_PER_MINUTE },
|
|
285
|
-
{ regex: /(\d+(?:\.\d+)?)\s*s(?:ec(?:onds?)?)?(?!\w)/g, multiplier: MILLISECONDS_PER_SECOND },
|
|
289
|
+
{ regex: /(\d+(?:\.\d+)?)\s*y(?:ears?)?(?!\w)/g, multiplier: constants_js_1.MILLISECONDS_PER_DAY * 365.25 },
|
|
290
|
+
{ regex: /(\d+(?:\.\d+)?)\s*mo(?:nths?)?(?!\w)/g, multiplier: constants_js_1.MILLISECONDS_PER_DAY * 30.44 },
|
|
291
|
+
{ regex: /(\d+(?:\.\d+)?)\s*w(?:eeks?)?(?!\w)/g, multiplier: constants_js_1.MILLISECONDS_PER_WEEK },
|
|
292
|
+
{ regex: /(\d+(?:\.\d+)?)\s*d(?:ays?)?(?!\w)/g, multiplier: constants_js_1.MILLISECONDS_PER_DAY },
|
|
293
|
+
{ regex: /(\d+(?:\.\d+)?)\s*h(?:ours?)?(?!\w)/g, multiplier: constants_js_1.MILLISECONDS_PER_HOUR },
|
|
294
|
+
{ regex: /(\d+(?:\.\d+)?)\s*min(?:utes?)?(?!\w)/g, multiplier: constants_js_1.MILLISECONDS_PER_MINUTE },
|
|
295
|
+
{ regex: /(\d+(?:\.\d+)?)\s*m(?!s|o)(?!\w)/g, multiplier: constants_js_1.MILLISECONDS_PER_MINUTE },
|
|
296
|
+
{ regex: /(\d+(?:\.\d+)?)\s*s(?:ec(?:onds?)?)?(?!\w)/g, multiplier: constants_js_1.MILLISECONDS_PER_SECOND },
|
|
286
297
|
{ regex: /(\d+(?:\.\d+)?)\s*ms(?!\w)/g, multiplier: 1 }
|
|
287
298
|
];
|
|
288
299
|
let total = 0;
|
|
@@ -307,28 +318,29 @@ export class Duration {
|
|
|
307
318
|
return new Duration(input);
|
|
308
319
|
}
|
|
309
320
|
}
|
|
321
|
+
exports.Duration = Duration;
|
|
310
322
|
/**
|
|
311
323
|
* Create a new Duration instance
|
|
312
324
|
*/
|
|
313
|
-
|
|
325
|
+
function createDuration(input) {
|
|
314
326
|
return new Duration(input);
|
|
315
327
|
}
|
|
316
328
|
/**
|
|
317
329
|
* Check if a value is a valid duration
|
|
318
330
|
*/
|
|
319
|
-
|
|
331
|
+
function isValidDuration(value) {
|
|
320
332
|
return value instanceof Duration;
|
|
321
333
|
}
|
|
322
334
|
/**
|
|
323
335
|
* Parse duration from various formats
|
|
324
336
|
*/
|
|
325
|
-
|
|
337
|
+
function parseDurationString(input) {
|
|
326
338
|
return new Duration(input);
|
|
327
339
|
}
|
|
328
340
|
/**
|
|
329
341
|
* Format duration to human-readable string
|
|
330
342
|
*/
|
|
331
|
-
|
|
343
|
+
function formatDurationString(duration, options) {
|
|
332
344
|
const d = typeof duration === 'number' ? new Duration(duration) : duration;
|
|
333
345
|
if (options?.long) {
|
|
334
346
|
const obj = d.toObject();
|
|
@@ -352,7 +364,7 @@ export function formatDurationString(duration, options) {
|
|
|
352
364
|
/**
|
|
353
365
|
* Get the maximum duration from an array
|
|
354
366
|
*/
|
|
355
|
-
|
|
367
|
+
function maxDuration(...durations) {
|
|
356
368
|
if (durations.length === 0)
|
|
357
369
|
return null;
|
|
358
370
|
return durations.reduce((max, current) => current.greaterThan(max) ? current : max);
|
|
@@ -360,7 +372,7 @@ export function maxDuration(...durations) {
|
|
|
360
372
|
/**
|
|
361
373
|
* Get the minimum duration from an array
|
|
362
374
|
*/
|
|
363
|
-
|
|
375
|
+
function minDuration(...durations) {
|
|
364
376
|
if (durations.length === 0)
|
|
365
377
|
return null;
|
|
366
378
|
return durations.reduce((min, current) => current.lessThan(min) ? current : min);
|
|
@@ -368,13 +380,13 @@ export function minDuration(...durations) {
|
|
|
368
380
|
/**
|
|
369
381
|
* Sum multiple durations
|
|
370
382
|
*/
|
|
371
|
-
|
|
383
|
+
function sumDurations(...durations) {
|
|
372
384
|
return durations.reduce((sum, current) => sum.add(current), new Duration(0));
|
|
373
385
|
}
|
|
374
386
|
/**
|
|
375
387
|
* Get average duration from an array
|
|
376
388
|
*/
|
|
377
|
-
|
|
389
|
+
function averageDuration(...durations) {
|
|
378
390
|
if (durations.length === 0)
|
|
379
391
|
return null;
|
|
380
392
|
const sum = sumDurations(...durations);
|
package/dist/esm/chain.js
CHANGED
|
@@ -415,8 +415,3 @@ export function chain(date) {
|
|
|
415
415
|
export function formatMs(ms, options) {
|
|
416
416
|
return formatDuration(ms, options);
|
|
417
417
|
}
|
|
418
|
-
// Initialize plugin system if it's available
|
|
419
|
-
// This allows plugins to extend ChainedDate
|
|
420
|
-
if (typeof globalThis !== 'undefined') {
|
|
421
|
-
globalThis.__chainedDateClass = ChainedDate;
|
|
422
|
-
}
|