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,205 @@
1
+ /**
2
+ * ChronosPeriodCollection - Manage collections of ChronosPeriod
3
+ * Inspired by spatie/period PHP library
4
+ * @see https://github.com/spatie/period
5
+ */
6
+ import { ChronosPeriod } from './period';
7
+ import { Chronos } from './chronos';
8
+ import { DateInput } from '../types';
9
+ /**
10
+ * ChronosPeriodCollection - A collection of periods with powerful operations
11
+ *
12
+ * Inspired by spatie/period, this class provides a rich API for working with
13
+ * collections of time periods including overlap detection, gap analysis,
14
+ * and set operations.
15
+ *
16
+ * @example
17
+ * ```typescript
18
+ * const collection = new ChronosPeriodCollection([
19
+ * ChronosPeriod.create('2024-01-01', '2024-01-15'),
20
+ * ChronosPeriod.create('2024-01-10', '2024-01-25'),
21
+ * ChronosPeriod.create('2024-02-01', '2024-02-15'),
22
+ * ]);
23
+ *
24
+ * // Find overlapping periods
25
+ * const overlapping = collection.overlapAll();
26
+ *
27
+ * // Get gaps between periods
28
+ * const gaps = collection.gaps();
29
+ *
30
+ * // Get boundaries
31
+ * const boundaries = collection.boundaries();
32
+ * ```
33
+ */
34
+ export declare class ChronosPeriodCollection implements Iterable<ChronosPeriod> {
35
+ private _periods;
36
+ constructor(periods?: ChronosPeriod[]);
37
+ /**
38
+ * Create a new collection from periods
39
+ */
40
+ static create(...periods: ChronosPeriod[]): ChronosPeriodCollection;
41
+ /**
42
+ * Create an empty collection
43
+ */
44
+ static empty(): ChronosPeriodCollection;
45
+ /**
46
+ * Create a collection from an array of date pairs
47
+ */
48
+ static fromDatePairs(pairs: Array<[DateInput, DateInput]>): ChronosPeriodCollection;
49
+ /** Add a period to the collection */
50
+ add(period: ChronosPeriod): this;
51
+ /** Add multiple periods */
52
+ addAll(periods: ChronosPeriod[]): this;
53
+ /** Get a shallow copy of periods */
54
+ toArray(): ChronosPeriod[];
55
+ /** Get the number of periods in the collection */
56
+ get length(): number;
57
+ /** Check if collection is empty */
58
+ isEmpty(): boolean;
59
+ /** Check if collection is not empty */
60
+ isNotEmpty(): boolean;
61
+ /** Get a period at a specific index */
62
+ get(index: number): ChronosPeriod | undefined;
63
+ /** Get the first period */
64
+ first(): ChronosPeriod | undefined;
65
+ /** Get the last period */
66
+ last(): ChronosPeriod | undefined;
67
+ /** Clear collection */
68
+ clear(): this;
69
+ /** Iterator implementation */
70
+ [Symbol.iterator](): Iterator<ChronosPeriod>;
71
+ /** ForEach iteration */
72
+ forEach(callback: (period: ChronosPeriod, index: number) => void): void;
73
+ /** Map periods to a new array */
74
+ map<T>(callback: (period: ChronosPeriod, index: number) => T): T[];
75
+ /** Filter periods */
76
+ filter(predicate: (period: ChronosPeriod, index: number) => boolean): ChronosPeriodCollection;
77
+ /** Reduce periods to a single value */
78
+ reduce<T>(callback: (acc: T, period: ChronosPeriod, index: number) => T, initial: T): T;
79
+ /** Find a period matching a predicate */
80
+ find(predicate: (period: ChronosPeriod, index: number) => boolean): ChronosPeriod | undefined;
81
+ /** Check if any period matches a predicate */
82
+ some(predicate: (period: ChronosPeriod, index: number) => boolean): boolean;
83
+ /** Check if all periods match a predicate */
84
+ every(predicate: (period: ChronosPeriod, index: number) => boolean): boolean;
85
+ /**
86
+ * Get the overall boundaries of the collection
87
+ * Returns a period from the earliest start to the latest end
88
+ */
89
+ boundaries(): ChronosPeriod | null;
90
+ /**
91
+ * Get the earliest start date across all periods
92
+ */
93
+ start(): Chronos | null;
94
+ /**
95
+ * Get the latest end date across all periods
96
+ */
97
+ end(): Chronos | null;
98
+ /** Normalize and merge overlapping/adjacent periods */
99
+ normalize(): ChronosPeriod[];
100
+ /** Check if any period overlaps with the provided period */
101
+ overlaps(period: ChronosPeriod): boolean;
102
+ /**
103
+ * Check if any period in the collection overlaps with any other period
104
+ * in the collection (internal overlaps)
105
+ */
106
+ overlapAny(): boolean;
107
+ /**
108
+ * Get all overlapping period segments across the collection
109
+ * Returns periods where two or more periods in the collection overlap
110
+ */
111
+ overlapAll(): ChronosPeriodCollection;
112
+ /** Return intersections between collection and a given period */
113
+ intersect(period: ChronosPeriod): ChronosPeriodCollection;
114
+ /**
115
+ * Get intersection of all periods in the collection
116
+ * Returns the period where ALL periods overlap (if any)
117
+ */
118
+ intersectAll(): ChronosPeriod | null;
119
+ /** Return the union (merged) of all periods in the collection */
120
+ union(): ChronosPeriodCollection;
121
+ /**
122
+ * Alias for normalize() - returns merged periods
123
+ * @deprecated Use union() instead
124
+ */
125
+ unionAll(): ChronosPeriod[];
126
+ /** Merge collection into a single union period if contiguous/overlapping */
127
+ mergeToSingle(): ChronosPeriod | null;
128
+ /** Return gaps between merged periods */
129
+ gaps(): ChronosPeriodCollection;
130
+ /**
131
+ * Check if there are any gaps between periods
132
+ */
133
+ hasGaps(): boolean;
134
+ /**
135
+ * Subtract a period from all periods in the collection
136
+ * Returns periods with the subtracted portion removed
137
+ */
138
+ subtract(period: ChronosPeriod): ChronosPeriodCollection;
139
+ /**
140
+ * Subtract multiple periods from the collection
141
+ */
142
+ subtractAll(periods: ChronosPeriod[]): ChronosPeriodCollection;
143
+ /**
144
+ * Check if any period touches (is adjacent to) the given period
145
+ * Two periods touch if one ends exactly where the other begins
146
+ */
147
+ touchesWith(period: ChronosPeriod): boolean;
148
+ /**
149
+ * Check if two periods touch (are adjacent)
150
+ */
151
+ private _periodsTouch;
152
+ /**
153
+ * Get all periods that touch the given period
154
+ */
155
+ touchingPeriods(period: ChronosPeriod): ChronosPeriodCollection;
156
+ /**
157
+ * Check if a date is contained in any period of the collection
158
+ */
159
+ contains(date: DateInput): boolean;
160
+ /**
161
+ * Check if a period is fully contained in any period of the collection
162
+ */
163
+ containsPeriod(period: ChronosPeriod): boolean;
164
+ /**
165
+ * Check if two collections are equal (same periods)
166
+ */
167
+ equals(other: ChronosPeriodCollection): boolean;
168
+ /**
169
+ * Get periods sorted by start date
170
+ */
171
+ private _sortedByStart;
172
+ /**
173
+ * Sort periods by start date (ascending)
174
+ */
175
+ sortByStart(): ChronosPeriodCollection;
176
+ /**
177
+ * Sort periods by end date (ascending)
178
+ */
179
+ sortByEnd(): ChronosPeriodCollection;
180
+ /**
181
+ * Sort periods by duration (ascending)
182
+ */
183
+ sortByDuration(): ChronosPeriodCollection;
184
+ /**
185
+ * Reverse the order of periods
186
+ */
187
+ reverse(): ChronosPeriodCollection;
188
+ /**
189
+ * Convert to JSON
190
+ */
191
+ toJSON(): object[];
192
+ /**
193
+ * Convert to string
194
+ */
195
+ toString(): string;
196
+ /**
197
+ * Get total duration across all periods (in days)
198
+ * Note: Overlapping portions may be counted multiple times
199
+ */
200
+ totalDays(): number;
201
+ /**
202
+ * Get total unique duration (merged periods, no double-counting)
203
+ */
204
+ uniqueDays(): number;
205
+ }