@naturalcycles/js-lib 15.8.1 → 15.9.1
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/dist/array/array.util.d.ts +14 -6
- package/dist/array/array.util.js +57 -36
- package/dist/datetime/dateInterval.d.ts +4 -5
- package/dist/datetime/dateInterval.js +8 -11
- package/dist/datetime/localDate.d.ts +9 -8
- package/dist/datetime/localDate.js +15 -17
- package/dist/datetime/localTime.d.ts +9 -8
- package/dist/datetime/localTime.js +35 -36
- package/dist/datetime/timeInterval.d.ts +2 -2
- package/dist/datetime/timeInterval.js +3 -4
- package/dist/http/fetcher.js +1 -1
- package/dist/json-schema/jsonSchema.util.js +1 -1
- package/dist/log/commonLogger.d.ts +2 -1
- package/dist/log/commonLogger.js +2 -3
- package/dist/math/math.util.js +0 -1
- package/dist/number/number.util.d.ts +4 -3
- package/dist/number/number.util.js +9 -14
- package/dist/object/object.util.d.ts +14 -13
- package/dist/object/object.util.js +26 -26
- package/dist/semver.d.ts +2 -2
- package/dist/semver.js +3 -3
- package/dist/types.d.ts +7 -5
- package/dist/types.js +1 -1
- package/package.json +1 -1
- package/src/array/array.util.ts +73 -45
- package/src/datetime/dateInterval.ts +9 -12
- package/src/datetime/localDate.ts +17 -18
- package/src/datetime/localTime.ts +36 -36
- package/src/datetime/timeInterval.ts +4 -5
- package/src/http/fetcher.ts +1 -1
- package/src/json-schema/jsonSchema.util.ts +1 -1
- package/src/log/commonLogger.ts +4 -2
- package/src/math/math.util.ts +0 -1
- package/src/number/number.util.ts +9 -19
- package/src/object/object.util.ts +30 -26
- package/src/semver.ts +4 -4
- package/src/types.ts +11 -6
|
@@ -90,6 +90,18 @@ export declare function _mapBy<ITEM, KEY>(items: readonly ITEM[], mapper: Mapper
|
|
|
90
90
|
* Returning `undefined` from the Mapper will EXCLUDE the item.
|
|
91
91
|
*/
|
|
92
92
|
export declare function _groupBy<T>(items: readonly T[], mapper: Mapper<T, any>): StringMap<T[]>;
|
|
93
|
+
export interface MutateOptions {
|
|
94
|
+
/**
|
|
95
|
+
* Defaults to false.
|
|
96
|
+
*/
|
|
97
|
+
mutate?: boolean;
|
|
98
|
+
}
|
|
99
|
+
export interface SortOptions extends MutateOptions {
|
|
100
|
+
/**
|
|
101
|
+
* Defaults to 'asc'.
|
|
102
|
+
*/
|
|
103
|
+
dir?: SortDirection;
|
|
104
|
+
}
|
|
93
105
|
/**
|
|
94
106
|
* _sortBy([{age: 20}, {age: 10}], 'age')
|
|
95
107
|
* // => [{age: 10}, {age: 20}]
|
|
@@ -97,11 +109,7 @@ export declare function _groupBy<T>(items: readonly T[], mapper: Mapper<T, any>)
|
|
|
97
109
|
* Same:
|
|
98
110
|
* _sortBy([{age: 20}, {age: 10}], o => o.age)
|
|
99
111
|
*/
|
|
100
|
-
export declare function _sortBy<T>(items: T[], mapper: Mapper<T,
|
|
101
|
-
/**
|
|
102
|
-
* Alias for _sortBy with descending order.
|
|
103
|
-
*/
|
|
104
|
-
export declare function _sortDescBy<T>(items: T[], mapper: Mapper<T, any>, mutate?: boolean): T[];
|
|
112
|
+
export declare function _sortBy<T, COMPARE_TYPE extends string | number>(items: T[], mapper: Mapper<T, COMPARE_TYPE>, opt?: SortOptions): T[];
|
|
105
113
|
/**
|
|
106
114
|
* Similar to `Array.find`, but the `predicate` may return `END` to stop the iteration early.
|
|
107
115
|
*
|
|
@@ -194,7 +202,7 @@ export declare function _mapToObject<T, V>(array: Iterable<T>, mapper: (item: T)
|
|
|
194
202
|
* Fisher–Yates algorithm.
|
|
195
203
|
* Based on: https://stackoverflow.com/a/12646864/4919972
|
|
196
204
|
*/
|
|
197
|
-
export declare function _shuffle<T>(array: T[],
|
|
205
|
+
export declare function _shuffle<T>(array: T[], opt?: MutateOptions): T[];
|
|
198
206
|
/**
|
|
199
207
|
* Returns last item of non-empty array.
|
|
200
208
|
* Throws if array is empty.
|
package/dist/array/array.util.js
CHANGED
|
@@ -50,9 +50,9 @@ export function _pushUniq(a, ...items) {
|
|
|
50
50
|
* Mutates the array (same as normal `push`).
|
|
51
51
|
*/
|
|
52
52
|
export function _pushUniqBy(a, mapper, ...items) {
|
|
53
|
-
const mappedSet = new Set(a.map(
|
|
54
|
-
items.forEach(
|
|
55
|
-
const mapped = mapper(item
|
|
53
|
+
const mappedSet = new Set(a.map(mapper));
|
|
54
|
+
items.forEach(item => {
|
|
55
|
+
const mapped = mapper(item);
|
|
56
56
|
if (!mappedSet.has(mapped)) {
|
|
57
57
|
a.push(item);
|
|
58
58
|
mappedSet.add(mapped);
|
|
@@ -79,8 +79,9 @@ export function _pushUniqBy(a, mapper, ...items) {
|
|
|
79
79
|
*/
|
|
80
80
|
export function _uniqBy(arr, mapper) {
|
|
81
81
|
const map = new Map();
|
|
82
|
-
for (
|
|
83
|
-
const
|
|
82
|
+
for (let i = 0; i < arr.length; i++) {
|
|
83
|
+
const item = arr[i];
|
|
84
|
+
const key = item === undefined || item === null ? item : mapper(item);
|
|
84
85
|
if (!map.has(key))
|
|
85
86
|
map.set(key, item);
|
|
86
87
|
}
|
|
@@ -107,13 +108,29 @@ export function _uniqBy(arr, mapper) {
|
|
|
107
108
|
* Returning `undefined` from the Mapper will EXCLUDE the item.
|
|
108
109
|
*/
|
|
109
110
|
export function _by(items, mapper) {
|
|
110
|
-
|
|
111
|
+
const map = {};
|
|
112
|
+
for (let i = 0; i < items.length; i++) {
|
|
113
|
+
const v = items[i];
|
|
114
|
+
const k = mapper(v);
|
|
115
|
+
if (k !== undefined) {
|
|
116
|
+
map[k] = v;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return map;
|
|
111
120
|
}
|
|
112
121
|
/**
|
|
113
122
|
* Map an array of items by a key, that is calculated by a Mapper.
|
|
114
123
|
*/
|
|
115
124
|
export function _mapBy(items, mapper) {
|
|
116
|
-
|
|
125
|
+
const map = new Map();
|
|
126
|
+
for (let i = 0; i < items.length; i++) {
|
|
127
|
+
const item = items[i];
|
|
128
|
+
const key = mapper(item);
|
|
129
|
+
if (key !== undefined) {
|
|
130
|
+
map.set(key, item);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
return map;
|
|
117
134
|
}
|
|
118
135
|
/**
|
|
119
136
|
* const a = [1, 2, 3, 4, 5]
|
|
@@ -128,11 +145,13 @@ export function _mapBy(items, mapper) {
|
|
|
128
145
|
*/
|
|
129
146
|
export function _groupBy(items, mapper) {
|
|
130
147
|
const map = {};
|
|
131
|
-
for (
|
|
132
|
-
const
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
148
|
+
for (let i = 0; i < items.length; i++) {
|
|
149
|
+
const item = items[i];
|
|
150
|
+
const key = mapper(item);
|
|
151
|
+
if (key !== undefined) {
|
|
152
|
+
;
|
|
153
|
+
(map[key] ||= []).push(item);
|
|
154
|
+
}
|
|
136
155
|
}
|
|
137
156
|
return map;
|
|
138
157
|
}
|
|
@@ -143,28 +162,30 @@ export function _groupBy(items, mapper) {
|
|
|
143
162
|
* Same:
|
|
144
163
|
* _sortBy([{age: 20}, {age: 10}], o => o.age)
|
|
145
164
|
*/
|
|
146
|
-
export function _sortBy(items, mapper,
|
|
147
|
-
const mod = dir === 'desc' ? -1 : 1;
|
|
148
|
-
return (mutate ? items : [...items]).sort((_a, _b) => {
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
165
|
+
export function _sortBy(items, mapper, opt = {}) {
|
|
166
|
+
const mod = opt.dir === 'desc' ? -1 : 1;
|
|
167
|
+
return (opt.mutate ? items : [...items]).sort((_a, _b) => {
|
|
168
|
+
// This implementation may call mapper more than once per item,
|
|
169
|
+
// but the benchmarks show no significant difference in performance.
|
|
170
|
+
const a = mapper(_a);
|
|
171
|
+
const b = mapper(_b);
|
|
172
|
+
// if (typeof a === 'number' && typeof b === 'number') return (a - b) * mod
|
|
173
|
+
// return String(a).localeCompare(String(b)) * mod
|
|
174
|
+
if (a > b)
|
|
175
|
+
return mod;
|
|
176
|
+
if (a < b)
|
|
177
|
+
return -mod;
|
|
178
|
+
return 0;
|
|
153
179
|
});
|
|
154
180
|
}
|
|
155
|
-
/**
|
|
156
|
-
* Alias for _sortBy with descending order.
|
|
157
|
-
*/
|
|
158
|
-
export function _sortDescBy(items, mapper, mutate = false) {
|
|
159
|
-
return _sortBy(items, mapper, mutate, 'desc');
|
|
160
|
-
}
|
|
161
181
|
/**
|
|
162
182
|
* Similar to `Array.find`, but the `predicate` may return `END` to stop the iteration early.
|
|
163
183
|
*
|
|
164
184
|
* Use `Array.find` if you don't need to stop the iteration early.
|
|
165
185
|
*/
|
|
166
186
|
export function _find(items, predicate) {
|
|
167
|
-
for (
|
|
187
|
+
for (let i = 0; i < items.length; i++) {
|
|
188
|
+
const item = items[i];
|
|
168
189
|
const result = predicate(item, i);
|
|
169
190
|
if (result === END)
|
|
170
191
|
return;
|
|
@@ -241,9 +262,8 @@ export function _count(items, predicate, limit) {
|
|
|
241
262
|
}
|
|
242
263
|
export function _countBy(items, mapper) {
|
|
243
264
|
const map = {};
|
|
244
|
-
let i = 0;
|
|
245
265
|
for (const item of items) {
|
|
246
|
-
const key = mapper(item
|
|
266
|
+
const key = mapper(item);
|
|
247
267
|
map[key] = (map[key] || 0) + 1;
|
|
248
268
|
}
|
|
249
269
|
return map;
|
|
@@ -301,9 +321,8 @@ export function _sum(items) {
|
|
|
301
321
|
}
|
|
302
322
|
export function _sumBy(items, mapper) {
|
|
303
323
|
let sum = 0;
|
|
304
|
-
let i = 0;
|
|
305
324
|
for (const n of items) {
|
|
306
|
-
const v = mapper(n
|
|
325
|
+
const v = mapper(n);
|
|
307
326
|
if (typeof v === 'number') {
|
|
308
327
|
// count only numbers, nothing else
|
|
309
328
|
sum = (sum + v);
|
|
@@ -339,8 +358,8 @@ export function _mapToObject(array, mapper) {
|
|
|
339
358
|
* Fisher–Yates algorithm.
|
|
340
359
|
* Based on: https://stackoverflow.com/a/12646864/4919972
|
|
341
360
|
*/
|
|
342
|
-
export function _shuffle(array,
|
|
343
|
-
const a = mutate ? array : [...array];
|
|
361
|
+
export function _shuffle(array, opt = {}) {
|
|
362
|
+
const a = opt.mutate ? array : [...array];
|
|
344
363
|
for (let i = a.length - 1; i > 0; i--) {
|
|
345
364
|
const j = Math.floor(Math.random() * (i + 1));
|
|
346
365
|
[a[i], a[j]] = [a[j], a[i]];
|
|
@@ -425,8 +444,9 @@ export function _maxByOrUndefined(array, mapper) {
|
|
|
425
444
|
return;
|
|
426
445
|
let maxItem;
|
|
427
446
|
let max;
|
|
428
|
-
for (
|
|
429
|
-
const
|
|
447
|
+
for (let i = 0; i < array.length; i++) {
|
|
448
|
+
const item = array[i];
|
|
449
|
+
const v = mapper(item);
|
|
430
450
|
if (v !== undefined && (max === undefined || v > max)) {
|
|
431
451
|
maxItem = item;
|
|
432
452
|
max = v;
|
|
@@ -439,8 +459,9 @@ export function _minByOrUndefined(array, mapper) {
|
|
|
439
459
|
return;
|
|
440
460
|
let minItem;
|
|
441
461
|
let min;
|
|
442
|
-
for (
|
|
443
|
-
const
|
|
462
|
+
for (let i = 0; i < array.length; i++) {
|
|
463
|
+
const item = array[i];
|
|
464
|
+
const v = mapper(item);
|
|
444
465
|
if (v !== undefined && (min === undefined || v < min)) {
|
|
445
466
|
minItem = item;
|
|
446
467
|
min = v;
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import type { Inclusiveness } from '../types.js';
|
|
2
1
|
import type { LocalDate, LocalDateInput, LocalDateUnit } from './localDate.js';
|
|
3
2
|
export type DateIntervalConfig = DateInterval | DateIntervalString;
|
|
4
3
|
export type DateIntervalString = string;
|
|
@@ -24,18 +23,18 @@ export declare class DateInterval {
|
|
|
24
23
|
/**
|
|
25
24
|
* Ranges of DateInterval (start, end) are INCLUSIVE.
|
|
26
25
|
*/
|
|
27
|
-
includes(d: LocalDateInput
|
|
28
|
-
intersects(int: DateIntervalConfig
|
|
26
|
+
includes(d: LocalDateInput): boolean;
|
|
27
|
+
intersects(int: DateIntervalConfig): boolean;
|
|
29
28
|
/**
|
|
30
29
|
* DateIntervals compare by start date.
|
|
31
30
|
* If it's the same - then by end date.
|
|
32
31
|
*/
|
|
33
32
|
cmp(d: DateIntervalConfig): -1 | 0 | 1;
|
|
34
|
-
getDays(
|
|
33
|
+
getDays(): LocalDate[];
|
|
35
34
|
/**
|
|
36
35
|
* Returns an array of LocalDates that are included in the interval.
|
|
37
36
|
*/
|
|
38
|
-
range(
|
|
37
|
+
range(step?: number, stepUnit?: LocalDateUnit): LocalDate[];
|
|
39
38
|
toString(): DateIntervalString;
|
|
40
39
|
toJSON(): DateIntervalString;
|
|
41
40
|
}
|
|
@@ -46,15 +46,12 @@ export class DateInterval {
|
|
|
46
46
|
/**
|
|
47
47
|
* Ranges of DateInterval (start, end) are INCLUSIVE.
|
|
48
48
|
*/
|
|
49
|
-
includes(d
|
|
50
|
-
|
|
51
|
-
// eslint-disable-next-line @typescript-eslint/prefer-string-starts-ends-with
|
|
52
|
-
return d.isAfter(this.start, incl[0] === '[') && d.isBefore(this.end, incl[1] === ']');
|
|
49
|
+
includes(d) {
|
|
50
|
+
return localDate(d).isBetween(this.start, this.end, '[]');
|
|
53
51
|
}
|
|
54
|
-
intersects(int
|
|
52
|
+
intersects(int) {
|
|
55
53
|
const $int = DateInterval.parse(int);
|
|
56
|
-
|
|
57
|
-
return this.includes($int.start, incl) || this.includes($int.end, incl);
|
|
54
|
+
return this.includes($int.start) || this.includes($int.end);
|
|
58
55
|
}
|
|
59
56
|
/**
|
|
60
57
|
* DateIntervals compare by start date.
|
|
@@ -64,14 +61,14 @@ export class DateInterval {
|
|
|
64
61
|
d = DateInterval.parse(d);
|
|
65
62
|
return this.start.compare(d.start) || this.end.compare(d.end);
|
|
66
63
|
}
|
|
67
|
-
getDays(
|
|
68
|
-
return localDate.range(this.start, this.end,
|
|
64
|
+
getDays() {
|
|
65
|
+
return localDate.range(this.start, this.end, '[]', 1, 'day');
|
|
69
66
|
}
|
|
70
67
|
/**
|
|
71
68
|
* Returns an array of LocalDates that are included in the interval.
|
|
72
69
|
*/
|
|
73
|
-
range(
|
|
74
|
-
return localDate.range(this.start, this.end,
|
|
70
|
+
range(step = 1, stepUnit = 'day') {
|
|
71
|
+
return localDate.range(this.start, this.end, '[]', step, stepUnit);
|
|
75
72
|
}
|
|
76
73
|
toString() {
|
|
77
74
|
return [this.start, this.end].join('/');
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
import type { MutateOptions, SortOptions } from '../array/array.util.js';
|
|
1
2
|
import { Iterable2 } from '../iter/iterable2.js';
|
|
2
|
-
import type { Inclusiveness, IsoDate, IsoDateTime, MonthId,
|
|
3
|
+
import type { Inclusiveness, IsoDate, IsoDateTime, MonthId, UnixTimestamp, UnixTimestampMillis } from '../types.js';
|
|
3
4
|
import type { DateObject, ISODayOfWeek, LocalTime } from './localTime.js';
|
|
4
5
|
export type LocalDateUnit = LocalDateUnitStrict | 'week';
|
|
5
6
|
export type LocalDateUnitStrict = 'year' | 'month' | 'day';
|
|
@@ -16,7 +17,7 @@ export declare class LocalDate {
|
|
|
16
17
|
day: number;
|
|
17
18
|
constructor(year: number, month: number, day: number);
|
|
18
19
|
get(unit: LocalDateUnitStrict): number;
|
|
19
|
-
set(unit: LocalDateUnitStrict, v: number,
|
|
20
|
+
set(unit: LocalDateUnitStrict, v: number, opt?: MutateOptions): LocalDate;
|
|
20
21
|
setYear(v: number): LocalDate;
|
|
21
22
|
setMonth(v: number): LocalDate;
|
|
22
23
|
setDay(v: number): LocalDate;
|
|
@@ -38,7 +39,7 @@ export declare class LocalDate {
|
|
|
38
39
|
isSameOrBefore(d: LocalDateInput): boolean;
|
|
39
40
|
isAfter(d: LocalDateInput, inclusive?: boolean): boolean;
|
|
40
41
|
isSameOrAfter(d: LocalDateInput): boolean;
|
|
41
|
-
isBetween(min: LocalDateInput, max: LocalDateInput, incl
|
|
42
|
+
isBetween(min: LocalDateInput, max: LocalDateInput, incl: Inclusiveness): boolean;
|
|
42
43
|
/**
|
|
43
44
|
* Checks if this localDate is older (<) than "today" by X units.
|
|
44
45
|
*
|
|
@@ -100,8 +101,8 @@ export declare class LocalDate {
|
|
|
100
101
|
minusWeeks(num: number): LocalDate;
|
|
101
102
|
minusMonths(num: number): LocalDate;
|
|
102
103
|
minusYears(num: number): LocalDate;
|
|
103
|
-
plus(num: number, unit: LocalDateUnit,
|
|
104
|
-
minus(num: number, unit: LocalDateUnit,
|
|
104
|
+
plus(num: number, unit: LocalDateUnit, opt?: MutateOptions): LocalDate;
|
|
105
|
+
minus(num: number, unit: LocalDateUnit, opt?: MutateOptions): LocalDate;
|
|
105
106
|
startOf(unit: LocalDateUnitStrict): LocalDate;
|
|
106
107
|
endOf(unit: LocalDateUnitStrict): LocalDate;
|
|
107
108
|
/**
|
|
@@ -246,7 +247,7 @@ declare class LocalDateFactory {
|
|
|
246
247
|
/**
|
|
247
248
|
* Sorts an array of LocalDates in `dir` order (ascending by default).
|
|
248
249
|
*/
|
|
249
|
-
sort(items: LocalDate[],
|
|
250
|
+
sort(items: LocalDate[], opt?: SortOptions): LocalDate[];
|
|
250
251
|
/**
|
|
251
252
|
* Returns the earliest (min) LocalDate from the array, or undefined if the array is empty.
|
|
252
253
|
*/
|
|
@@ -269,12 +270,12 @@ declare class LocalDateFactory {
|
|
|
269
270
|
* Returns the range (array) of LocalDates between min and max.
|
|
270
271
|
* By default, min is included, max is excluded.
|
|
271
272
|
*/
|
|
272
|
-
range(min: LocalDateInput, max: LocalDateInput, incl
|
|
273
|
+
range(min: LocalDateInput, max: LocalDateInput, incl: Inclusiveness, step?: number, stepUnit?: LocalDateUnit): LocalDate[];
|
|
273
274
|
/**
|
|
274
275
|
* Returns the Iterable2 of LocalDates between min and max.
|
|
275
276
|
* By default, min is included, max is excluded.
|
|
276
277
|
*/
|
|
277
|
-
rangeIterable(min: LocalDateInput, max: LocalDateInput, incl
|
|
278
|
+
rangeIterable(min: LocalDateInput, max: LocalDateInput, incl: Inclusiveness, step?: number, stepUnit?: LocalDateUnit): Iterable2<LocalDate>;
|
|
278
279
|
getYearLength(year: number): number;
|
|
279
280
|
getMonthLength(year: number, month: number): number;
|
|
280
281
|
isLeapYear(year: number): boolean;
|
|
@@ -23,8 +23,8 @@ export class LocalDate {
|
|
|
23
23
|
get(unit) {
|
|
24
24
|
return unit === 'year' ? this.year : unit === 'month' ? this.month : this.day;
|
|
25
25
|
}
|
|
26
|
-
set(unit, v,
|
|
27
|
-
const t = mutate ? this : this.clone();
|
|
26
|
+
set(unit, v, opt = {}) {
|
|
27
|
+
const t = opt.mutate ? this : this.clone();
|
|
28
28
|
if (unit === 'year') {
|
|
29
29
|
t.year = v;
|
|
30
30
|
}
|
|
@@ -88,10 +88,9 @@ export class LocalDate {
|
|
|
88
88
|
isSameOrAfter(d) {
|
|
89
89
|
return this.compare(d) >= 0;
|
|
90
90
|
}
|
|
91
|
-
isBetween(min, max, incl
|
|
91
|
+
isBetween(min, max, incl) {
|
|
92
92
|
let r = this.compare(min);
|
|
93
|
-
|
|
94
|
-
if (r < 0 || (r === 0 && incl[0] === '('))
|
|
93
|
+
if (r < 0)
|
|
95
94
|
return false;
|
|
96
95
|
r = this.compare(max);
|
|
97
96
|
if (r > 0 || (r === 0 && incl[1] === ')'))
|
|
@@ -267,7 +266,7 @@ export class LocalDate {
|
|
|
267
266
|
minusYears(num) {
|
|
268
267
|
return this.plus(-num, 'year');
|
|
269
268
|
}
|
|
270
|
-
plus(num, unit,
|
|
269
|
+
plus(num, unit, opt = {}) {
|
|
271
270
|
num = Math.floor(num); // if a fractional number like 0.5 is passed - it will be floored, as LocalDate only deals with "whole days" as minimal unit
|
|
272
271
|
let { day, month, year } = this;
|
|
273
272
|
if (unit === 'week') {
|
|
@@ -324,7 +323,7 @@ export class LocalDate {
|
|
|
324
323
|
}
|
|
325
324
|
}
|
|
326
325
|
}
|
|
327
|
-
if (mutate) {
|
|
326
|
+
if (opt.mutate) {
|
|
328
327
|
this.year = year;
|
|
329
328
|
this.month = month;
|
|
330
329
|
this.day = day;
|
|
@@ -332,8 +331,8 @@ export class LocalDate {
|
|
|
332
331
|
}
|
|
333
332
|
return new LocalDate(year, month, day);
|
|
334
333
|
}
|
|
335
|
-
minus(num, unit,
|
|
336
|
-
return this.plus(-num, unit,
|
|
334
|
+
minus(num, unit, opt = {}) {
|
|
335
|
+
return this.plus(-num, unit, opt);
|
|
337
336
|
}
|
|
338
337
|
startOf(unit) {
|
|
339
338
|
if (unit === 'day')
|
|
@@ -613,9 +612,9 @@ class LocalDateFactory {
|
|
|
613
612
|
/**
|
|
614
613
|
* Sorts an array of LocalDates in `dir` order (ascending by default).
|
|
615
614
|
*/
|
|
616
|
-
sort(items,
|
|
617
|
-
const mod = dir === 'desc' ? -1 : 1;
|
|
618
|
-
return (mutate ? items : [...items]).sort((a, b) => a.compare(b) * mod);
|
|
615
|
+
sort(items, opt = {}) {
|
|
616
|
+
const mod = opt.dir === 'desc' ? -1 : 1;
|
|
617
|
+
return (opt.mutate ? items : [...items]).sort((a, b) => a.compare(b) * mod);
|
|
619
618
|
}
|
|
620
619
|
/**
|
|
621
620
|
* Returns the earliest (min) LocalDate from the array, or undefined if the array is empty.
|
|
@@ -669,14 +668,14 @@ class LocalDateFactory {
|
|
|
669
668
|
* Returns the range (array) of LocalDates between min and max.
|
|
670
669
|
* By default, min is included, max is excluded.
|
|
671
670
|
*/
|
|
672
|
-
range(min, max, incl
|
|
671
|
+
range(min, max, incl, step = 1, stepUnit = 'day') {
|
|
673
672
|
return this.rangeIterable(min, max, incl, step, stepUnit).toArray();
|
|
674
673
|
}
|
|
675
674
|
/**
|
|
676
675
|
* Returns the Iterable2 of LocalDates between min and max.
|
|
677
676
|
* By default, min is included, max is excluded.
|
|
678
677
|
*/
|
|
679
|
-
rangeIterable(min, max, incl
|
|
678
|
+
rangeIterable(min, max, incl, step = 1, stepUnit = 'day') {
|
|
680
679
|
if (stepUnit === 'week') {
|
|
681
680
|
step *= 7;
|
|
682
681
|
stepUnit = 'day';
|
|
@@ -684,12 +683,11 @@ class LocalDateFactory {
|
|
|
684
683
|
const $min = this.fromInput(min).startOf(stepUnit);
|
|
685
684
|
const $max = this.fromInput(max).startOf(stepUnit);
|
|
686
685
|
let value = $min;
|
|
687
|
-
|
|
688
|
-
if (value.isAfter($min, incl[0] === '[')) {
|
|
686
|
+
if (value.isSameOrAfter($min)) {
|
|
689
687
|
// ok
|
|
690
688
|
}
|
|
691
689
|
else {
|
|
692
|
-
value.plus(1, stepUnit, true);
|
|
690
|
+
value.plus(1, stepUnit, { mutate: true });
|
|
693
691
|
}
|
|
694
692
|
const rightInclusive = incl[1] === ']';
|
|
695
693
|
return Iterable2.of({
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { MutateOptions } from '../array/array.util.js';
|
|
1
2
|
import type { IANATimezone, Inclusiveness, IsoDate, IsoDateTime, MonthId, NumberOfHours, NumberOfMinutes, SortDirection, UnixTimestamp, UnixTimestampMillis } from '../types.js';
|
|
2
3
|
import type { LocalDate } from './localDate.js';
|
|
3
4
|
import { WallTime } from './wallTime.js';
|
|
@@ -86,7 +87,7 @@ export declare class LocalTime {
|
|
|
86
87
|
*/
|
|
87
88
|
getUTCOffsetString(tz: IANATimezone): string;
|
|
88
89
|
get(unit: LocalTimeUnit): number;
|
|
89
|
-
set(unit: LocalTimeUnit, v: number,
|
|
90
|
+
set(unit: LocalTimeUnit, v: number, opt?: MutateOptions): LocalTime;
|
|
90
91
|
get year(): number;
|
|
91
92
|
setYear(v: number): LocalTime;
|
|
92
93
|
get month(): number;
|
|
@@ -117,7 +118,7 @@ export declare class LocalTime {
|
|
|
117
118
|
* If this LocalTime is Monday, and desired DoW is also Monday - `this` is returned.
|
|
118
119
|
*/
|
|
119
120
|
setNextDayOfWeek(dow: ISODayOfWeek): LocalTime;
|
|
120
|
-
setComponents(c: Partial<DateTimeObject>,
|
|
121
|
+
setComponents(c: Partial<DateTimeObject>, opt?: MutateOptions): LocalTime;
|
|
121
122
|
plusSeconds(num: number): LocalTime;
|
|
122
123
|
plusMinutes(num: number): LocalTime;
|
|
123
124
|
plusHours(num: number): LocalTime;
|
|
@@ -132,12 +133,12 @@ export declare class LocalTime {
|
|
|
132
133
|
minusWeeks(num: number): LocalTime;
|
|
133
134
|
minusMonths(num: number): LocalTime;
|
|
134
135
|
minusYears(num: number): LocalTime;
|
|
135
|
-
plus(num: number, unit: LocalTimeUnit,
|
|
136
|
-
minus(num: number, unit: LocalTimeUnit,
|
|
136
|
+
plus(num: number, unit: LocalTimeUnit, opt?: MutateOptions): LocalTime;
|
|
137
|
+
minus(num: number, unit: LocalTimeUnit, opt?: MutateOptions): LocalTime;
|
|
137
138
|
absDiff(other: LocalTimeInput, unit: LocalTimeUnit): number;
|
|
138
139
|
diff(other: LocalTimeInput, unit: LocalTimeUnit): number;
|
|
139
|
-
startOf(unit: LocalTimeUnit,
|
|
140
|
-
endOf(unit: LocalTimeUnit,
|
|
140
|
+
startOf(unit: LocalTimeUnit, opt?: MutateOptions): LocalTime;
|
|
141
|
+
endOf(unit: LocalTimeUnit, opt?: MutateOptions): LocalTime;
|
|
141
142
|
/**
|
|
142
143
|
* Returns how many days are in the current month.
|
|
143
144
|
* E.g 31 for January.
|
|
@@ -148,7 +149,7 @@ export declare class LocalTime {
|
|
|
148
149
|
isSameOrBefore(d: LocalTimeInput): boolean;
|
|
149
150
|
isAfter(d: LocalTimeInput, inclusive?: boolean): boolean;
|
|
150
151
|
isSameOrAfter(d: LocalTimeInput): boolean;
|
|
151
|
-
isBetween(min: LocalTimeInput, max: LocalTimeInput, incl
|
|
152
|
+
isBetween(min: LocalTimeInput, max: LocalTimeInput, incl: Inclusiveness): boolean;
|
|
152
153
|
/**
|
|
153
154
|
* Checks if this localTime is older (<) than "now" by X units.
|
|
154
155
|
*
|
|
@@ -314,7 +315,7 @@ declare class LocalTimeFactory {
|
|
|
314
315
|
* consider caching the Intl.supportedValuesOf values as Set and reuse that.
|
|
315
316
|
*/
|
|
316
317
|
isTimezoneValid(tz: string): boolean;
|
|
317
|
-
sort(items: LocalTime[], dir?: SortDirection,
|
|
318
|
+
sort(items: LocalTime[], dir?: SortDirection, opt?: MutateOptions): LocalTime[];
|
|
318
319
|
minOrUndefined(items: LocalTimeInputNullable[]): LocalTime | undefined;
|
|
319
320
|
min(items: LocalTimeInputNullable[]): LocalTime;
|
|
320
321
|
maxOrUndefined(items: LocalTimeInputNullable[]): LocalTime | undefined;
|