chronos-ts 1.0.3 → 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.
- package/README.md +251 -433
- package/dist/core/chronos.d.ts +460 -0
- package/dist/core/chronos.js +1259 -0
- package/dist/core/index.d.ts +9 -0
- package/dist/core/index.js +19 -0
- package/dist/core/interval.d.ts +289 -0
- package/dist/core/interval.js +689 -0
- package/dist/core/period-collection.d.ts +205 -0
- package/dist/core/period-collection.js +562 -0
- package/dist/core/period.d.ts +428 -0
- package/dist/core/period.js +1007 -0
- package/dist/core/timezone.d.ts +289 -0
- package/dist/core/timezone.js +671 -0
- package/dist/index.d.ts +50 -3
- package/dist/index.js +148 -7
- package/dist/locales/index.d.ts +66 -0
- package/dist/locales/index.js +847 -0
- package/dist/types/index.d.ts +428 -0
- package/dist/types/index.js +71 -0
- package/dist/utils/index.d.ts +127 -0
- package/dist/utils/index.js +656 -0
- package/package.json +19 -3
- package/dist/interval.d.ts +0 -61
- package/dist/interval.js +0 -82
- package/dist/period.d.ts +0 -185
- package/dist/period.js +0 -326
- package/dist/precision.d.ts +0 -24
- package/dist/precision.js +0 -46
- package/dist/utils.d.ts +0 -188
- package/dist/utils.js +0 -337
package/dist/interval.d.ts
DELETED
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
export declare class Interval {
|
|
2
|
-
private minutes;
|
|
3
|
-
private hours;
|
|
4
|
-
private days;
|
|
5
|
-
private weeks;
|
|
6
|
-
private months;
|
|
7
|
-
/**
|
|
8
|
-
* Constructs a new instance of the Interval class.
|
|
9
|
-
*
|
|
10
|
-
* @param minutes - The number of minutes (default is 0).
|
|
11
|
-
* @param hours - The number of hours (default is 0).
|
|
12
|
-
* @param days - The number of days (default is 0).
|
|
13
|
-
* @param weeks - The number of weeks (default is 0).
|
|
14
|
-
* @param months - The number of months (default is 0).
|
|
15
|
-
*/
|
|
16
|
-
private constructor();
|
|
17
|
-
/**
|
|
18
|
-
* Creates an Interval instance representing the specified number of minutes.
|
|
19
|
-
*
|
|
20
|
-
* @param minutes - The number of minutes for the interval.
|
|
21
|
-
* @returns An Interval instance representing the specified number of minutes.
|
|
22
|
-
*/
|
|
23
|
-
static minutes(minutes: number): Interval;
|
|
24
|
-
/**
|
|
25
|
-
* Creates an Interval instance representing the given number of hours.
|
|
26
|
-
*
|
|
27
|
-
* @param hours - The number of hours for the interval.
|
|
28
|
-
* @returns An Interval instance with the specified hours.
|
|
29
|
-
*/
|
|
30
|
-
static hours(hours: number): Interval;
|
|
31
|
-
/**
|
|
32
|
-
* Creates an Interval instance representing the specified number of days.
|
|
33
|
-
*
|
|
34
|
-
* @param days - The number of days for the interval.
|
|
35
|
-
* @returns An Interval instance with the specified number of days.
|
|
36
|
-
*/
|
|
37
|
-
static days(days: number): Interval;
|
|
38
|
-
/**
|
|
39
|
-
* Creates an Interval instance representing the specified number of weeks.
|
|
40
|
-
*
|
|
41
|
-
* @param weeks - The number of weeks for the interval.
|
|
42
|
-
* @returns An Interval instance with the specified number of weeks.
|
|
43
|
-
*/
|
|
44
|
-
static weeks(weeks: number): Interval;
|
|
45
|
-
/**
|
|
46
|
-
* Creates an Interval instance representing a specified number of months.
|
|
47
|
-
*
|
|
48
|
-
* @param months - The number of months for the interval.
|
|
49
|
-
* @returns An Interval instance with the specified number of months.
|
|
50
|
-
*/
|
|
51
|
-
static months(months: number): Interval;
|
|
52
|
-
/**
|
|
53
|
-
* Calculates the total interval in minutes.
|
|
54
|
-
*
|
|
55
|
-
* This method sums up the minutes, hours, days, weeks, and months
|
|
56
|
-
* to return the total interval in minutes.
|
|
57
|
-
*
|
|
58
|
-
* @returns {number} The total interval in minutes.
|
|
59
|
-
*/
|
|
60
|
-
getMinutesInterval(): number;
|
|
61
|
-
}
|
package/dist/interval.js
DELETED
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Interval = void 0;
|
|
4
|
-
class Interval {
|
|
5
|
-
/**
|
|
6
|
-
* Constructs a new instance of the Interval class.
|
|
7
|
-
*
|
|
8
|
-
* @param minutes - The number of minutes (default is 0).
|
|
9
|
-
* @param hours - The number of hours (default is 0).
|
|
10
|
-
* @param days - The number of days (default is 0).
|
|
11
|
-
* @param weeks - The number of weeks (default is 0).
|
|
12
|
-
* @param months - The number of months (default is 0).
|
|
13
|
-
*/
|
|
14
|
-
constructor(minutes = 0, hours = 0, days = 0, weeks = 0, months = 0) {
|
|
15
|
-
this.minutes = minutes;
|
|
16
|
-
this.hours = hours;
|
|
17
|
-
this.days = days;
|
|
18
|
-
this.weeks = weeks;
|
|
19
|
-
this.months = months;
|
|
20
|
-
}
|
|
21
|
-
/**
|
|
22
|
-
* Creates an Interval instance representing the specified number of minutes.
|
|
23
|
-
*
|
|
24
|
-
* @param minutes - The number of minutes for the interval.
|
|
25
|
-
* @returns An Interval instance representing the specified number of minutes.
|
|
26
|
-
*/
|
|
27
|
-
static minutes(minutes) {
|
|
28
|
-
return new Interval(minutes);
|
|
29
|
-
}
|
|
30
|
-
/**
|
|
31
|
-
* Creates an Interval instance representing the given number of hours.
|
|
32
|
-
*
|
|
33
|
-
* @param hours - The number of hours for the interval.
|
|
34
|
-
* @returns An Interval instance with the specified hours.
|
|
35
|
-
*/
|
|
36
|
-
static hours(hours) {
|
|
37
|
-
return new Interval(0, hours);
|
|
38
|
-
}
|
|
39
|
-
/**
|
|
40
|
-
* Creates an Interval instance representing the specified number of days.
|
|
41
|
-
*
|
|
42
|
-
* @param days - The number of days for the interval.
|
|
43
|
-
* @returns An Interval instance with the specified number of days.
|
|
44
|
-
*/
|
|
45
|
-
static days(days) {
|
|
46
|
-
return new Interval(0, 0, days);
|
|
47
|
-
}
|
|
48
|
-
/**
|
|
49
|
-
* Creates an Interval instance representing the specified number of weeks.
|
|
50
|
-
*
|
|
51
|
-
* @param weeks - The number of weeks for the interval.
|
|
52
|
-
* @returns An Interval instance with the specified number of weeks.
|
|
53
|
-
*/
|
|
54
|
-
static weeks(weeks) {
|
|
55
|
-
return new Interval(0, 0, 0, weeks);
|
|
56
|
-
}
|
|
57
|
-
/**
|
|
58
|
-
* Creates an Interval instance representing a specified number of months.
|
|
59
|
-
*
|
|
60
|
-
* @param months - The number of months for the interval.
|
|
61
|
-
* @returns An Interval instance with the specified number of months.
|
|
62
|
-
*/
|
|
63
|
-
static months(months) {
|
|
64
|
-
return new Interval(0, 0, 0, 0, months);
|
|
65
|
-
}
|
|
66
|
-
/**
|
|
67
|
-
* Calculates the total interval in minutes.
|
|
68
|
-
*
|
|
69
|
-
* This method sums up the minutes, hours, days, weeks, and months
|
|
70
|
-
* to return the total interval in minutes.
|
|
71
|
-
*
|
|
72
|
-
* @returns {number} The total interval in minutes.
|
|
73
|
-
*/
|
|
74
|
-
getMinutesInterval() {
|
|
75
|
-
return (this.minutes +
|
|
76
|
-
this.hours * 60 +
|
|
77
|
-
this.days * 24 * 60 +
|
|
78
|
-
this.weeks * 7 * 24 * 60 +
|
|
79
|
-
this.months * 30 * 24 * 60);
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
exports.Interval = Interval;
|
package/dist/period.d.ts
DELETED
|
@@ -1,185 +0,0 @@
|
|
|
1
|
-
import { Interval } from './interval';
|
|
2
|
-
import { Precision } from './precision';
|
|
3
|
-
export declare class Period {
|
|
4
|
-
private startDate;
|
|
5
|
-
private endDate;
|
|
6
|
-
private precision;
|
|
7
|
-
private interval;
|
|
8
|
-
/**
|
|
9
|
-
* Constructs a new Period instance.
|
|
10
|
-
*
|
|
11
|
-
* @param start - The start date of the period, either as a string or a Date object.
|
|
12
|
-
* @param end - The end date of the period, either as a string or a Date object.
|
|
13
|
-
* @param precision - The precision level of the period, defaulting to Precision.DAY.
|
|
14
|
-
* @param interval - An optional interval associated with the period, defaulting to null.
|
|
15
|
-
*/
|
|
16
|
-
constructor(start: string | Date, end: string | Date, precision?: Precision, interval?: Interval | null);
|
|
17
|
-
/**
|
|
18
|
-
* Retrieves the start date of the period.
|
|
19
|
-
*
|
|
20
|
-
* @returns {Date} The start date as a Date object.
|
|
21
|
-
*/
|
|
22
|
-
getStartDate(): Date;
|
|
23
|
-
/**
|
|
24
|
-
* Retrieves the end date of the period.
|
|
25
|
-
*
|
|
26
|
-
* @returns {Date} The end date as a Date object.
|
|
27
|
-
*/
|
|
28
|
-
getEndDate(): Date;
|
|
29
|
-
/**
|
|
30
|
-
* Checks if a given date is within the period defined by `startDate` and `endDate`.
|
|
31
|
-
*
|
|
32
|
-
* @param date - The date to check, either as a string or a Date object.
|
|
33
|
-
* @returns `true` if the date is within the period, `false` otherwise.
|
|
34
|
-
*/
|
|
35
|
-
contains(date: string | Date): boolean;
|
|
36
|
-
/**
|
|
37
|
-
* Determines if the current period overlaps with another period.
|
|
38
|
-
*
|
|
39
|
-
* @param other - The other period to compare with.
|
|
40
|
-
* @returns `true` if the periods overlap, otherwise `false`.
|
|
41
|
-
*/
|
|
42
|
-
overlapsWith(other: Period): boolean;
|
|
43
|
-
/**
|
|
44
|
-
* Determines if the current period is adjacent to another period.
|
|
45
|
-
*
|
|
46
|
-
* Two periods are considered adjacent if they do not overlap and the gap
|
|
47
|
-
* between them is within one precision unit of the larger precision of the two periods.
|
|
48
|
-
*
|
|
49
|
-
* @param other - The other period to compare with.
|
|
50
|
-
* @returns `true` if the periods are adjacent, `false` otherwise.
|
|
51
|
-
*/
|
|
52
|
-
isAdjacentTo(other: Period): boolean;
|
|
53
|
-
/**
|
|
54
|
-
* Retrieves an array of dates within the specified interval.
|
|
55
|
-
*
|
|
56
|
-
* @returns {Date[] | null} An array of dates if the interval is defined, otherwise `null`.
|
|
57
|
-
*/
|
|
58
|
-
getDatesInInterval(): Date[] | null;
|
|
59
|
-
/**
|
|
60
|
-
* Calculates the number of minutes in the interval between the start and end dates.
|
|
61
|
-
*
|
|
62
|
-
* @returns {number} The number of minutes between the start and end dates.
|
|
63
|
-
*/
|
|
64
|
-
getMinutesInInterval(): number;
|
|
65
|
-
/**
|
|
66
|
-
* Calculates the number of whole hours in the interval.
|
|
67
|
-
*
|
|
68
|
-
* @returns {number} The number of whole hours in the interval.
|
|
69
|
-
*/
|
|
70
|
-
getHoursInInterval(): number;
|
|
71
|
-
/**
|
|
72
|
-
* Calculates the number of whole days in the interval.
|
|
73
|
-
*
|
|
74
|
-
* @returns The number of whole days in the interval.
|
|
75
|
-
*/
|
|
76
|
-
getDaysInInterval(): number;
|
|
77
|
-
/**
|
|
78
|
-
* Calculates the number of whole weeks in the interval.
|
|
79
|
-
*
|
|
80
|
-
* @returns The number of whole weeks in the interval.
|
|
81
|
-
*/
|
|
82
|
-
getWeeksInInterval(): number;
|
|
83
|
-
/**
|
|
84
|
-
* Calculates the number of whole months in the interval between the start and end dates.
|
|
85
|
-
*
|
|
86
|
-
* This method computes the difference in months between the `startDate` and `endDate` properties.
|
|
87
|
-
* It adjusts for month boundaries by decrementing the month count if the end date's day is less than the start date's day.
|
|
88
|
-
*
|
|
89
|
-
* @returns {number} The number of whole months in the interval.
|
|
90
|
-
*/
|
|
91
|
-
getMonthsInInterval(): number;
|
|
92
|
-
/**
|
|
93
|
-
* Calculates the number of whole years in the interval.
|
|
94
|
-
*
|
|
95
|
-
* @returns The number of whole years in the interval.
|
|
96
|
-
*/
|
|
97
|
-
getYearsInInterval(): number;
|
|
98
|
-
/**
|
|
99
|
-
* Calculates the length of the period in the specified precision.
|
|
100
|
-
*
|
|
101
|
-
* @returns {number} The length of the period, rounded down to the nearest whole number.
|
|
102
|
-
*/
|
|
103
|
-
length(): number;
|
|
104
|
-
/**
|
|
105
|
-
* Determines the overlapping period between this period and another period.
|
|
106
|
-
*
|
|
107
|
-
* @param other - The other period to check for overlap with.
|
|
108
|
-
* @returns The overlapping period as a new `Period` instance, or `null` if there is no overlap.
|
|
109
|
-
*/
|
|
110
|
-
overlap(other: Period): Period | null;
|
|
111
|
-
/**
|
|
112
|
-
* Subtracts the given period from the current period and returns the resulting periods.
|
|
113
|
-
* If the periods do not overlap, the current period is returned as a single-element array.
|
|
114
|
-
* If they do overlap, the resulting periods are returned as an array.
|
|
115
|
-
*
|
|
116
|
-
* @param other - The period to subtract from the current period.
|
|
117
|
-
* @returns An array of resulting periods after subtraction.
|
|
118
|
-
*/
|
|
119
|
-
subtract(other: Period): Period[];
|
|
120
|
-
/**
|
|
121
|
-
* Calculates the gap between this period and another period.
|
|
122
|
-
* If the periods overlap or are adjacent, returns null.
|
|
123
|
-
* Otherwise, returns a new Period representing the gap between the two periods.
|
|
124
|
-
*
|
|
125
|
-
* @param other - The other period to compare with.
|
|
126
|
-
* @returns A new Period representing the gap, or null if the periods overlap or are adjacent.
|
|
127
|
-
*/
|
|
128
|
-
gap(other: Period): Period | null;
|
|
129
|
-
/**
|
|
130
|
-
* Computes the symmetric difference between this period and another period.
|
|
131
|
-
* The symmetric difference is defined as the set of periods that are in either
|
|
132
|
-
* of the two periods but not in their intersection.
|
|
133
|
-
*
|
|
134
|
-
* @param other - The other period to compute the symmetric difference with.
|
|
135
|
-
* @returns An array of periods representing the symmetric difference, sorted
|
|
136
|
-
* by start date and merged if adjacent.
|
|
137
|
-
*/
|
|
138
|
-
symmetricDifference(other: Period): Period[];
|
|
139
|
-
private mergeAdjacentPeriods;
|
|
140
|
-
/**
|
|
141
|
-
* Renews the current period by creating a new `Period` instance.
|
|
142
|
-
* The new period starts immediately after the end of the current period
|
|
143
|
-
* and has the same length and precision.
|
|
144
|
-
*
|
|
145
|
-
* @returns {Period} A new `Period` instance with updated start and end dates.
|
|
146
|
-
*/
|
|
147
|
-
renew(): Period;
|
|
148
|
-
/**
|
|
149
|
-
* Computes the union of this period with another period.
|
|
150
|
-
* If the periods overlap or are adjacent, they are merged into a single period.
|
|
151
|
-
* Otherwise, the periods are returned as separate, sorted periods.
|
|
152
|
-
*
|
|
153
|
-
* @param other - The other period to union with this period.
|
|
154
|
-
* @returns An array of periods resulting from the union operation.
|
|
155
|
-
*/
|
|
156
|
-
union(other: Period): Period[];
|
|
157
|
-
/**
|
|
158
|
-
* Sets the start date for the period.
|
|
159
|
-
*
|
|
160
|
-
* @param start - The start date, which can be a string or a Date object.
|
|
161
|
-
* @returns The current instance for method chaining.
|
|
162
|
-
*/
|
|
163
|
-
setStart(start: string | Date): this;
|
|
164
|
-
/**
|
|
165
|
-
* Sets the end date for the period.
|
|
166
|
-
*
|
|
167
|
-
* @param end - The end date as a string or Date object.
|
|
168
|
-
* @returns The current instance for method chaining.
|
|
169
|
-
*/
|
|
170
|
-
setEnd(end: string | Date): this;
|
|
171
|
-
/**
|
|
172
|
-
* Sets the precision for the period.
|
|
173
|
-
*
|
|
174
|
-
* @param precision - The precision to set.
|
|
175
|
-
* @returns The current instance for method chaining.
|
|
176
|
-
*/
|
|
177
|
-
setPrecision(precision: Precision): this;
|
|
178
|
-
/**
|
|
179
|
-
* Sets the interval for the period.
|
|
180
|
-
*
|
|
181
|
-
* @param interval - The interval to be set.
|
|
182
|
-
* @returns The current instance of the period.
|
|
183
|
-
*/
|
|
184
|
-
setInterval(interval: Interval): this;
|
|
185
|
-
}
|
package/dist/period.js
DELETED
|
@@ -1,326 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Period = void 0;
|
|
4
|
-
const precision_1 = require("./precision");
|
|
5
|
-
const utils_1 = require("./utils");
|
|
6
|
-
class Period {
|
|
7
|
-
/**
|
|
8
|
-
* Constructs a new Period instance.
|
|
9
|
-
*
|
|
10
|
-
* @param start - The start date of the period, either as a string or a Date object.
|
|
11
|
-
* @param end - The end date of the period, either as a string or a Date object.
|
|
12
|
-
* @param precision - The precision level of the period, defaulting to Precision.DAY.
|
|
13
|
-
* @param interval - An optional interval associated with the period, defaulting to null.
|
|
14
|
-
*/
|
|
15
|
-
constructor(start, end, precision = precision_1.Precision.DAY, interval = null) {
|
|
16
|
-
this.startDate = new Date(start);
|
|
17
|
-
this.endDate = new Date(end);
|
|
18
|
-
this.precision = precision;
|
|
19
|
-
this.interval = interval;
|
|
20
|
-
}
|
|
21
|
-
/**
|
|
22
|
-
* Retrieves the start date of the period.
|
|
23
|
-
*
|
|
24
|
-
* @returns {Date} The start date as a Date object.
|
|
25
|
-
*/
|
|
26
|
-
getStartDate() {
|
|
27
|
-
return new Date(this.startDate);
|
|
28
|
-
}
|
|
29
|
-
/**
|
|
30
|
-
* Retrieves the end date of the period.
|
|
31
|
-
*
|
|
32
|
-
* @returns {Date} The end date as a Date object.
|
|
33
|
-
*/
|
|
34
|
-
getEndDate() {
|
|
35
|
-
return new Date(this.endDate);
|
|
36
|
-
}
|
|
37
|
-
/**
|
|
38
|
-
* Checks if a given date is within the period defined by `startDate` and `endDate`.
|
|
39
|
-
*
|
|
40
|
-
* @param date - The date to check, either as a string or a Date object.
|
|
41
|
-
* @returns `true` if the date is within the period, `false` otherwise.
|
|
42
|
-
*/
|
|
43
|
-
contains(date) {
|
|
44
|
-
const checkDate = new Date(date);
|
|
45
|
-
return checkDate >= this.startDate && checkDate <= this.endDate;
|
|
46
|
-
}
|
|
47
|
-
/**
|
|
48
|
-
* Determines if the current period overlaps with another period.
|
|
49
|
-
*
|
|
50
|
-
* @param other - The other period to compare with.
|
|
51
|
-
* @returns `true` if the periods overlap, otherwise `false`.
|
|
52
|
-
*/
|
|
53
|
-
overlapsWith(other) {
|
|
54
|
-
return this.startDate < other.endDate && this.endDate > other.startDate;
|
|
55
|
-
}
|
|
56
|
-
/**
|
|
57
|
-
* Determines if the current period is adjacent to another period.
|
|
58
|
-
*
|
|
59
|
-
* Two periods are considered adjacent if they do not overlap and the gap
|
|
60
|
-
* between them is within one precision unit of the larger precision of the two periods.
|
|
61
|
-
*
|
|
62
|
-
* @param other - The other period to compare with.
|
|
63
|
-
* @returns `true` if the periods are adjacent, `false` otherwise.
|
|
64
|
-
*/
|
|
65
|
-
isAdjacentTo(other) {
|
|
66
|
-
const thisPrecisionMs = (0, precision_1.getPrecisionInMilliseconds)(this.precision);
|
|
67
|
-
const otherPrecisionMs = (0, precision_1.getPrecisionInMilliseconds)(other.precision);
|
|
68
|
-
const maxPrecisionMs = Math.max(thisPrecisionMs, otherPrecisionMs);
|
|
69
|
-
const thisEndTime = this.endDate.getTime();
|
|
70
|
-
const otherStartTime = other.startDate.getTime();
|
|
71
|
-
const thisStartTime = this.startDate.getTime();
|
|
72
|
-
const otherEndTime = other.endDate.getTime();
|
|
73
|
-
// Check for overlap
|
|
74
|
-
if (thisStartTime <= otherEndTime && otherStartTime <= thisEndTime) {
|
|
75
|
-
return false;
|
|
76
|
-
}
|
|
77
|
-
// Check if the gap between periods is within one precision unit
|
|
78
|
-
const gap = Math.abs(otherStartTime - thisEndTime);
|
|
79
|
-
const reverseGap = Math.abs(thisStartTime - otherEndTime);
|
|
80
|
-
return ((gap > 0 && gap <= maxPrecisionMs) ||
|
|
81
|
-
(reverseGap > 0 && reverseGap <= maxPrecisionMs));
|
|
82
|
-
}
|
|
83
|
-
/**
|
|
84
|
-
* Retrieves an array of dates within the specified interval.
|
|
85
|
-
*
|
|
86
|
-
* @returns {Date[] | null} An array of dates if the interval is defined, otherwise `null`.
|
|
87
|
-
*/
|
|
88
|
-
getDatesInInterval() {
|
|
89
|
-
if (this.interval) {
|
|
90
|
-
return (0, utils_1.getDatesWithInterval)(this.startDate, this.endDate, this.interval);
|
|
91
|
-
}
|
|
92
|
-
return null;
|
|
93
|
-
}
|
|
94
|
-
/**
|
|
95
|
-
* Calculates the number of minutes in the interval between the start and end dates.
|
|
96
|
-
*
|
|
97
|
-
* @returns {number} The number of minutes between the start and end dates.
|
|
98
|
-
*/
|
|
99
|
-
getMinutesInInterval() {
|
|
100
|
-
return Math.floor((this.endDate.getTime() - this.startDate.getTime()) / (1000 * 60));
|
|
101
|
-
}
|
|
102
|
-
/**
|
|
103
|
-
* Calculates the number of whole hours in the interval.
|
|
104
|
-
*
|
|
105
|
-
* @returns {number} The number of whole hours in the interval.
|
|
106
|
-
*/
|
|
107
|
-
getHoursInInterval() {
|
|
108
|
-
return Math.floor(this.getMinutesInInterval() / 60);
|
|
109
|
-
}
|
|
110
|
-
/**
|
|
111
|
-
* Calculates the number of whole days in the interval.
|
|
112
|
-
*
|
|
113
|
-
* @returns The number of whole days in the interval.
|
|
114
|
-
*/
|
|
115
|
-
getDaysInInterval() {
|
|
116
|
-
return Math.floor(this.getHoursInInterval() / 24);
|
|
117
|
-
}
|
|
118
|
-
/**
|
|
119
|
-
* Calculates the number of whole weeks in the interval.
|
|
120
|
-
*
|
|
121
|
-
* @returns The number of whole weeks in the interval.
|
|
122
|
-
*/
|
|
123
|
-
getWeeksInInterval() {
|
|
124
|
-
return Math.floor(this.getDaysInInterval() / 7);
|
|
125
|
-
}
|
|
126
|
-
/**
|
|
127
|
-
* Calculates the number of whole months in the interval between the start and end dates.
|
|
128
|
-
*
|
|
129
|
-
* This method computes the difference in months between the `startDate` and `endDate` properties.
|
|
130
|
-
* It adjusts for month boundaries by decrementing the month count if the end date's day is less than the start date's day.
|
|
131
|
-
*
|
|
132
|
-
* @returns {number} The number of whole months in the interval.
|
|
133
|
-
*/
|
|
134
|
-
getMonthsInInterval() {
|
|
135
|
-
let months = (this.endDate.getFullYear() - this.startDate.getFullYear()) * 12;
|
|
136
|
-
months += this.endDate.getMonth() - this.startDate.getMonth();
|
|
137
|
-
// Adjust for month boundaries
|
|
138
|
-
if (this.endDate.getDate() < this.startDate.getDate()) {
|
|
139
|
-
months--;
|
|
140
|
-
}
|
|
141
|
-
return months;
|
|
142
|
-
}
|
|
143
|
-
/**
|
|
144
|
-
* Calculates the number of whole years in the interval.
|
|
145
|
-
*
|
|
146
|
-
* @returns The number of whole years in the interval.
|
|
147
|
-
*/
|
|
148
|
-
getYearsInInterval() {
|
|
149
|
-
return Math.floor(this.getMonthsInInterval() / 12);
|
|
150
|
-
}
|
|
151
|
-
/**
|
|
152
|
-
* Calculates the length of the period in the specified precision.
|
|
153
|
-
*
|
|
154
|
-
* @returns {number} The length of the period, rounded down to the nearest whole number.
|
|
155
|
-
*/
|
|
156
|
-
length() {
|
|
157
|
-
return Math.floor((this.endDate.getTime() - this.startDate.getTime()) /
|
|
158
|
-
(0, precision_1.getPrecisionInMilliseconds)(this.precision));
|
|
159
|
-
}
|
|
160
|
-
/**
|
|
161
|
-
* Determines the overlapping period between this period and another period.
|
|
162
|
-
*
|
|
163
|
-
* @param other - The other period to check for overlap with.
|
|
164
|
-
* @returns The overlapping period as a new `Period` instance, or `null` if there is no overlap.
|
|
165
|
-
*/
|
|
166
|
-
overlap(other) {
|
|
167
|
-
if (!this.overlapsWith(other)) {
|
|
168
|
-
return null;
|
|
169
|
-
}
|
|
170
|
-
const start = new Date(Math.max(this.startDate.getTime(), other.startDate.getTime()));
|
|
171
|
-
const end = new Date(Math.min(this.endDate.getTime(), other.endDate.getTime()));
|
|
172
|
-
return new Period(start, end, this.precision);
|
|
173
|
-
}
|
|
174
|
-
/**
|
|
175
|
-
* Subtracts the given period from the current period and returns the resulting periods.
|
|
176
|
-
* If the periods do not overlap, the current period is returned as a single-element array.
|
|
177
|
-
* If they do overlap, the resulting periods are returned as an array.
|
|
178
|
-
*
|
|
179
|
-
* @param other - The period to subtract from the current period.
|
|
180
|
-
* @returns An array of resulting periods after subtraction.
|
|
181
|
-
*/
|
|
182
|
-
subtract(other) {
|
|
183
|
-
if (!this.overlapsWith(other)) {
|
|
184
|
-
return [this];
|
|
185
|
-
}
|
|
186
|
-
const periods = [];
|
|
187
|
-
if (this.startDate < other.startDate) {
|
|
188
|
-
const endDate = new Date(other.startDate.getTime() - 1);
|
|
189
|
-
periods.push(new Period(this.startDate, endDate, this.precision));
|
|
190
|
-
}
|
|
191
|
-
if (this.endDate > other.endDate) {
|
|
192
|
-
const startDate = new Date(other.endDate.getTime() + 1);
|
|
193
|
-
periods.push(new Period(startDate, this.endDate, this.precision));
|
|
194
|
-
}
|
|
195
|
-
return periods;
|
|
196
|
-
}
|
|
197
|
-
/**
|
|
198
|
-
* Calculates the gap between this period and another period.
|
|
199
|
-
* If the periods overlap or are adjacent, returns null.
|
|
200
|
-
* Otherwise, returns a new Period representing the gap between the two periods.
|
|
201
|
-
*
|
|
202
|
-
* @param other - The other period to compare with.
|
|
203
|
-
* @returns A new Period representing the gap, or null if the periods overlap or are adjacent.
|
|
204
|
-
*/
|
|
205
|
-
gap(other) {
|
|
206
|
-
if (this.overlapsWith(other) || this.isAdjacentTo(other)) {
|
|
207
|
-
return null;
|
|
208
|
-
}
|
|
209
|
-
const precisionMs = (0, precision_1.getPrecisionInMilliseconds)(this.precision);
|
|
210
|
-
const start = this.endDate < other.startDate ? this.endDate : other.endDate;
|
|
211
|
-
const end = this.startDate > other.startDate ? this.startDate : other.startDate;
|
|
212
|
-
return new Period(new Date(start.getTime() + precisionMs), new Date(end.getTime() - precisionMs), this.precision);
|
|
213
|
-
}
|
|
214
|
-
/**
|
|
215
|
-
* Computes the symmetric difference between this period and another period.
|
|
216
|
-
* The symmetric difference is defined as the set of periods that are in either
|
|
217
|
-
* of the two periods but not in their intersection.
|
|
218
|
-
*
|
|
219
|
-
* @param other - The other period to compute the symmetric difference with.
|
|
220
|
-
* @returns An array of periods representing the symmetric difference, sorted
|
|
221
|
-
* by start date and merged if adjacent.
|
|
222
|
-
*/
|
|
223
|
-
symmetricDifference(other) {
|
|
224
|
-
const periods = [];
|
|
225
|
-
const overlapPeriod = this.overlap(other);
|
|
226
|
-
if (!overlapPeriod) {
|
|
227
|
-
// No overlap, return both periods
|
|
228
|
-
return [this, other].sort((a, b) => a.startDate.getTime() - b.startDate.getTime());
|
|
229
|
-
}
|
|
230
|
-
const thisSubtracted = this.subtract(overlapPeriod);
|
|
231
|
-
const otherSubtracted = other.subtract(overlapPeriod);
|
|
232
|
-
periods.push(...thisSubtracted, ...otherSubtracted);
|
|
233
|
-
// Sort periods and merge adjacent ones
|
|
234
|
-
return this.mergeAdjacentPeriods(periods);
|
|
235
|
-
}
|
|
236
|
-
mergeAdjacentPeriods(periods) {
|
|
237
|
-
if (periods.length <= 1)
|
|
238
|
-
return periods;
|
|
239
|
-
periods.sort((a, b) => a.startDate.getTime() - b.startDate.getTime());
|
|
240
|
-
const merged = [periods[0]];
|
|
241
|
-
for (let i = 1; i < periods.length; i++) {
|
|
242
|
-
const current = periods[i];
|
|
243
|
-
const previous = merged[merged.length - 1];
|
|
244
|
-
if (previous.isAdjacentTo(current) || previous.overlapsWith(current)) {
|
|
245
|
-
merged[merged.length - 1] = new Period(previous.startDate, current.endDate > previous.endDate
|
|
246
|
-
? current.endDate
|
|
247
|
-
: previous.endDate, this.precision);
|
|
248
|
-
}
|
|
249
|
-
else {
|
|
250
|
-
merged.push(current);
|
|
251
|
-
}
|
|
252
|
-
}
|
|
253
|
-
return merged;
|
|
254
|
-
}
|
|
255
|
-
/**
|
|
256
|
-
* Renews the current period by creating a new `Period` instance.
|
|
257
|
-
* The new period starts immediately after the end of the current period
|
|
258
|
-
* and has the same length and precision.
|
|
259
|
-
*
|
|
260
|
-
* @returns {Period} A new `Period` instance with updated start and end dates.
|
|
261
|
-
*/
|
|
262
|
-
renew() {
|
|
263
|
-
const length = this.length();
|
|
264
|
-
const newStart = new Date(this.endDate.getTime() + (0, precision_1.getPrecisionInMilliseconds)(this.precision));
|
|
265
|
-
const newEnd = new Date(newStart.getTime() + length * (0, precision_1.getPrecisionInMilliseconds)(this.precision));
|
|
266
|
-
return new Period(newStart, newEnd, this.precision, this.interval);
|
|
267
|
-
}
|
|
268
|
-
/**
|
|
269
|
-
* Computes the union of this period with another period.
|
|
270
|
-
* If the periods overlap or are adjacent, they are merged into a single period.
|
|
271
|
-
* Otherwise, the periods are returned as separate, sorted periods.
|
|
272
|
-
*
|
|
273
|
-
* @param other - The other period to union with this period.
|
|
274
|
-
* @returns An array of periods resulting from the union operation.
|
|
275
|
-
*/
|
|
276
|
-
union(other) {
|
|
277
|
-
if (this.overlapsWith(other) || this.isAdjacentTo(other)) {
|
|
278
|
-
const start = new Date(Math.min(this.startDate.getTime(), other.startDate.getTime()));
|
|
279
|
-
const end = new Date(Math.max(this.endDate.getTime(), other.endDate.getTime()));
|
|
280
|
-
return [new Period(start, end, this.precision)];
|
|
281
|
-
}
|
|
282
|
-
return [this, other].sort((a, b) => a.startDate.getTime() - b.startDate.getTime());
|
|
283
|
-
}
|
|
284
|
-
// Fluent API methods
|
|
285
|
-
/**
|
|
286
|
-
* Sets the start date for the period.
|
|
287
|
-
*
|
|
288
|
-
* @param start - The start date, which can be a string or a Date object.
|
|
289
|
-
* @returns The current instance for method chaining.
|
|
290
|
-
*/
|
|
291
|
-
setStart(start) {
|
|
292
|
-
this.startDate = new Date(start);
|
|
293
|
-
return this;
|
|
294
|
-
}
|
|
295
|
-
/**
|
|
296
|
-
* Sets the end date for the period.
|
|
297
|
-
*
|
|
298
|
-
* @param end - The end date as a string or Date object.
|
|
299
|
-
* @returns The current instance for method chaining.
|
|
300
|
-
*/
|
|
301
|
-
setEnd(end) {
|
|
302
|
-
this.endDate = new Date(end);
|
|
303
|
-
return this;
|
|
304
|
-
}
|
|
305
|
-
/**
|
|
306
|
-
* Sets the precision for the period.
|
|
307
|
-
*
|
|
308
|
-
* @param precision - The precision to set.
|
|
309
|
-
* @returns The current instance for method chaining.
|
|
310
|
-
*/
|
|
311
|
-
setPrecision(precision) {
|
|
312
|
-
this.precision = precision;
|
|
313
|
-
return this;
|
|
314
|
-
}
|
|
315
|
-
/**
|
|
316
|
-
* Sets the interval for the period.
|
|
317
|
-
*
|
|
318
|
-
* @param interval - The interval to be set.
|
|
319
|
-
* @returns The current instance of the period.
|
|
320
|
-
*/
|
|
321
|
-
setInterval(interval) {
|
|
322
|
-
this.interval = interval;
|
|
323
|
-
return this;
|
|
324
|
-
}
|
|
325
|
-
}
|
|
326
|
-
exports.Period = Period;
|
package/dist/precision.d.ts
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
export declare enum Precision {
|
|
2
|
-
MINUTE = "minute",
|
|
3
|
-
HOUR = "hour",
|
|
4
|
-
DAY = "day",
|
|
5
|
-
WEEK = "week",
|
|
6
|
-
MONTH = "month",
|
|
7
|
-
YEAR = "year"
|
|
8
|
-
}
|
|
9
|
-
/**
|
|
10
|
-
* Converts a given precision to its equivalent duration in milliseconds.
|
|
11
|
-
*
|
|
12
|
-
* @param precision - The precision to convert. It can be one of the following:
|
|
13
|
-
* - `Precision.MINUTE`: 1 minute in milliseconds.
|
|
14
|
-
* - `Precision.HOUR`: 1 hour in milliseconds.
|
|
15
|
-
* - `Precision.DAY`: 1 day in milliseconds.
|
|
16
|
-
* - `Precision.WEEK`: 1 week in milliseconds.
|
|
17
|
-
* - `Precision.MONTH`: 1 month (approximated as 30 days) in milliseconds.
|
|
18
|
-
* - `Precision.YEAR`: 1 year (approximated as 365 days) in milliseconds.
|
|
19
|
-
*
|
|
20
|
-
* @returns The duration in milliseconds corresponding to the given precision.
|
|
21
|
-
*
|
|
22
|
-
* @throws Will throw an error if the provided precision is not supported.
|
|
23
|
-
*/
|
|
24
|
-
export declare function getPrecisionInMilliseconds(precision: Precision): number;
|