chronos-ts 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.
@@ -0,0 +1,428 @@
1
+ /**
2
+ * ChronosPeriod - Date range iteration
3
+ * @module ChronosPeriod
4
+ */
5
+ import { Duration, AnyTimeUnit, DateInput, PeriodOptions } from '../types';
6
+ import { Chronos } from './chronos';
7
+ import { ChronosInterval } from './interval';
8
+ /**
9
+ * ChronosPeriod - Represents a date range with iteration capabilities
10
+ *
11
+ * Inspired by CarbonPeriod, this class provides powerful date range
12
+ * iteration with support for various interval types, filters, and
13
+ * recurrence patterns.
14
+ *
15
+ * @example
16
+ * ```typescript
17
+ * // Basic iteration
18
+ * const period = ChronosPeriod.create('2024-01-01', '2024-01-31');
19
+ * for (const date of period) {
20
+ * console.log(date.format('YYYY-MM-DD'));
21
+ * }
22
+ *
23
+ * // With custom interval
24
+ * const weekly = ChronosPeriod.create('2024-01-01', '2024-03-31')
25
+ * .setInterval(ChronosInterval.weeks(1));
26
+ *
27
+ * // With filters
28
+ * const weekdays = period.filter(date => date.dayOfWeek < 6);
29
+ *
30
+ * // Recurrence
31
+ * const recur = ChronosPeriod.recur('2024-01-01')
32
+ * .every(1, 'month')
33
+ * .times(12);
34
+ * ```
35
+ */
36
+ export declare class ChronosPeriod implements Iterable<Chronos> {
37
+ private _start;
38
+ private _end;
39
+ private _interval;
40
+ private _recurrences;
41
+ private _options;
42
+ private _filters;
43
+ private _current;
44
+ private _locale;
45
+ /**
46
+ * Create a new ChronosPeriod
47
+ */
48
+ constructor(start: DateInput, end?: DateInput, interval?: Duration | ChronosInterval, options?: Partial<PeriodOptions>);
49
+ /**
50
+ * Create a period between two dates
51
+ */
52
+ static create(start: DateInput, end?: DateInput, interval?: Duration | ChronosInterval): ChronosPeriod;
53
+ /**
54
+ * Create a period from a start date with recurrences
55
+ */
56
+ static recur(start: DateInput, interval?: Duration | ChronosInterval): ChronosPeriod;
57
+ /**
58
+ * Create a period for a specific number of days
59
+ */
60
+ static days(start: DateInput, count: number): ChronosPeriod;
61
+ /**
62
+ * Create a period for a specific number of weeks
63
+ */
64
+ static weeks(start: DateInput, count: number): ChronosPeriod;
65
+ /**
66
+ * Create a period for a specific number of months
67
+ */
68
+ static months(start: DateInput, count: number): ChronosPeriod;
69
+ /**
70
+ * Create a period for a specific number of years
71
+ */
72
+ static years(start: DateInput, count: number): ChronosPeriod;
73
+ /**
74
+ * Create a period for the current month
75
+ */
76
+ static currentMonth(): ChronosPeriod;
77
+ /**
78
+ * Create a period for the current year
79
+ */
80
+ static currentYear(): ChronosPeriod;
81
+ /**
82
+ * Create a period for the current week
83
+ */
84
+ static currentWeek(): ChronosPeriod;
85
+ /**
86
+ * Create a period for the current quarter
87
+ */
88
+ static currentQuarter(): ChronosPeriod;
89
+ /**
90
+ * Alias for currentWeek()
91
+ */
92
+ static thisWeek(): ChronosPeriod;
93
+ /**
94
+ * Alias for currentMonth()
95
+ */
96
+ static thisMonth(): ChronosPeriod;
97
+ /**
98
+ * Alias for currentYear()
99
+ */
100
+ static thisYear(): ChronosPeriod;
101
+ /**
102
+ * Alias for currentQuarter()
103
+ */
104
+ static thisQuarter(): ChronosPeriod;
105
+ /**
106
+ * Create a period for the previous week
107
+ */
108
+ static lastWeek(): ChronosPeriod;
109
+ /**
110
+ * Create a period for the previous month
111
+ */
112
+ static lastMonth(): ChronosPeriod;
113
+ /**
114
+ * Create a period for the previous year
115
+ */
116
+ static lastYear(): ChronosPeriod;
117
+ /**
118
+ * Create a period for the previous quarter
119
+ */
120
+ static lastQuarter(): ChronosPeriod;
121
+ /**
122
+ * Create a period for the next week
123
+ */
124
+ static nextWeek(): ChronosPeriod;
125
+ /**
126
+ * Create a period for the next month
127
+ */
128
+ static nextMonth(): ChronosPeriod;
129
+ /**
130
+ * Create a period for the next year
131
+ */
132
+ static nextYear(): ChronosPeriod;
133
+ /**
134
+ * Create a period for the next quarter
135
+ */
136
+ static nextQuarter(): ChronosPeriod;
137
+ /**
138
+ * Create a period between two dates (alias for create)
139
+ */
140
+ static between(start: DateInput, end: DateInput, interval?: Duration | ChronosInterval): ChronosPeriod;
141
+ /**
142
+ * Create a period from an ISO 8601 repeating interval string
143
+ * @example
144
+ * ```typescript
145
+ * ChronosPeriod.fromISO('R5/2024-01-01/P1D') // 5 recurrences, daily from 2024-01-01
146
+ * ChronosPeriod.fromISO('2024-01-01/2024-01-31') // Date range
147
+ * ChronosPeriod.fromISO('2024-01-01/P1M') // From date with duration
148
+ * ```
149
+ */
150
+ static fromISO(iso: string): ChronosPeriod;
151
+ /**
152
+ * Get the start date
153
+ */
154
+ get start(): Chronos;
155
+ /**
156
+ * Get the end date
157
+ */
158
+ get end(): Chronos | null;
159
+ /**
160
+ * Get the interval
161
+ */
162
+ get interval(): ChronosInterval;
163
+ /**
164
+ * Get the number of recurrences
165
+ */
166
+ get recurrences(): number | null;
167
+ /**
168
+ * Check if the period includes the start boundary
169
+ */
170
+ get includesStart(): boolean;
171
+ /**
172
+ * Check if the period includes the end boundary
173
+ */
174
+ get includesEnd(): boolean;
175
+ /**
176
+ * Check if the period has an end date
177
+ */
178
+ get hasEnd(): boolean;
179
+ /**
180
+ * Check if the period is unbounded
181
+ */
182
+ get isUnbounded(): boolean;
183
+ /**
184
+ * Set the start date
185
+ */
186
+ setStart(start: DateInput): ChronosPeriod;
187
+ /**
188
+ * Set the end date
189
+ */
190
+ setEnd(end: DateInput): ChronosPeriod;
191
+ /**
192
+ * Set the interval
193
+ */
194
+ setInterval(interval: Duration | ChronosInterval): ChronosPeriod;
195
+ /**
196
+ * Set the number of recurrences
197
+ */
198
+ times(count: number): ChronosPeriod;
199
+ /**
200
+ * Set interval by unit
201
+ */
202
+ every(amount: number, unit: AnyTimeUnit): ChronosPeriod;
203
+ /**
204
+ * Exclude the start boundary
205
+ */
206
+ excludeStart(): ChronosPeriod;
207
+ /**
208
+ * Exclude the end boundary
209
+ */
210
+ excludeEnd(): ChronosPeriod;
211
+ /**
212
+ * Include the start boundary
213
+ */
214
+ includeStart(): ChronosPeriod;
215
+ /**
216
+ * Include the end boundary
217
+ */
218
+ includeEnd(): ChronosPeriod;
219
+ /**
220
+ * Add a filter function
221
+ */
222
+ filter(fn: (date: Chronos, key: number) => boolean): ChronosPeriod;
223
+ /**
224
+ * Filter to only include weekdays
225
+ */
226
+ weekdays(): ChronosPeriod;
227
+ /**
228
+ * Alias for weekdays()
229
+ */
230
+ filterWeekdays(): ChronosPeriod;
231
+ /**
232
+ * Filter to only include weekends
233
+ */
234
+ weekends(): ChronosPeriod;
235
+ /**
236
+ * Alias for weekends()
237
+ */
238
+ filterWeekends(): ChronosPeriod;
239
+ /**
240
+ * Filter to only include specific days of week
241
+ */
242
+ onlyDays(...days: number[]): ChronosPeriod;
243
+ /**
244
+ * Filter to exclude specific days of week
245
+ */
246
+ exceptDays(...days: number[]): ChronosPeriod;
247
+ /**
248
+ * Filter to only include specific months
249
+ */
250
+ onlyMonths(...months: number[]): ChronosPeriod;
251
+ /**
252
+ * Filter to exclude specific months
253
+ */
254
+ exceptMonths(...months: number[]): ChronosPeriod;
255
+ /**
256
+ * Clear all filters
257
+ */
258
+ clearFilters(): ChronosPeriod;
259
+ /**
260
+ * Get all dates in the period as an array
261
+ */
262
+ toArray(): Chronos[];
263
+ /**
264
+ * Iterate over the period
265
+ */
266
+ [Symbol.iterator](): Iterator<Chronos>;
267
+ /**
268
+ * Apply the interval to a date
269
+ */
270
+ private _applyInterval;
271
+ /**
272
+ * Check if iteration should continue
273
+ */
274
+ private _shouldContinue;
275
+ /**
276
+ * Check if a date passes all filters
277
+ */
278
+ private _passesFilters;
279
+ /**
280
+ * Get count of dates in the period
281
+ */
282
+ count(): number;
283
+ /**
284
+ * Get the first date in the period
285
+ */
286
+ first(): Chronos | null;
287
+ /**
288
+ * Get the last date in the period
289
+ */
290
+ last(): Chronos | null;
291
+ /**
292
+ * Get a date at a specific index
293
+ */
294
+ nth(index: number): Chronos | null;
295
+ /**
296
+ * Check if a date is within the period
297
+ */
298
+ contains(date: DateInput): boolean;
299
+ /**
300
+ * For each iteration
301
+ */
302
+ forEach(callback: (date: Chronos, index: number) => void): void;
303
+ /**
304
+ * Map dates to another type
305
+ */
306
+ map<T>(callback: (date: Chronos, index: number) => T): T[];
307
+ /**
308
+ * Reduce dates to a single value
309
+ */
310
+ reduce<T>(callback: (acc: T, date: Chronos, index: number) => T, initial: T): T;
311
+ /**
312
+ * Check if two periods overlap
313
+ */
314
+ overlaps(other: ChronosPeriod): boolean;
315
+ /**
316
+ * Get the intersection of two periods
317
+ */
318
+ intersect(other: ChronosPeriod): ChronosPeriod | null;
319
+ /**
320
+ * Get the union of two periods
321
+ */
322
+ union(other: ChronosPeriod): ChronosPeriod | null;
323
+ /**
324
+ * Get the difference between two periods
325
+ */
326
+ diff(other: ChronosPeriod): ChronosPeriod[];
327
+ /**
328
+ * Check if this period is adjacent to another
329
+ */
330
+ private _adjacentTo;
331
+ /**
332
+ * Get the duration of the period
333
+ */
334
+ duration(): ChronosInterval;
335
+ /**
336
+ * Get the number of days in the period
337
+ */
338
+ days(): number;
339
+ /**
340
+ * Get the number of weeks in the period
341
+ */
342
+ weeks(): number;
343
+ /**
344
+ * Get the number of months in the period
345
+ */
346
+ monthCount(): number;
347
+ /**
348
+ * Get the number of years in the period
349
+ */
350
+ yearCount(): number;
351
+ /**
352
+ * Split the period into chunks
353
+ */
354
+ split(count: number): ChronosPeriod[];
355
+ /**
356
+ * Split by a specific interval
357
+ */
358
+ splitBy(interval: Duration | ChronosInterval): ChronosPeriod[];
359
+ /**
360
+ * Split the period by a specified number of days
361
+ */
362
+ splitByDays(days: number): ChronosPeriod[];
363
+ /**
364
+ * Split the period by a specified number of weeks
365
+ */
366
+ splitByWeeks(weeks: number): ChronosPeriod[];
367
+ /**
368
+ * Split the period by a specified number of months
369
+ */
370
+ splitByMonths(months: number): ChronosPeriod[];
371
+ /**
372
+ * Split the period by a specified number of years
373
+ */
374
+ splitByYears(years: number): ChronosPeriod[];
375
+ /**
376
+ * Skip specific dates from the period iteration
377
+ * @param dates - Dates to exclude from iteration
378
+ */
379
+ skip(dates: DateInput[]): ChronosPeriod;
380
+ /**
381
+ * Convert to ISO 8601 string
382
+ */
383
+ toISO(): string;
384
+ /**
385
+ * Convert to string
386
+ */
387
+ toString(): string;
388
+ /**
389
+ * Convert to human-readable string
390
+ */
391
+ forHumans(): string;
392
+ /**
393
+ * Convert to JSON
394
+ */
395
+ toJSON(): object;
396
+ /**
397
+ * Clone this period
398
+ */
399
+ clone(): ChronosPeriod;
400
+ /**
401
+ * Clone for modification (respects immutable option)
402
+ */
403
+ private _cloneForModification;
404
+ /**
405
+ * Set locale for this period
406
+ */
407
+ locale(code: string): ChronosPeriod;
408
+ /**
409
+ * Create a period for a specific month
410
+ */
411
+ static month(year: number, month: number): ChronosPeriod;
412
+ /**
413
+ * Create a period for a specific year
414
+ */
415
+ static year(year: number): ChronosPeriod;
416
+ /**
417
+ * Create a period for a specific quarter
418
+ */
419
+ static quarter(year: number, quarter: number): ChronosPeriod;
420
+ /**
421
+ * Create a period between two dates as weekdays only
422
+ */
423
+ static weekdaysBetween(start: DateInput, end: DateInput): ChronosPeriod;
424
+ /**
425
+ * Create a period with business days only (weekdays, can add holidays filter)
426
+ */
427
+ static businessDays(start: DateInput, end: DateInput, holidays?: DateInput[]): ChronosPeriod;
428
+ }