@xh/hoist 79.0.0-SNAPSHOT.1766259546947 → 79.0.0-SNAPSHOT.1766260837143
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.
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
*
|
|
5
5
|
* Copyright © 2025 Extremely Heavy Industries Inc.
|
|
6
6
|
*/
|
|
7
|
-
import {XH} from '@xh/hoist/core';
|
|
7
|
+
import {LocalDateUnit, XH} from '@xh/hoist/core';
|
|
8
8
|
import {computeOnce, throwIf} from '@xh/hoist/utils/js';
|
|
9
9
|
import {isNil, isString} from 'lodash';
|
|
10
10
|
import moment, {Moment, MomentInput} from 'moment';
|
|
@@ -18,16 +18,26 @@ import moment, {Moment, MomentInput} from 'moment';
|
|
|
18
18
|
* For efficiency and to enable strict equality checks, instances of this class are memoized:
|
|
19
19
|
* only a single version of the object will be created and returned for each calendar day,
|
|
20
20
|
* as long as the caller uses one of the *public factory methods*, which they always should!
|
|
21
|
-
*
|
|
22
|
-
* Unit accepted by manipulation methods are ['year', 'quarter', 'month', 'week', 'day', 'date'].
|
|
23
21
|
*/
|
|
24
22
|
export class LocalDate {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
23
|
+
static readonly VALID_UNITS: Set<LocalDateUnit> = new Set([
|
|
24
|
+
'year',
|
|
25
|
+
'years',
|
|
26
|
+
'quarter',
|
|
27
|
+
'quarters',
|
|
28
|
+
'month',
|
|
29
|
+
'months',
|
|
30
|
+
'week',
|
|
31
|
+
'weeks',
|
|
32
|
+
'day',
|
|
33
|
+
'days'
|
|
34
|
+
]);
|
|
35
|
+
|
|
36
|
+
private static _instances: Map<string, LocalDate> = new Map();
|
|
37
|
+
|
|
38
|
+
private readonly _isoString: string;
|
|
39
|
+
private readonly _moment: Moment;
|
|
40
|
+
private readonly _date: Date;
|
|
31
41
|
|
|
32
42
|
//------------------------
|
|
33
43
|
// Factories
|
|
@@ -187,17 +197,17 @@ export class LocalDate {
|
|
|
187
197
|
//--------------------------
|
|
188
198
|
// Manipulate/Calendar logic
|
|
189
199
|
//--------------------------
|
|
190
|
-
add(value, unit = 'days'): LocalDate {
|
|
200
|
+
add(value: number, unit: LocalDateUnit = 'days'): LocalDate {
|
|
191
201
|
this.ensureUnitValid(unit);
|
|
192
202
|
return LocalDate.from(this.moment.add(value, unit));
|
|
193
203
|
}
|
|
194
204
|
|
|
195
|
-
subtract(value, unit = 'days'): LocalDate {
|
|
205
|
+
subtract(value: number, unit: LocalDateUnit = 'days'): LocalDate {
|
|
196
206
|
this.ensureUnitValid(unit);
|
|
197
207
|
return LocalDate.from(this.moment.subtract(value, unit));
|
|
198
208
|
}
|
|
199
209
|
|
|
200
|
-
addWeekdays(value): LocalDate {
|
|
210
|
+
addWeekdays(value: number): LocalDate {
|
|
201
211
|
if (value < 0) {
|
|
202
212
|
return this.subtractWeekdays(Math.abs(value));
|
|
203
213
|
}
|
|
@@ -211,7 +221,7 @@ export class LocalDate {
|
|
|
211
221
|
return ret;
|
|
212
222
|
}
|
|
213
223
|
|
|
214
|
-
subtractWeekdays(value): LocalDate {
|
|
224
|
+
subtractWeekdays(value: number): LocalDate {
|
|
215
225
|
if (value < 0) {
|
|
216
226
|
return this.addWeekdays(Math.abs(value));
|
|
217
227
|
}
|
|
@@ -225,7 +235,7 @@ export class LocalDate {
|
|
|
225
235
|
return ret;
|
|
226
236
|
}
|
|
227
237
|
|
|
228
|
-
startOf(unit): LocalDate {
|
|
238
|
+
startOf(unit: LocalDateUnit): LocalDate {
|
|
229
239
|
this.ensureUnitValid(unit);
|
|
230
240
|
return LocalDate.from(this.moment.startOf(unit));
|
|
231
241
|
}
|
|
@@ -309,7 +319,7 @@ export class LocalDate {
|
|
|
309
319
|
return this.isWeekday ? this : this.previousWeekday();
|
|
310
320
|
}
|
|
311
321
|
|
|
312
|
-
diff(other: LocalDate, unit:
|
|
322
|
+
diff(other: LocalDate, unit: LocalDateUnit = 'days'): number {
|
|
313
323
|
this.ensureUnitValid(unit);
|
|
314
324
|
return this._moment.diff(other._moment, unit);
|
|
315
325
|
}
|
|
@@ -329,20 +339,12 @@ export class LocalDate {
|
|
|
329
339
|
this._date = m.toDate();
|
|
330
340
|
}
|
|
331
341
|
|
|
332
|
-
private ensureUnitValid(unit) {
|
|
333
|
-
|
|
334
|
-
unit = moment.normalizeUnits(unit);
|
|
335
|
-
throwIf(
|
|
336
|
-
!LocalDate.VALID_UNITS.includes(unit),
|
|
337
|
-
`Invalid unit for LocalDate adjustment: ${unit}`
|
|
338
|
-
);
|
|
342
|
+
private ensureUnitValid(unit: LocalDateUnit) {
|
|
343
|
+
throwIf(!LocalDate.VALID_UNITS.has(unit), `Invalid unit for LocalDate adjustment: ${unit}`);
|
|
339
344
|
}
|
|
340
345
|
}
|
|
341
346
|
|
|
342
|
-
/**
|
|
343
|
-
* Is the input value a local Date?
|
|
344
|
-
* Convenience alias for LocalDate.isLocalDate()
|
|
345
|
-
*/
|
|
347
|
+
/** @returns true if the input value is a `LocalDate` instance. */
|
|
346
348
|
export function isLocalDate(val: any): val is LocalDate {
|
|
347
349
|
return !!val?.isLocalDate;
|
|
348
350
|
}
|