@naturalcycles/js-lib 14.188.2 → 14.190.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/dist/array/array.util.d.ts +6 -2
- package/dist/array/array.util.js +10 -3
- package/dist/datetime/localDate.d.ts +17 -9
- package/dist/datetime/localDate.js +29 -17
- package/dist/datetime/localTime.d.ts +17 -9
- package/dist/datetime/localTime.js +28 -16
- package/dist/is.util.d.ts +1 -0
- package/dist/is.util.js +5 -1
- package/dist/number/number.util.d.ts +2 -1
- package/dist/number/number.util.js +2 -2
- package/dist/types.d.ts +1 -0
- package/dist-esm/array/array.util.js +8 -2
- package/dist-esm/datetime/localDate.js +29 -17
- package/dist-esm/datetime/localTime.js +28 -16
- package/dist-esm/is.util.js +3 -0
- package/dist-esm/number/number.util.js +2 -2
- package/package.json +2 -2
- package/src/array/array.util.ts +10 -3
- package/src/datetime/localDate.ts +32 -17
- package/src/datetime/localTime.ts +31 -16
- package/src/is.util.ts +4 -0
- package/src/number/number.util.ts +8 -2
- package/src/types.ts +2 -0
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { FalsyValue, Mapper, Predicate, StringMap } from '../types';
|
|
1
|
+
import type { FalsyValue, Mapper, Predicate, SortDirection, StringMap } from '../types';
|
|
2
2
|
/**
|
|
3
3
|
* Creates an array of elements split into groups the length of size. If collection can’t be split evenly, the
|
|
4
4
|
* final chunk will be the remaining elements.
|
|
@@ -93,7 +93,11 @@ export declare function _groupBy<T>(items: readonly T[], mapper: Mapper<T, any>)
|
|
|
93
93
|
* Same:
|
|
94
94
|
* _sortBy([{age: 20}, {age: 10}], o => o.age)
|
|
95
95
|
*/
|
|
96
|
-
export declare function _sortBy<T>(items: T[], mapper: Mapper<T, any>, mutate?: boolean,
|
|
96
|
+
export declare function _sortBy<T>(items: T[], mapper: Mapper<T, any>, mutate?: boolean, dir?: SortDirection): T[];
|
|
97
|
+
/**
|
|
98
|
+
* Alias for _sortBy with descending order.
|
|
99
|
+
*/
|
|
100
|
+
export declare function _sortDescBy<T>(items: T[], mapper: Mapper<T, any>, mutate?: boolean): T[];
|
|
97
101
|
/**
|
|
98
102
|
* Like items.find(), but it tries to find from the END of the array.
|
|
99
103
|
*/
|
package/dist/array/array.util.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports._minByOrUndefined = exports._maxByOrUndefined = exports._minBy = exports._maxBy = exports._max = exports._maxOrUndefined = exports._min = exports._minOrUndefined = exports._lastOrUndefined = exports._last = exports._shuffle = exports._mapToObject = exports._sumBy = exports._sum = exports._difference = exports._intersection = exports._countBy = exports._dropRightWhile = exports._dropWhile = exports._takeRightWhile = exports._takeWhile = exports._findLast = exports._sortBy = exports._groupBy = exports._by = exports._uniqBy = exports._pushUniqBy = exports._pushUniq = exports._uniq = exports._chunk = void 0;
|
|
3
|
+
exports._minByOrUndefined = exports._maxByOrUndefined = exports._minBy = exports._maxBy = exports._max = exports._maxOrUndefined = exports._min = exports._minOrUndefined = exports._lastOrUndefined = exports._last = exports._shuffle = exports._mapToObject = exports._sumBy = exports._sum = exports._difference = exports._intersection = exports._countBy = exports._dropRightWhile = exports._dropWhile = exports._takeRightWhile = exports._takeWhile = exports._findLast = exports._sortDescBy = exports._sortBy = exports._groupBy = exports._by = exports._uniqBy = exports._pushUniqBy = exports._pushUniq = exports._uniq = exports._chunk = void 0;
|
|
4
4
|
const is_util_1 = require("../is.util");
|
|
5
5
|
/**
|
|
6
6
|
* Creates an array of elements split into groups the length of size. If collection can’t be split evenly, the
|
|
@@ -151,8 +151,8 @@ exports._groupBy = _groupBy;
|
|
|
151
151
|
* Same:
|
|
152
152
|
* _sortBy([{age: 20}, {age: 10}], o => o.age)
|
|
153
153
|
*/
|
|
154
|
-
function _sortBy(items, mapper, mutate = false,
|
|
155
|
-
const mod =
|
|
154
|
+
function _sortBy(items, mapper, mutate = false, dir = 'asc') {
|
|
155
|
+
const mod = dir === 'desc' ? -1 : 1;
|
|
156
156
|
return (mutate ? items : [...items]).sort((_a, _b) => {
|
|
157
157
|
const [a, b] = [_a, _b].map(mapper);
|
|
158
158
|
if (typeof a === 'number' && typeof b === 'number')
|
|
@@ -161,6 +161,13 @@ function _sortBy(items, mapper, mutate = false, descending = false) {
|
|
|
161
161
|
});
|
|
162
162
|
}
|
|
163
163
|
exports._sortBy = _sortBy;
|
|
164
|
+
/**
|
|
165
|
+
* Alias for _sortBy with descending order.
|
|
166
|
+
*/
|
|
167
|
+
function _sortDescBy(items, mapper, mutate = false) {
|
|
168
|
+
return _sortBy(items, mapper, mutate, 'desc');
|
|
169
|
+
}
|
|
170
|
+
exports._sortDescBy = _sortDescBy;
|
|
164
171
|
/**
|
|
165
172
|
* Like items.find(), but it tries to find from the END of the array.
|
|
166
173
|
*/
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { IsoDateString, IsoDateTimeString, MonthId, UnixTimestampMillisNumber, UnixTimestampNumber } from '../types';
|
|
1
|
+
import type { IsoDateString, IsoDateTimeString, MonthId, SortDirection, UnixTimestampMillisNumber, UnixTimestampNumber } from '../types';
|
|
2
2
|
import { LocalTime } from './localTime';
|
|
3
3
|
export type LocalDateUnit = LocalDateUnitStrict | 'week';
|
|
4
4
|
export type LocalDateUnitStrict = 'year' | 'month' | 'day';
|
|
@@ -29,7 +29,7 @@ export declare class LocalDate {
|
|
|
29
29
|
static isValid(iso: string | undefined | null): boolean;
|
|
30
30
|
static today(): LocalDate;
|
|
31
31
|
static todayUTC(): LocalDate;
|
|
32
|
-
static sort(items: LocalDate[], mutate?: boolean,
|
|
32
|
+
static sort(items: LocalDate[], mutate?: boolean, dir?: SortDirection): LocalDate[];
|
|
33
33
|
static earliestOrUndefined(items: LocalDateInput[]): LocalDate | undefined;
|
|
34
34
|
static earliest(items: LocalDateInput[]): LocalDate;
|
|
35
35
|
static latestOrUndefined(items: LocalDateInput[]): LocalDate | undefined;
|
|
@@ -50,7 +50,7 @@ export declare class LocalDate {
|
|
|
50
50
|
isSameOrAfter(d: LocalDateInput): boolean;
|
|
51
51
|
isBetween(min: LocalDateInput, max: LocalDateInput, incl?: Inclusiveness): boolean;
|
|
52
52
|
/**
|
|
53
|
-
* Checks if this localDate is older than "today" by X units.
|
|
53
|
+
* Checks if this localDate is older (<) than "today" by X units.
|
|
54
54
|
*
|
|
55
55
|
* Example:
|
|
56
56
|
*
|
|
@@ -60,7 +60,11 @@ export declare class LocalDate {
|
|
|
60
60
|
*/
|
|
61
61
|
isOlderThan(n: number, unit: LocalDateUnit, today?: LocalDateInput): boolean;
|
|
62
62
|
/**
|
|
63
|
-
* Checks if this localDate is
|
|
63
|
+
* Checks if this localDate is same or older (<=) than "today" by X units.
|
|
64
|
+
*/
|
|
65
|
+
isSameOrOlderThan(n: number, unit: LocalDateUnit, today?: LocalDateInput): boolean;
|
|
66
|
+
/**
|
|
67
|
+
* Checks if this localDate is younger (>) than "today" by X units.
|
|
64
68
|
*
|
|
65
69
|
* Example:
|
|
66
70
|
*
|
|
@@ -69,6 +73,10 @@ export declare class LocalDate {
|
|
|
69
73
|
* Third argument allows to override "today".
|
|
70
74
|
*/
|
|
71
75
|
isYoungerThan(n: number, unit: LocalDateUnit, today?: LocalDateInput): boolean;
|
|
76
|
+
/**
|
|
77
|
+
* Checks if this localDate is same or younger (>=) than "today" by X units.
|
|
78
|
+
*/
|
|
79
|
+
isSameOrYoungerThan(n: number, unit: LocalDateUnit, today?: LocalDateInput): boolean;
|
|
72
80
|
/**
|
|
73
81
|
* Returns 1 if this > d
|
|
74
82
|
* returns 0 if they are equal
|
|
@@ -85,16 +93,16 @@ export declare class LocalDate {
|
|
|
85
93
|
* a.diff(b) means "a minus b"
|
|
86
94
|
*/
|
|
87
95
|
diff(d: LocalDateInput, unit: LocalDateUnit): number;
|
|
88
|
-
|
|
89
|
-
subtract(num: number, unit: LocalDateUnit, mutate?: boolean): LocalDate;
|
|
96
|
+
plus(num: number, unit: LocalDateUnit, mutate?: boolean): LocalDate;
|
|
90
97
|
/**
|
|
91
|
-
*
|
|
98
|
+
* @deprecated use `minus` instead
|
|
92
99
|
*/
|
|
100
|
+
subtract(num: number, unit: LocalDateUnit, mutate?: boolean): LocalDate;
|
|
93
101
|
minus(num: number, unit: LocalDateUnit, mutate?: boolean): LocalDate;
|
|
94
102
|
/**
|
|
95
|
-
*
|
|
103
|
+
* @deprecated use `plus` instead
|
|
96
104
|
*/
|
|
97
|
-
|
|
105
|
+
add(num: number, unit: LocalDateUnit, mutate?: boolean): LocalDate;
|
|
98
106
|
startOf(unit: LocalDateUnitStrict): LocalDate;
|
|
99
107
|
endOf(unit: LocalDateUnitStrict): LocalDate;
|
|
100
108
|
static getYearLength(year: number): number;
|
|
@@ -81,8 +81,8 @@ class LocalDate {
|
|
|
81
81
|
static todayUTC() {
|
|
82
82
|
return this.fromDateUTC(new Date());
|
|
83
83
|
}
|
|
84
|
-
static sort(items, mutate = false,
|
|
85
|
-
const mod =
|
|
84
|
+
static sort(items, mutate = false, dir = 'asc') {
|
|
85
|
+
const mod = dir === 'desc' ? -1 : 1;
|
|
86
86
|
return (mutate ? items : [...items]).sort((a, b) => a.cmp(b) * mod);
|
|
87
87
|
}
|
|
88
88
|
static earliestOrUndefined(items) {
|
|
@@ -117,12 +117,12 @@ class LocalDate {
|
|
|
117
117
|
// ok
|
|
118
118
|
}
|
|
119
119
|
else {
|
|
120
|
-
current.
|
|
120
|
+
current.plus(1, stepUnit, true);
|
|
121
121
|
}
|
|
122
122
|
const incl2 = incl[1] === ']';
|
|
123
123
|
while (current.isBefore($max, incl2)) {
|
|
124
124
|
dates.push(current);
|
|
125
|
-
current = current.
|
|
125
|
+
current = current.plus(step, stepUnit);
|
|
126
126
|
}
|
|
127
127
|
return dates;
|
|
128
128
|
}
|
|
@@ -180,7 +180,7 @@ class LocalDate {
|
|
|
180
180
|
return true;
|
|
181
181
|
}
|
|
182
182
|
/**
|
|
183
|
-
* Checks if this localDate is older than "today" by X units.
|
|
183
|
+
* Checks if this localDate is older (<) than "today" by X units.
|
|
184
184
|
*
|
|
185
185
|
* Example:
|
|
186
186
|
*
|
|
@@ -189,10 +189,16 @@ class LocalDate {
|
|
|
189
189
|
* Third argument allows to override "today".
|
|
190
190
|
*/
|
|
191
191
|
isOlderThan(n, unit, today) {
|
|
192
|
-
return this.isBefore(LocalDate.of(today || new Date()).
|
|
192
|
+
return this.isBefore(LocalDate.of(today || new Date()).plus(-n, unit));
|
|
193
193
|
}
|
|
194
194
|
/**
|
|
195
|
-
* Checks if this localDate is
|
|
195
|
+
* Checks if this localDate is same or older (<=) than "today" by X units.
|
|
196
|
+
*/
|
|
197
|
+
isSameOrOlderThan(n, unit, today) {
|
|
198
|
+
return this.isSameOrBefore(LocalDate.of(today || new Date()).plus(-n, unit));
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Checks if this localDate is younger (>) than "today" by X units.
|
|
196
202
|
*
|
|
197
203
|
* Example:
|
|
198
204
|
*
|
|
@@ -201,7 +207,13 @@ class LocalDate {
|
|
|
201
207
|
* Third argument allows to override "today".
|
|
202
208
|
*/
|
|
203
209
|
isYoungerThan(n, unit, today) {
|
|
204
|
-
return
|
|
210
|
+
return this.isAfter(LocalDate.of(today || new Date()).plus(-n, unit));
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Checks if this localDate is same or younger (>=) than "today" by X units.
|
|
214
|
+
*/
|
|
215
|
+
isSameOrYoungerThan(n, unit, today) {
|
|
216
|
+
return this.isSameOrAfter(LocalDate.of(today || new Date()).plus(-n, unit));
|
|
205
217
|
}
|
|
206
218
|
/**
|
|
207
219
|
* Returns 1 if this > d
|
|
@@ -285,7 +297,7 @@ class LocalDate {
|
|
|
285
297
|
}
|
|
286
298
|
return days * sign || 0;
|
|
287
299
|
}
|
|
288
|
-
|
|
300
|
+
plus(num, unit, mutate = false) {
|
|
289
301
|
let { $day, $month, $year } = this;
|
|
290
302
|
if (unit === 'week') {
|
|
291
303
|
num *= 7;
|
|
@@ -349,20 +361,20 @@ class LocalDate {
|
|
|
349
361
|
}
|
|
350
362
|
return new LocalDate($year, $month, $day);
|
|
351
363
|
}
|
|
352
|
-
subtract(num, unit, mutate = false) {
|
|
353
|
-
return this.add(-num, unit, mutate);
|
|
354
|
-
}
|
|
355
364
|
/**
|
|
356
|
-
*
|
|
365
|
+
* @deprecated use `minus` instead
|
|
357
366
|
*/
|
|
367
|
+
subtract(num, unit, mutate = false) {
|
|
368
|
+
return this.plus(-num, unit, mutate);
|
|
369
|
+
}
|
|
358
370
|
minus(num, unit, mutate = false) {
|
|
359
|
-
return this.
|
|
371
|
+
return this.plus(-num, unit, mutate);
|
|
360
372
|
}
|
|
361
373
|
/**
|
|
362
|
-
*
|
|
374
|
+
* @deprecated use `plus` instead
|
|
363
375
|
*/
|
|
364
|
-
|
|
365
|
-
return this.
|
|
376
|
+
add(num, unit, mutate = false) {
|
|
377
|
+
return this.plus(num, unit, mutate);
|
|
366
378
|
}
|
|
367
379
|
startOf(unit) {
|
|
368
380
|
if (unit === 'day')
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { IsoDateString, IsoDateTimeString, MonthId, UnixTimestampMillisNumber, UnixTimestampNumber } from '../types';
|
|
1
|
+
import type { IsoDateString, IsoDateTimeString, MonthId, SortDirection, UnixTimestampMillisNumber, UnixTimestampNumber } from '../types';
|
|
2
2
|
import type { Inclusiveness } from './localDate';
|
|
3
3
|
import { LocalDate } from './localDate';
|
|
4
4
|
export type LocalTimeUnit = 'year' | 'month' | 'week' | 'day' | 'hour' | 'minute' | 'second';
|
|
@@ -70,21 +70,21 @@ export declare class LocalTime {
|
|
|
70
70
|
second(): number;
|
|
71
71
|
second(v: number): LocalTime;
|
|
72
72
|
setComponents(c: Partial<LocalTimeComponents>, mutate?: boolean): LocalTime;
|
|
73
|
-
|
|
74
|
-
subtract(num: number, unit: LocalTimeUnit, mutate?: boolean): LocalTime;
|
|
73
|
+
plus(num: number, unit: LocalTimeUnit, mutate?: boolean): LocalTime;
|
|
75
74
|
/**
|
|
76
|
-
*
|
|
75
|
+
* @deprecated use `minus` instead
|
|
77
76
|
*/
|
|
77
|
+
subtract(num: number, unit: LocalTimeUnit, mutate?: boolean): LocalTime;
|
|
78
78
|
minus(num: number, unit: LocalTimeUnit, mutate?: boolean): LocalTime;
|
|
79
79
|
/**
|
|
80
|
-
*
|
|
80
|
+
* @deprecated use `plus` instead
|
|
81
81
|
*/
|
|
82
|
-
|
|
82
|
+
add(num: number, unit: LocalTimeUnit, mutate?: boolean): LocalTime;
|
|
83
83
|
absDiff(other: LocalTimeInput, unit: LocalTimeUnit): number;
|
|
84
84
|
diff(other: LocalTimeInput, unit: LocalTimeUnit): number;
|
|
85
85
|
startOf(unit: LocalTimeUnit, mutate?: boolean): LocalTime;
|
|
86
86
|
endOf(unit: LocalTimeUnit, mutate?: boolean): LocalTime;
|
|
87
|
-
static sort(items: LocalTime[], mutate?: boolean,
|
|
87
|
+
static sort(items: LocalTime[], mutate?: boolean, dir?: SortDirection): LocalTime[];
|
|
88
88
|
static earliestOrUndefined(items: LocalTimeInput[]): LocalTime | undefined;
|
|
89
89
|
static earliest(items: LocalTimeInput[]): LocalTime;
|
|
90
90
|
static latestOrUndefined(items: LocalTimeInput[]): LocalTime | undefined;
|
|
@@ -96,7 +96,7 @@ export declare class LocalTime {
|
|
|
96
96
|
isSameOrAfter(d: LocalTimeInput): boolean;
|
|
97
97
|
isBetween(min: LocalTimeInput, max: LocalTimeInput, incl?: Inclusiveness): boolean;
|
|
98
98
|
/**
|
|
99
|
-
* Checks if this localTime is older than "now" by X units.
|
|
99
|
+
* Checks if this localTime is older (<) than "now" by X units.
|
|
100
100
|
*
|
|
101
101
|
* Example:
|
|
102
102
|
*
|
|
@@ -106,7 +106,11 @@ export declare class LocalTime {
|
|
|
106
106
|
*/
|
|
107
107
|
isOlderThan(n: number, unit: LocalTimeUnit, now?: LocalTimeInput): boolean;
|
|
108
108
|
/**
|
|
109
|
-
* Checks if this localTime is
|
|
109
|
+
* Checks if this localTime is same or older (<=) than "now" by X units.
|
|
110
|
+
*/
|
|
111
|
+
isSameOrOlderThan(n: number, unit: LocalTimeUnit, now?: LocalTimeInput): boolean;
|
|
112
|
+
/**
|
|
113
|
+
* Checks if this localTime is younger (>) than "now" by X units.
|
|
110
114
|
*
|
|
111
115
|
* Example:
|
|
112
116
|
*
|
|
@@ -115,6 +119,10 @@ export declare class LocalTime {
|
|
|
115
119
|
* Third argument allows to override "now".
|
|
116
120
|
*/
|
|
117
121
|
isYoungerThan(n: number, unit: LocalTimeUnit, now?: LocalTimeInput): boolean;
|
|
122
|
+
/**
|
|
123
|
+
* Checks if this localTime is same or younger (>=) than "now" by X units.
|
|
124
|
+
*/
|
|
125
|
+
isSameOrYoungerThan(n: number, unit: LocalTimeUnit, now?: LocalTimeInput): boolean;
|
|
118
126
|
/**
|
|
119
127
|
* Returns 1 if this > d
|
|
120
128
|
* returns 0 if they are equal
|
|
@@ -172,7 +172,7 @@ class LocalTime {
|
|
|
172
172
|
return dow;
|
|
173
173
|
}
|
|
174
174
|
(0, assert_1._assert)(VALID_DAYS_OF_WEEK.has(v), `Invalid dayOfWeek: ${v}`);
|
|
175
|
-
return this.
|
|
175
|
+
return this.plus(v - dow, 'day');
|
|
176
176
|
}
|
|
177
177
|
hour(v) {
|
|
178
178
|
return v === undefined ? this.get('hour') : this.set('hour', v);
|
|
@@ -200,7 +200,7 @@ class LocalTime {
|
|
|
200
200
|
}
|
|
201
201
|
return mutate ? this : new LocalTime(d);
|
|
202
202
|
}
|
|
203
|
-
|
|
203
|
+
plus(num, unit, mutate = false) {
|
|
204
204
|
if (unit === 'week') {
|
|
205
205
|
num *= 7;
|
|
206
206
|
unit = 'day';
|
|
@@ -211,20 +211,20 @@ class LocalTime {
|
|
|
211
211
|
}
|
|
212
212
|
return this.set(unit, this.get(unit) + num, mutate);
|
|
213
213
|
}
|
|
214
|
-
subtract(num, unit, mutate = false) {
|
|
215
|
-
return this.add(num * -1, unit, mutate);
|
|
216
|
-
}
|
|
217
214
|
/**
|
|
218
|
-
*
|
|
215
|
+
* @deprecated use `minus` instead
|
|
219
216
|
*/
|
|
217
|
+
subtract(num, unit, mutate = false) {
|
|
218
|
+
return this.plus(num * -1, unit, mutate);
|
|
219
|
+
}
|
|
220
220
|
minus(num, unit, mutate = false) {
|
|
221
|
-
return this.
|
|
221
|
+
return this.plus(num * -1, unit, mutate);
|
|
222
222
|
}
|
|
223
223
|
/**
|
|
224
|
-
*
|
|
224
|
+
* @deprecated use `plus` instead
|
|
225
225
|
*/
|
|
226
|
-
|
|
227
|
-
return this.
|
|
226
|
+
add(num, unit, mutate = false) {
|
|
227
|
+
return this.plus(num, unit, mutate);
|
|
228
228
|
}
|
|
229
229
|
absDiff(other, unit) {
|
|
230
230
|
return Math.abs(this.diff(other, unit));
|
|
@@ -314,8 +314,8 @@ class LocalTime {
|
|
|
314
314
|
}
|
|
315
315
|
return mutate ? this : new LocalTime(d);
|
|
316
316
|
}
|
|
317
|
-
static sort(items, mutate = false,
|
|
318
|
-
const mod =
|
|
317
|
+
static sort(items, mutate = false, dir = 'asc') {
|
|
318
|
+
const mod = dir === 'desc' ? -1 : 1;
|
|
319
319
|
return (mutate ? items : [...items]).sort((a, b) => {
|
|
320
320
|
const v1 = a.$date.valueOf();
|
|
321
321
|
const v2 = b.$date.valueOf();
|
|
@@ -370,7 +370,7 @@ class LocalTime {
|
|
|
370
370
|
return true;
|
|
371
371
|
}
|
|
372
372
|
/**
|
|
373
|
-
* Checks if this localTime is older than "now" by X units.
|
|
373
|
+
* Checks if this localTime is older (<) than "now" by X units.
|
|
374
374
|
*
|
|
375
375
|
* Example:
|
|
376
376
|
*
|
|
@@ -379,10 +379,16 @@ class LocalTime {
|
|
|
379
379
|
* Third argument allows to override "now".
|
|
380
380
|
*/
|
|
381
381
|
isOlderThan(n, unit, now) {
|
|
382
|
-
return this.isBefore(LocalTime.of(now ?? new Date()).
|
|
382
|
+
return this.isBefore(LocalTime.of(now ?? new Date()).plus(-n, unit));
|
|
383
|
+
}
|
|
384
|
+
/**
|
|
385
|
+
* Checks if this localTime is same or older (<=) than "now" by X units.
|
|
386
|
+
*/
|
|
387
|
+
isSameOrOlderThan(n, unit, now) {
|
|
388
|
+
return this.isSameOrBefore(LocalTime.of(now ?? new Date()).plus(-n, unit));
|
|
383
389
|
}
|
|
384
390
|
/**
|
|
385
|
-
* Checks if this localTime is younger than "now" by X units.
|
|
391
|
+
* Checks if this localTime is younger (>) than "now" by X units.
|
|
386
392
|
*
|
|
387
393
|
* Example:
|
|
388
394
|
*
|
|
@@ -391,7 +397,13 @@ class LocalTime {
|
|
|
391
397
|
* Third argument allows to override "now".
|
|
392
398
|
*/
|
|
393
399
|
isYoungerThan(n, unit, now) {
|
|
394
|
-
return
|
|
400
|
+
return this.isAfter(LocalTime.of(now ?? new Date()).plus(-n, unit));
|
|
401
|
+
}
|
|
402
|
+
/**
|
|
403
|
+
* Checks if this localTime is same or younger (>=) than "now" by X units.
|
|
404
|
+
*/
|
|
405
|
+
isSameOrYoungerThan(n, unit, now) {
|
|
406
|
+
return this.isSameOrAfter(LocalTime.of(now ?? new Date()).plus(-n, unit));
|
|
395
407
|
}
|
|
396
408
|
/**
|
|
397
409
|
* Returns 1 if this > d
|
package/dist/is.util.d.ts
CHANGED
|
@@ -28,6 +28,7 @@ export declare const _isFalsy: <T>(v: T) => v is Falsy<T>;
|
|
|
28
28
|
export declare function _isObject(obj: any): obj is AnyObject;
|
|
29
29
|
export declare function _isPrimitive(v: any): v is Primitive;
|
|
30
30
|
export declare function _isEmptyObject(obj: AnyObject): boolean;
|
|
31
|
+
export declare function _isNotEmptyObject(obj: AnyObject): boolean;
|
|
31
32
|
/**
|
|
32
33
|
* Object is considered empty if it's one of:
|
|
33
34
|
* undefined
|
package/dist/is.util.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports._isNotEmpty = exports._isEmpty = exports._isEmptyObject = exports._isPrimitive = exports._isObject = exports._isFalsy = exports._isTruthy = exports._isNotNullish = exports._isNullish = exports._isUndefined = exports._isNull = void 0;
|
|
3
|
+
exports._isNotEmpty = exports._isEmpty = exports._isNotEmptyObject = exports._isEmptyObject = exports._isPrimitive = exports._isObject = exports._isFalsy = exports._isTruthy = exports._isNotNullish = exports._isNullish = exports._isUndefined = exports._isNull = void 0;
|
|
4
4
|
const _isNull = (v) => v === null;
|
|
5
5
|
exports._isNull = _isNull;
|
|
6
6
|
const _isUndefined = (v) => v === undefined;
|
|
@@ -47,6 +47,10 @@ function _isEmptyObject(obj) {
|
|
|
47
47
|
return Object.keys(obj).length === 0;
|
|
48
48
|
}
|
|
49
49
|
exports._isEmptyObject = _isEmptyObject;
|
|
50
|
+
function _isNotEmptyObject(obj) {
|
|
51
|
+
return Object.keys(obj).length > 0;
|
|
52
|
+
}
|
|
53
|
+
exports._isNotEmptyObject = _isNotEmptyObject;
|
|
50
54
|
/**
|
|
51
55
|
* Object is considered empty if it's one of:
|
|
52
56
|
* undefined
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { SortDirection } from '../types';
|
|
1
2
|
export declare function _randomInt(minIncl: number, maxIncl: number): number;
|
|
2
3
|
/**
|
|
3
4
|
* Returns random item from an array.
|
|
@@ -23,7 +24,7 @@ export declare function _clamp(x: number, minIncl: number, maxIncl: number): num
|
|
|
23
24
|
* _sortNumbers([1, 3, 2])
|
|
24
25
|
* // [1, 2, 3]
|
|
25
26
|
*/
|
|
26
|
-
export declare function _sortNumbers(numbers: number[], mutate?: boolean,
|
|
27
|
+
export declare function _sortNumbers(numbers: number[], mutate?: boolean, dir?: SortDirection): number[];
|
|
27
28
|
/**
|
|
28
29
|
* Same as .toFixed(), but conveniently casts the output to Number.
|
|
29
30
|
*
|
|
@@ -39,8 +39,8 @@ exports._clamp = _clamp;
|
|
|
39
39
|
* _sortNumbers([1, 3, 2])
|
|
40
40
|
* // [1, 2, 3]
|
|
41
41
|
*/
|
|
42
|
-
function _sortNumbers(numbers, mutate = false,
|
|
43
|
-
const mod =
|
|
42
|
+
function _sortNumbers(numbers, mutate = false, dir = 'asc') {
|
|
43
|
+
const mod = dir === 'desc' ? -1 : 1;
|
|
44
44
|
return (mutate ? numbers : [...numbers]).sort((a, b) => (a - b) * mod);
|
|
45
45
|
}
|
|
46
46
|
exports._sortNumbers = _sortNumbers;
|
package/dist/types.d.ts
CHANGED
|
@@ -242,3 +242,4 @@ export declare const _objectAssign: <T extends AnyObject>(target: T, part: Parti
|
|
|
242
242
|
* Functions like pTry use that.
|
|
243
243
|
*/
|
|
244
244
|
export type ErrorDataTuple<T = unknown, ERR = Error> = [err: null, data: T] | [err: ERR, data: null];
|
|
245
|
+
export type SortDirection = 'asc' | 'desc';
|
|
@@ -141,8 +141,8 @@ export function _groupBy(items, mapper) {
|
|
|
141
141
|
* Same:
|
|
142
142
|
* _sortBy([{age: 20}, {age: 10}], o => o.age)
|
|
143
143
|
*/
|
|
144
|
-
export function _sortBy(items, mapper, mutate = false,
|
|
145
|
-
const mod =
|
|
144
|
+
export function _sortBy(items, mapper, mutate = false, dir = 'asc') {
|
|
145
|
+
const mod = dir === 'desc' ? -1 : 1;
|
|
146
146
|
return (mutate ? items : [...items]).sort((_a, _b) => {
|
|
147
147
|
const [a, b] = [_a, _b].map(mapper);
|
|
148
148
|
if (typeof a === 'number' && typeof b === 'number')
|
|
@@ -150,6 +150,12 @@ export function _sortBy(items, mapper, mutate = false, descending = false) {
|
|
|
150
150
|
return String(a).localeCompare(String(b)) * mod;
|
|
151
151
|
});
|
|
152
152
|
}
|
|
153
|
+
/**
|
|
154
|
+
* Alias for _sortBy with descending order.
|
|
155
|
+
*/
|
|
156
|
+
export function _sortDescBy(items, mapper, mutate = false) {
|
|
157
|
+
return _sortBy(items, mapper, mutate, 'desc');
|
|
158
|
+
}
|
|
153
159
|
/**
|
|
154
160
|
* Like items.find(), but it tries to find from the END of the array.
|
|
155
161
|
*/
|
|
@@ -78,8 +78,8 @@ export class LocalDate {
|
|
|
78
78
|
static todayUTC() {
|
|
79
79
|
return this.fromDateUTC(new Date());
|
|
80
80
|
}
|
|
81
|
-
static sort(items, mutate = false,
|
|
82
|
-
const mod =
|
|
81
|
+
static sort(items, mutate = false, dir = 'asc') {
|
|
82
|
+
const mod = dir === 'desc' ? -1 : 1;
|
|
83
83
|
return (mutate ? items : [...items]).sort((a, b) => a.cmp(b) * mod);
|
|
84
84
|
}
|
|
85
85
|
static earliestOrUndefined(items) {
|
|
@@ -114,12 +114,12 @@ export class LocalDate {
|
|
|
114
114
|
// ok
|
|
115
115
|
}
|
|
116
116
|
else {
|
|
117
|
-
current.
|
|
117
|
+
current.plus(1, stepUnit, true);
|
|
118
118
|
}
|
|
119
119
|
const incl2 = incl[1] === ']';
|
|
120
120
|
while (current.isBefore($max, incl2)) {
|
|
121
121
|
dates.push(current);
|
|
122
|
-
current = current.
|
|
122
|
+
current = current.plus(step, stepUnit);
|
|
123
123
|
}
|
|
124
124
|
return dates;
|
|
125
125
|
}
|
|
@@ -177,7 +177,7 @@ export class LocalDate {
|
|
|
177
177
|
return true;
|
|
178
178
|
}
|
|
179
179
|
/**
|
|
180
|
-
* Checks if this localDate is older than "today" by X units.
|
|
180
|
+
* Checks if this localDate is older (<) than "today" by X units.
|
|
181
181
|
*
|
|
182
182
|
* Example:
|
|
183
183
|
*
|
|
@@ -186,10 +186,16 @@ export class LocalDate {
|
|
|
186
186
|
* Third argument allows to override "today".
|
|
187
187
|
*/
|
|
188
188
|
isOlderThan(n, unit, today) {
|
|
189
|
-
return this.isBefore(LocalDate.of(today || new Date()).
|
|
189
|
+
return this.isBefore(LocalDate.of(today || new Date()).plus(-n, unit));
|
|
190
190
|
}
|
|
191
191
|
/**
|
|
192
|
-
* Checks if this localDate is
|
|
192
|
+
* Checks if this localDate is same or older (<=) than "today" by X units.
|
|
193
|
+
*/
|
|
194
|
+
isSameOrOlderThan(n, unit, today) {
|
|
195
|
+
return this.isSameOrBefore(LocalDate.of(today || new Date()).plus(-n, unit));
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Checks if this localDate is younger (>) than "today" by X units.
|
|
193
199
|
*
|
|
194
200
|
* Example:
|
|
195
201
|
*
|
|
@@ -198,7 +204,13 @@ export class LocalDate {
|
|
|
198
204
|
* Third argument allows to override "today".
|
|
199
205
|
*/
|
|
200
206
|
isYoungerThan(n, unit, today) {
|
|
201
|
-
return
|
|
207
|
+
return this.isAfter(LocalDate.of(today || new Date()).plus(-n, unit));
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Checks if this localDate is same or younger (>=) than "today" by X units.
|
|
211
|
+
*/
|
|
212
|
+
isSameOrYoungerThan(n, unit, today) {
|
|
213
|
+
return this.isSameOrAfter(LocalDate.of(today || new Date()).plus(-n, unit));
|
|
202
214
|
}
|
|
203
215
|
/**
|
|
204
216
|
* Returns 1 if this > d
|
|
@@ -282,7 +294,7 @@ export class LocalDate {
|
|
|
282
294
|
}
|
|
283
295
|
return days * sign || 0;
|
|
284
296
|
}
|
|
285
|
-
|
|
297
|
+
plus(num, unit, mutate = false) {
|
|
286
298
|
let { $day, $month, $year } = this;
|
|
287
299
|
if (unit === 'week') {
|
|
288
300
|
num *= 7;
|
|
@@ -346,20 +358,20 @@ export class LocalDate {
|
|
|
346
358
|
}
|
|
347
359
|
return new LocalDate($year, $month, $day);
|
|
348
360
|
}
|
|
349
|
-
subtract(num, unit, mutate = false) {
|
|
350
|
-
return this.add(-num, unit, mutate);
|
|
351
|
-
}
|
|
352
361
|
/**
|
|
353
|
-
*
|
|
362
|
+
* @deprecated use `minus` instead
|
|
354
363
|
*/
|
|
364
|
+
subtract(num, unit, mutate = false) {
|
|
365
|
+
return this.plus(-num, unit, mutate);
|
|
366
|
+
}
|
|
355
367
|
minus(num, unit, mutate = false) {
|
|
356
|
-
return this.
|
|
368
|
+
return this.plus(-num, unit, mutate);
|
|
357
369
|
}
|
|
358
370
|
/**
|
|
359
|
-
*
|
|
371
|
+
* @deprecated use `plus` instead
|
|
360
372
|
*/
|
|
361
|
-
|
|
362
|
-
return this.
|
|
373
|
+
add(num, unit, mutate = false) {
|
|
374
|
+
return this.plus(num, unit, mutate);
|
|
363
375
|
}
|
|
364
376
|
startOf(unit) {
|
|
365
377
|
if (unit === 'day')
|
|
@@ -169,7 +169,7 @@ export class LocalTime {
|
|
|
169
169
|
return dow;
|
|
170
170
|
}
|
|
171
171
|
_assert(VALID_DAYS_OF_WEEK.has(v), `Invalid dayOfWeek: ${v}`);
|
|
172
|
-
return this.
|
|
172
|
+
return this.plus(v - dow, 'day');
|
|
173
173
|
}
|
|
174
174
|
hour(v) {
|
|
175
175
|
return v === undefined ? this.get('hour') : this.set('hour', v);
|
|
@@ -198,7 +198,7 @@ export class LocalTime {
|
|
|
198
198
|
}
|
|
199
199
|
return mutate ? this : new LocalTime(d);
|
|
200
200
|
}
|
|
201
|
-
|
|
201
|
+
plus(num, unit, mutate = false) {
|
|
202
202
|
if (unit === 'week') {
|
|
203
203
|
num *= 7;
|
|
204
204
|
unit = 'day';
|
|
@@ -209,20 +209,20 @@ export class LocalTime {
|
|
|
209
209
|
}
|
|
210
210
|
return this.set(unit, this.get(unit) + num, mutate);
|
|
211
211
|
}
|
|
212
|
-
subtract(num, unit, mutate = false) {
|
|
213
|
-
return this.add(num * -1, unit, mutate);
|
|
214
|
-
}
|
|
215
212
|
/**
|
|
216
|
-
*
|
|
213
|
+
* @deprecated use `minus` instead
|
|
217
214
|
*/
|
|
215
|
+
subtract(num, unit, mutate = false) {
|
|
216
|
+
return this.plus(num * -1, unit, mutate);
|
|
217
|
+
}
|
|
218
218
|
minus(num, unit, mutate = false) {
|
|
219
|
-
return this.
|
|
219
|
+
return this.plus(num * -1, unit, mutate);
|
|
220
220
|
}
|
|
221
221
|
/**
|
|
222
|
-
*
|
|
222
|
+
* @deprecated use `plus` instead
|
|
223
223
|
*/
|
|
224
|
-
|
|
225
|
-
return this.
|
|
224
|
+
add(num, unit, mutate = false) {
|
|
225
|
+
return this.plus(num, unit, mutate);
|
|
226
226
|
}
|
|
227
227
|
absDiff(other, unit) {
|
|
228
228
|
return Math.abs(this.diff(other, unit));
|
|
@@ -312,8 +312,8 @@ export class LocalTime {
|
|
|
312
312
|
}
|
|
313
313
|
return mutate ? this : new LocalTime(d);
|
|
314
314
|
}
|
|
315
|
-
static sort(items, mutate = false,
|
|
316
|
-
const mod =
|
|
315
|
+
static sort(items, mutate = false, dir = 'asc') {
|
|
316
|
+
const mod = dir === 'desc' ? -1 : 1;
|
|
317
317
|
return (mutate ? items : [...items]).sort((a, b) => {
|
|
318
318
|
const v1 = a.$date.valueOf();
|
|
319
319
|
const v2 = b.$date.valueOf();
|
|
@@ -368,7 +368,7 @@ export class LocalTime {
|
|
|
368
368
|
return true;
|
|
369
369
|
}
|
|
370
370
|
/**
|
|
371
|
-
* Checks if this localTime is older than "now" by X units.
|
|
371
|
+
* Checks if this localTime is older (<) than "now" by X units.
|
|
372
372
|
*
|
|
373
373
|
* Example:
|
|
374
374
|
*
|
|
@@ -377,10 +377,16 @@ export class LocalTime {
|
|
|
377
377
|
* Third argument allows to override "now".
|
|
378
378
|
*/
|
|
379
379
|
isOlderThan(n, unit, now) {
|
|
380
|
-
return this.isBefore(LocalTime.of(now !== null && now !== void 0 ? now : new Date()).
|
|
380
|
+
return this.isBefore(LocalTime.of(now !== null && now !== void 0 ? now : new Date()).plus(-n, unit));
|
|
381
|
+
}
|
|
382
|
+
/**
|
|
383
|
+
* Checks if this localTime is same or older (<=) than "now" by X units.
|
|
384
|
+
*/
|
|
385
|
+
isSameOrOlderThan(n, unit, now) {
|
|
386
|
+
return this.isSameOrBefore(LocalTime.of(now !== null && now !== void 0 ? now : new Date()).plus(-n, unit));
|
|
381
387
|
}
|
|
382
388
|
/**
|
|
383
|
-
* Checks if this localTime is younger than "now" by X units.
|
|
389
|
+
* Checks if this localTime is younger (>) than "now" by X units.
|
|
384
390
|
*
|
|
385
391
|
* Example:
|
|
386
392
|
*
|
|
@@ -389,7 +395,13 @@ export class LocalTime {
|
|
|
389
395
|
* Third argument allows to override "now".
|
|
390
396
|
*/
|
|
391
397
|
isYoungerThan(n, unit, now) {
|
|
392
|
-
return
|
|
398
|
+
return this.isAfter(LocalTime.of(now !== null && now !== void 0 ? now : new Date()).plus(-n, unit));
|
|
399
|
+
}
|
|
400
|
+
/**
|
|
401
|
+
* Checks if this localTime is same or younger (>=) than "now" by X units.
|
|
402
|
+
*/
|
|
403
|
+
isSameOrYoungerThan(n, unit, now) {
|
|
404
|
+
return this.isSameOrAfter(LocalTime.of(now !== null && now !== void 0 ? now : new Date()).plus(-n, unit));
|
|
393
405
|
}
|
|
394
406
|
/**
|
|
395
407
|
* Returns 1 if this > d
|
package/dist-esm/is.util.js
CHANGED
|
@@ -35,6 +35,9 @@ export function _isPrimitive(v) {
|
|
|
35
35
|
export function _isEmptyObject(obj) {
|
|
36
36
|
return Object.keys(obj).length === 0;
|
|
37
37
|
}
|
|
38
|
+
export function _isNotEmptyObject(obj) {
|
|
39
|
+
return Object.keys(obj).length > 0;
|
|
40
|
+
}
|
|
38
41
|
/**
|
|
39
42
|
* Object is considered empty if it's one of:
|
|
40
43
|
* undefined
|
|
@@ -32,8 +32,8 @@ export function _clamp(x, minIncl, maxIncl) {
|
|
|
32
32
|
* _sortNumbers([1, 3, 2])
|
|
33
33
|
* // [1, 2, 3]
|
|
34
34
|
*/
|
|
35
|
-
export function _sortNumbers(numbers, mutate = false,
|
|
36
|
-
const mod =
|
|
35
|
+
export function _sortNumbers(numbers, mutate = false, dir = 'asc') {
|
|
36
|
+
const mod = dir === 'desc' ? -1 : 1;
|
|
37
37
|
return (mutate ? numbers : [...numbers]).sort((a, b) => (a - b) * mod);
|
|
38
38
|
}
|
|
39
39
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@naturalcycles/js-lib",
|
|
3
|
-
"version": "14.
|
|
3
|
+
"version": "14.190.0",
|
|
4
4
|
"scripts": {
|
|
5
5
|
"prepare": "husky install",
|
|
6
6
|
"build-prod": "build-prod-esm-cjs",
|
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
"crypto-js": "^4.1.1",
|
|
23
23
|
"jest": "^29.0.0",
|
|
24
24
|
"prettier": "^3.0.0",
|
|
25
|
-
"vitepress": "^1.0.0-rc.
|
|
25
|
+
"vitepress": "^1.0.0-rc.22",
|
|
26
26
|
"vue": "^3.2.45"
|
|
27
27
|
},
|
|
28
28
|
"resolutions": {
|
package/src/array/array.util.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { _isNotNullish } from '../is.util'
|
|
2
|
-
import type { FalsyValue, Mapper, Predicate, StringMap } from '../types'
|
|
2
|
+
import type { FalsyValue, Mapper, Predicate, SortDirection, StringMap } from '../types'
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Creates an array of elements split into groups the length of size. If collection can’t be split evenly, the
|
|
@@ -156,9 +156,9 @@ export function _sortBy<T>(
|
|
|
156
156
|
items: T[],
|
|
157
157
|
mapper: Mapper<T, any>,
|
|
158
158
|
mutate = false,
|
|
159
|
-
|
|
159
|
+
dir: SortDirection = 'asc',
|
|
160
160
|
): T[] {
|
|
161
|
-
const mod =
|
|
161
|
+
const mod = dir === 'desc' ? -1 : 1
|
|
162
162
|
return (mutate ? items : [...items]).sort((_a, _b) => {
|
|
163
163
|
const [a, b] = [_a, _b].map(mapper)
|
|
164
164
|
if (typeof a === 'number' && typeof b === 'number') return (a - b) * mod
|
|
@@ -166,6 +166,13 @@ export function _sortBy<T>(
|
|
|
166
166
|
})
|
|
167
167
|
}
|
|
168
168
|
|
|
169
|
+
/**
|
|
170
|
+
* Alias for _sortBy with descending order.
|
|
171
|
+
*/
|
|
172
|
+
export function _sortDescBy<T>(items: T[], mapper: Mapper<T, any>, mutate = false): T[] {
|
|
173
|
+
return _sortBy(items, mapper, mutate, 'desc')
|
|
174
|
+
}
|
|
175
|
+
|
|
169
176
|
/**
|
|
170
177
|
* Like items.find(), but it tries to find from the END of the array.
|
|
171
178
|
*/
|
|
@@ -3,6 +3,7 @@ import type {
|
|
|
3
3
|
IsoDateString,
|
|
4
4
|
IsoDateTimeString,
|
|
5
5
|
MonthId,
|
|
6
|
+
SortDirection,
|
|
6
7
|
UnixTimestampMillisNumber,
|
|
7
8
|
UnixTimestampNumber,
|
|
8
9
|
} from '../types'
|
|
@@ -112,8 +113,8 @@ export class LocalDate {
|
|
|
112
113
|
return this.fromDateUTC(new Date())
|
|
113
114
|
}
|
|
114
115
|
|
|
115
|
-
static sort(items: LocalDate[], mutate = false,
|
|
116
|
-
const mod =
|
|
116
|
+
static sort(items: LocalDate[], mutate = false, dir: SortDirection = 'asc'): LocalDate[] {
|
|
117
|
+
const mod = dir === 'desc' ? -1 : 1
|
|
117
118
|
return (mutate ? items : [...items]).sort((a, b) => a.cmp(b) * mod)
|
|
118
119
|
}
|
|
119
120
|
|
|
@@ -162,13 +163,13 @@ export class LocalDate {
|
|
|
162
163
|
if (current.isAfter($min, incl[0] === '[')) {
|
|
163
164
|
// ok
|
|
164
165
|
} else {
|
|
165
|
-
current.
|
|
166
|
+
current.plus(1, stepUnit, true)
|
|
166
167
|
}
|
|
167
168
|
|
|
168
169
|
const incl2 = incl[1] === ']'
|
|
169
170
|
while (current.isBefore($max, incl2)) {
|
|
170
171
|
dates.push(current)
|
|
171
|
-
current = current.
|
|
172
|
+
current = current.plus(step, stepUnit)
|
|
172
173
|
}
|
|
173
174
|
|
|
174
175
|
return dates
|
|
@@ -241,7 +242,7 @@ export class LocalDate {
|
|
|
241
242
|
}
|
|
242
243
|
|
|
243
244
|
/**
|
|
244
|
-
* Checks if this localDate is older than "today" by X units.
|
|
245
|
+
* Checks if this localDate is older (<) than "today" by X units.
|
|
245
246
|
*
|
|
246
247
|
* Example:
|
|
247
248
|
*
|
|
@@ -250,11 +251,18 @@ export class LocalDate {
|
|
|
250
251
|
* Third argument allows to override "today".
|
|
251
252
|
*/
|
|
252
253
|
isOlderThan(n: number, unit: LocalDateUnit, today?: LocalDateInput): boolean {
|
|
253
|
-
return this.isBefore(LocalDate.of(today || new Date()).
|
|
254
|
+
return this.isBefore(LocalDate.of(today || new Date()).plus(-n, unit))
|
|
254
255
|
}
|
|
255
256
|
|
|
256
257
|
/**
|
|
257
|
-
* Checks if this localDate is
|
|
258
|
+
* Checks if this localDate is same or older (<=) than "today" by X units.
|
|
259
|
+
*/
|
|
260
|
+
isSameOrOlderThan(n: number, unit: LocalDateUnit, today?: LocalDateInput): boolean {
|
|
261
|
+
return this.isSameOrBefore(LocalDate.of(today || new Date()).plus(-n, unit))
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* Checks if this localDate is younger (>) than "today" by X units.
|
|
258
266
|
*
|
|
259
267
|
* Example:
|
|
260
268
|
*
|
|
@@ -263,7 +271,14 @@ export class LocalDate {
|
|
|
263
271
|
* Third argument allows to override "today".
|
|
264
272
|
*/
|
|
265
273
|
isYoungerThan(n: number, unit: LocalDateUnit, today?: LocalDateInput): boolean {
|
|
266
|
-
return
|
|
274
|
+
return this.isAfter(LocalDate.of(today || new Date()).plus(-n, unit))
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* Checks if this localDate is same or younger (>=) than "today" by X units.
|
|
279
|
+
*/
|
|
280
|
+
isSameOrYoungerThan(n: number, unit: LocalDateUnit, today?: LocalDateInput): boolean {
|
|
281
|
+
return this.isSameOrAfter(LocalDate.of(today || new Date()).plus(-n, unit))
|
|
267
282
|
}
|
|
268
283
|
|
|
269
284
|
/**
|
|
@@ -358,7 +373,7 @@ export class LocalDate {
|
|
|
358
373
|
return days * sign || 0
|
|
359
374
|
}
|
|
360
375
|
|
|
361
|
-
|
|
376
|
+
plus(num: number, unit: LocalDateUnit, mutate = false): LocalDate {
|
|
362
377
|
let { $day, $month, $year } = this
|
|
363
378
|
|
|
364
379
|
if (unit === 'week') {
|
|
@@ -428,22 +443,22 @@ export class LocalDate {
|
|
|
428
443
|
return new LocalDate($year, $month, $day)
|
|
429
444
|
}
|
|
430
445
|
|
|
446
|
+
/**
|
|
447
|
+
* @deprecated use `minus` instead
|
|
448
|
+
*/
|
|
431
449
|
subtract(num: number, unit: LocalDateUnit, mutate = false): LocalDate {
|
|
432
|
-
return this.
|
|
450
|
+
return this.plus(-num, unit, mutate)
|
|
433
451
|
}
|
|
434
452
|
|
|
435
|
-
/**
|
|
436
|
-
* Alias to subtract
|
|
437
|
-
*/
|
|
438
453
|
minus(num: number, unit: LocalDateUnit, mutate = false): LocalDate {
|
|
439
|
-
return this.
|
|
454
|
+
return this.plus(-num, unit, mutate)
|
|
440
455
|
}
|
|
441
456
|
|
|
442
457
|
/**
|
|
443
|
-
*
|
|
458
|
+
* @deprecated use `plus` instead
|
|
444
459
|
*/
|
|
445
|
-
|
|
446
|
-
return this.
|
|
460
|
+
add(num: number, unit: LocalDateUnit, mutate = false): LocalDate {
|
|
461
|
+
return this.plus(num, unit, mutate)
|
|
447
462
|
}
|
|
448
463
|
|
|
449
464
|
startOf(unit: LocalDateUnitStrict): LocalDate {
|
|
@@ -4,6 +4,7 @@ import type {
|
|
|
4
4
|
IsoDateString,
|
|
5
5
|
IsoDateTimeString,
|
|
6
6
|
MonthId,
|
|
7
|
+
SortDirection,
|
|
7
8
|
UnixTimestampMillisNumber,
|
|
8
9
|
UnixTimestampNumber,
|
|
9
10
|
} from '../types'
|
|
@@ -223,7 +224,7 @@ export class LocalTime {
|
|
|
223
224
|
|
|
224
225
|
_assert(VALID_DAYS_OF_WEEK.has(v), `Invalid dayOfWeek: ${v}`)
|
|
225
226
|
|
|
226
|
-
return this.
|
|
227
|
+
return this.plus(v - dow, 'day')
|
|
227
228
|
}
|
|
228
229
|
hour(): number
|
|
229
230
|
hour(v: number): LocalTime
|
|
@@ -266,7 +267,7 @@ export class LocalTime {
|
|
|
266
267
|
return mutate ? this : new LocalTime(d)
|
|
267
268
|
}
|
|
268
269
|
|
|
269
|
-
|
|
270
|
+
plus(num: number, unit: LocalTimeUnit, mutate = false): LocalTime {
|
|
270
271
|
if (unit === 'week') {
|
|
271
272
|
num *= 7
|
|
272
273
|
unit = 'day'
|
|
@@ -280,22 +281,22 @@ export class LocalTime {
|
|
|
280
281
|
return this.set(unit, this.get(unit) + num, mutate)
|
|
281
282
|
}
|
|
282
283
|
|
|
284
|
+
/**
|
|
285
|
+
* @deprecated use `minus` instead
|
|
286
|
+
*/
|
|
283
287
|
subtract(num: number, unit: LocalTimeUnit, mutate = false): LocalTime {
|
|
284
|
-
return this.
|
|
288
|
+
return this.plus(num * -1, unit, mutate)
|
|
285
289
|
}
|
|
286
290
|
|
|
287
|
-
/**
|
|
288
|
-
* Alias to subtract.
|
|
289
|
-
*/
|
|
290
291
|
minus(num: number, unit: LocalTimeUnit, mutate = false): LocalTime {
|
|
291
|
-
return this.
|
|
292
|
+
return this.plus(num * -1, unit, mutate)
|
|
292
293
|
}
|
|
293
294
|
|
|
294
295
|
/**
|
|
295
|
-
*
|
|
296
|
+
* @deprecated use `plus` instead
|
|
296
297
|
*/
|
|
297
|
-
|
|
298
|
-
return this.
|
|
298
|
+
add(num: number, unit: LocalTimeUnit, mutate = false): LocalTime {
|
|
299
|
+
return this.plus(num, unit, mutate)
|
|
299
300
|
}
|
|
300
301
|
|
|
301
302
|
absDiff(other: LocalTimeInput, unit: LocalTimeUnit): number {
|
|
@@ -389,8 +390,8 @@ export class LocalTime {
|
|
|
389
390
|
return mutate ? this : new LocalTime(d)
|
|
390
391
|
}
|
|
391
392
|
|
|
392
|
-
static sort(items: LocalTime[], mutate = false,
|
|
393
|
-
const mod =
|
|
393
|
+
static sort(items: LocalTime[], mutate = false, dir: SortDirection = 'asc'): LocalTime[] {
|
|
394
|
+
const mod = dir === 'desc' ? -1 : 1
|
|
394
395
|
return (mutate ? items : [...items]).sort((a, b) => {
|
|
395
396
|
const v1 = a.$date.valueOf()
|
|
396
397
|
const v2 = b.$date.valueOf()
|
|
@@ -455,7 +456,7 @@ export class LocalTime {
|
|
|
455
456
|
}
|
|
456
457
|
|
|
457
458
|
/**
|
|
458
|
-
* Checks if this localTime is older than "now" by X units.
|
|
459
|
+
* Checks if this localTime is older (<) than "now" by X units.
|
|
459
460
|
*
|
|
460
461
|
* Example:
|
|
461
462
|
*
|
|
@@ -464,11 +465,18 @@ export class LocalTime {
|
|
|
464
465
|
* Third argument allows to override "now".
|
|
465
466
|
*/
|
|
466
467
|
isOlderThan(n: number, unit: LocalTimeUnit, now?: LocalTimeInput): boolean {
|
|
467
|
-
return this.isBefore(LocalTime.of(now ?? new Date()).
|
|
468
|
+
return this.isBefore(LocalTime.of(now ?? new Date()).plus(-n, unit))
|
|
468
469
|
}
|
|
469
470
|
|
|
470
471
|
/**
|
|
471
|
-
* Checks if this localTime is
|
|
472
|
+
* Checks if this localTime is same or older (<=) than "now" by X units.
|
|
473
|
+
*/
|
|
474
|
+
isSameOrOlderThan(n: number, unit: LocalTimeUnit, now?: LocalTimeInput): boolean {
|
|
475
|
+
return this.isSameOrBefore(LocalTime.of(now ?? new Date()).plus(-n, unit))
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
/**
|
|
479
|
+
* Checks if this localTime is younger (>) than "now" by X units.
|
|
472
480
|
*
|
|
473
481
|
* Example:
|
|
474
482
|
*
|
|
@@ -477,7 +485,14 @@ export class LocalTime {
|
|
|
477
485
|
* Third argument allows to override "now".
|
|
478
486
|
*/
|
|
479
487
|
isYoungerThan(n: number, unit: LocalTimeUnit, now?: LocalTimeInput): boolean {
|
|
480
|
-
return
|
|
488
|
+
return this.isAfter(LocalTime.of(now ?? new Date()).plus(-n, unit))
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
/**
|
|
492
|
+
* Checks if this localTime is same or younger (>=) than "now" by X units.
|
|
493
|
+
*/
|
|
494
|
+
isSameOrYoungerThan(n: number, unit: LocalTimeUnit, now?: LocalTimeInput): boolean {
|
|
495
|
+
return this.isSameOrAfter(LocalTime.of(now ?? new Date()).plus(-n, unit))
|
|
481
496
|
}
|
|
482
497
|
|
|
483
498
|
/**
|
package/src/is.util.ts
CHANGED
|
@@ -49,6 +49,10 @@ export function _isEmptyObject(obj: AnyObject): boolean {
|
|
|
49
49
|
return Object.keys(obj).length === 0
|
|
50
50
|
}
|
|
51
51
|
|
|
52
|
+
export function _isNotEmptyObject(obj: AnyObject): boolean {
|
|
53
|
+
return Object.keys(obj).length > 0
|
|
54
|
+
}
|
|
55
|
+
|
|
52
56
|
/**
|
|
53
57
|
* Object is considered empty if it's one of:
|
|
54
58
|
* undefined
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { SortDirection } from '../types'
|
|
2
|
+
|
|
1
3
|
export function _randomInt(minIncl: number, maxIncl: number): number {
|
|
2
4
|
return Math.floor(Math.random() * (maxIncl - minIncl + 1) + minIncl)
|
|
3
5
|
}
|
|
@@ -37,8 +39,12 @@ export function _clamp(x: number, minIncl: number, maxIncl: number): number {
|
|
|
37
39
|
* _sortNumbers([1, 3, 2])
|
|
38
40
|
* // [1, 2, 3]
|
|
39
41
|
*/
|
|
40
|
-
export function _sortNumbers(
|
|
41
|
-
|
|
42
|
+
export function _sortNumbers(
|
|
43
|
+
numbers: number[],
|
|
44
|
+
mutate = false,
|
|
45
|
+
dir: SortDirection = 'asc',
|
|
46
|
+
): number[] {
|
|
47
|
+
const mod = dir === 'desc' ? -1 : 1
|
|
42
48
|
return (mutate ? numbers : [...numbers]).sort((a, b) => (a - b) * mod)
|
|
43
49
|
}
|
|
44
50
|
|
package/src/types.ts
CHANGED
|
@@ -324,3 +324,5 @@ export const _objectAssign = Object.assign as <T extends AnyObject>(
|
|
|
324
324
|
* Functions like pTry use that.
|
|
325
325
|
*/
|
|
326
326
|
export type ErrorDataTuple<T = unknown, ERR = Error> = [err: null, data: T] | [err: ERR, data: null]
|
|
327
|
+
|
|
328
|
+
export type SortDirection = 'asc' | 'desc'
|