@naturalcycles/js-lib 14.188.1 → 14.189.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 +18 -6
- package/dist/datetime/localDate.js +23 -5
- package/dist/datetime/localTime.d.ts +16 -4
- package/dist/datetime/localTime.js +23 -5
- 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 +23 -5
- package/dist-esm/datetime/localTime.js +23 -5
- package/dist-esm/is.util.js +3 -0
- package/dist-esm/number/number.util.js +2 -2
- package/package.json +1 -1
- package/src/array/array.util.ts +10 -3
- package/src/datetime/localDate.ts +29 -7
- package/src/datetime/localTime.ts +27 -5
- 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
|
*
|
|
@@ -58,9 +58,13 @@ export declare class LocalDate {
|
|
|
58
58
|
*
|
|
59
59
|
* Third argument allows to override "today".
|
|
60
60
|
*/
|
|
61
|
-
isOlderThan(n: number, unit:
|
|
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
|
*
|
|
@@ -68,7 +72,11 @@ export declare class LocalDate {
|
|
|
68
72
|
*
|
|
69
73
|
* Third argument allows to override "today".
|
|
70
74
|
*/
|
|
71
|
-
isYoungerThan(n: number, unit:
|
|
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
|
|
@@ -91,6 +99,10 @@ export declare class LocalDate {
|
|
|
91
99
|
* Alias to subtract
|
|
92
100
|
*/
|
|
93
101
|
minus(num: number, unit: LocalDateUnit, mutate?: boolean): LocalDate;
|
|
102
|
+
/**
|
|
103
|
+
* Alias to add
|
|
104
|
+
*/
|
|
105
|
+
plus(num: number, unit: LocalDateUnit, mutate?: boolean): LocalDate;
|
|
94
106
|
startOf(unit: LocalDateUnitStrict): LocalDate;
|
|
95
107
|
endOf(unit: LocalDateUnitStrict): LocalDate;
|
|
96
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) {
|
|
@@ -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
|
*
|
|
@@ -192,7 +192,13 @@ class LocalDate {
|
|
|
192
192
|
return this.isBefore(LocalDate.of(today || new Date()).add(-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()).add(-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()).add(-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()).add(-n, unit));
|
|
205
217
|
}
|
|
206
218
|
/**
|
|
207
219
|
* Returns 1 if this > d
|
|
@@ -358,6 +370,12 @@ class LocalDate {
|
|
|
358
370
|
minus(num, unit, mutate = false) {
|
|
359
371
|
return this.add(-num, unit, mutate);
|
|
360
372
|
}
|
|
373
|
+
/**
|
|
374
|
+
* Alias to add
|
|
375
|
+
*/
|
|
376
|
+
plus(num, unit, mutate = false) {
|
|
377
|
+
return this.add(num, unit, mutate);
|
|
378
|
+
}
|
|
361
379
|
startOf(unit) {
|
|
362
380
|
if (unit === 'day')
|
|
363
381
|
return this;
|
|
@@ -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';
|
|
@@ -76,11 +76,15 @@ export declare class LocalTime {
|
|
|
76
76
|
* Alias to subtract.
|
|
77
77
|
*/
|
|
78
78
|
minus(num: number, unit: LocalTimeUnit, mutate?: boolean): LocalTime;
|
|
79
|
+
/**
|
|
80
|
+
* Alias to add.
|
|
81
|
+
*/
|
|
82
|
+
plus(num: number, unit: LocalTimeUnit, mutate?: boolean): LocalTime;
|
|
79
83
|
absDiff(other: LocalTimeInput, unit: LocalTimeUnit): number;
|
|
80
84
|
diff(other: LocalTimeInput, unit: LocalTimeUnit): number;
|
|
81
85
|
startOf(unit: LocalTimeUnit, mutate?: boolean): LocalTime;
|
|
82
86
|
endOf(unit: LocalTimeUnit, mutate?: boolean): LocalTime;
|
|
83
|
-
static sort(items: LocalTime[], mutate?: boolean,
|
|
87
|
+
static sort(items: LocalTime[], mutate?: boolean, dir?: SortDirection): LocalTime[];
|
|
84
88
|
static earliestOrUndefined(items: LocalTimeInput[]): LocalTime | undefined;
|
|
85
89
|
static earliest(items: LocalTimeInput[]): LocalTime;
|
|
86
90
|
static latestOrUndefined(items: LocalTimeInput[]): LocalTime | undefined;
|
|
@@ -92,7 +96,7 @@ export declare class LocalTime {
|
|
|
92
96
|
isSameOrAfter(d: LocalTimeInput): boolean;
|
|
93
97
|
isBetween(min: LocalTimeInput, max: LocalTimeInput, incl?: Inclusiveness): boolean;
|
|
94
98
|
/**
|
|
95
|
-
* Checks if this localTime is older than "now" by X units.
|
|
99
|
+
* Checks if this localTime is older (<) than "now" by X units.
|
|
96
100
|
*
|
|
97
101
|
* Example:
|
|
98
102
|
*
|
|
@@ -102,7 +106,11 @@ export declare class LocalTime {
|
|
|
102
106
|
*/
|
|
103
107
|
isOlderThan(n: number, unit: LocalTimeUnit, now?: LocalTimeInput): boolean;
|
|
104
108
|
/**
|
|
105
|
-
* 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.
|
|
106
114
|
*
|
|
107
115
|
* Example:
|
|
108
116
|
*
|
|
@@ -111,6 +119,10 @@ export declare class LocalTime {
|
|
|
111
119
|
* Third argument allows to override "now".
|
|
112
120
|
*/
|
|
113
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;
|
|
114
126
|
/**
|
|
115
127
|
* Returns 1 if this > d
|
|
116
128
|
* returns 0 if they are equal
|
|
@@ -220,6 +220,12 @@ class LocalTime {
|
|
|
220
220
|
minus(num, unit, mutate = false) {
|
|
221
221
|
return this.add(num * -1, unit, mutate);
|
|
222
222
|
}
|
|
223
|
+
/**
|
|
224
|
+
* Alias to add.
|
|
225
|
+
*/
|
|
226
|
+
plus(num, unit, mutate = false) {
|
|
227
|
+
return this.add(num, unit, mutate);
|
|
228
|
+
}
|
|
223
229
|
absDiff(other, unit) {
|
|
224
230
|
return Math.abs(this.diff(other, unit));
|
|
225
231
|
}
|
|
@@ -308,8 +314,8 @@ class LocalTime {
|
|
|
308
314
|
}
|
|
309
315
|
return mutate ? this : new LocalTime(d);
|
|
310
316
|
}
|
|
311
|
-
static sort(items, mutate = false,
|
|
312
|
-
const mod =
|
|
317
|
+
static sort(items, mutate = false, dir = 'asc') {
|
|
318
|
+
const mod = dir === 'desc' ? -1 : 1;
|
|
313
319
|
return (mutate ? items : [...items]).sort((a, b) => {
|
|
314
320
|
const v1 = a.$date.valueOf();
|
|
315
321
|
const v2 = b.$date.valueOf();
|
|
@@ -364,7 +370,7 @@ class LocalTime {
|
|
|
364
370
|
return true;
|
|
365
371
|
}
|
|
366
372
|
/**
|
|
367
|
-
* Checks if this localTime is older than "now" by X units.
|
|
373
|
+
* Checks if this localTime is older (<) than "now" by X units.
|
|
368
374
|
*
|
|
369
375
|
* Example:
|
|
370
376
|
*
|
|
@@ -376,7 +382,13 @@ class LocalTime {
|
|
|
376
382
|
return this.isBefore(LocalTime.of(now ?? new Date()).add(-n, unit));
|
|
377
383
|
}
|
|
378
384
|
/**
|
|
379
|
-
* Checks if this localTime is
|
|
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()).add(-n, unit));
|
|
389
|
+
}
|
|
390
|
+
/**
|
|
391
|
+
* Checks if this localTime is younger (>) than "now" by X units.
|
|
380
392
|
*
|
|
381
393
|
* Example:
|
|
382
394
|
*
|
|
@@ -385,7 +397,13 @@ class LocalTime {
|
|
|
385
397
|
* Third argument allows to override "now".
|
|
386
398
|
*/
|
|
387
399
|
isYoungerThan(n, unit, now) {
|
|
388
|
-
return
|
|
400
|
+
return this.isAfter(LocalTime.of(now ?? new Date()).add(-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()).add(-n, unit));
|
|
389
407
|
}
|
|
390
408
|
/**
|
|
391
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) {
|
|
@@ -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
|
*
|
|
@@ -189,7 +189,13 @@ export class LocalDate {
|
|
|
189
189
|
return this.isBefore(LocalDate.of(today || new Date()).add(-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()).add(-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()).add(-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()).add(-n, unit));
|
|
202
214
|
}
|
|
203
215
|
/**
|
|
204
216
|
* Returns 1 if this > d
|
|
@@ -355,6 +367,12 @@ export class LocalDate {
|
|
|
355
367
|
minus(num, unit, mutate = false) {
|
|
356
368
|
return this.add(-num, unit, mutate);
|
|
357
369
|
}
|
|
370
|
+
/**
|
|
371
|
+
* Alias to add
|
|
372
|
+
*/
|
|
373
|
+
plus(num, unit, mutate = false) {
|
|
374
|
+
return this.add(num, unit, mutate);
|
|
375
|
+
}
|
|
358
376
|
startOf(unit) {
|
|
359
377
|
if (unit === 'day')
|
|
360
378
|
return this;
|
|
@@ -218,6 +218,12 @@ export class LocalTime {
|
|
|
218
218
|
minus(num, unit, mutate = false) {
|
|
219
219
|
return this.add(num * -1, unit, mutate);
|
|
220
220
|
}
|
|
221
|
+
/**
|
|
222
|
+
* Alias to add.
|
|
223
|
+
*/
|
|
224
|
+
plus(num, unit, mutate = false) {
|
|
225
|
+
return this.add(num, unit, mutate);
|
|
226
|
+
}
|
|
221
227
|
absDiff(other, unit) {
|
|
222
228
|
return Math.abs(this.diff(other, unit));
|
|
223
229
|
}
|
|
@@ -306,8 +312,8 @@ export class LocalTime {
|
|
|
306
312
|
}
|
|
307
313
|
return mutate ? this : new LocalTime(d);
|
|
308
314
|
}
|
|
309
|
-
static sort(items, mutate = false,
|
|
310
|
-
const mod =
|
|
315
|
+
static sort(items, mutate = false, dir = 'asc') {
|
|
316
|
+
const mod = dir === 'desc' ? -1 : 1;
|
|
311
317
|
return (mutate ? items : [...items]).sort((a, b) => {
|
|
312
318
|
const v1 = a.$date.valueOf();
|
|
313
319
|
const v2 = b.$date.valueOf();
|
|
@@ -362,7 +368,7 @@ export class LocalTime {
|
|
|
362
368
|
return true;
|
|
363
369
|
}
|
|
364
370
|
/**
|
|
365
|
-
* Checks if this localTime is older than "now" by X units.
|
|
371
|
+
* Checks if this localTime is older (<) than "now" by X units.
|
|
366
372
|
*
|
|
367
373
|
* Example:
|
|
368
374
|
*
|
|
@@ -374,7 +380,13 @@ export class LocalTime {
|
|
|
374
380
|
return this.isBefore(LocalTime.of(now !== null && now !== void 0 ? now : new Date()).add(-n, unit));
|
|
375
381
|
}
|
|
376
382
|
/**
|
|
377
|
-
* Checks if this localTime is
|
|
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()).add(-n, unit));
|
|
387
|
+
}
|
|
388
|
+
/**
|
|
389
|
+
* Checks if this localTime is younger (>) than "now" by X units.
|
|
378
390
|
*
|
|
379
391
|
* Example:
|
|
380
392
|
*
|
|
@@ -383,7 +395,13 @@ export class LocalTime {
|
|
|
383
395
|
* Third argument allows to override "now".
|
|
384
396
|
*/
|
|
385
397
|
isYoungerThan(n, unit, now) {
|
|
386
|
-
return
|
|
398
|
+
return this.isAfter(LocalTime.of(now !== null && now !== void 0 ? now : new Date()).add(-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()).add(-n, unit));
|
|
387
405
|
}
|
|
388
406
|
/**
|
|
389
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
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
|
|
|
@@ -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
|
*
|
|
@@ -249,12 +250,19 @@ export class LocalDate {
|
|
|
249
250
|
*
|
|
250
251
|
* Third argument allows to override "today".
|
|
251
252
|
*/
|
|
252
|
-
isOlderThan(n: number, unit:
|
|
253
|
+
isOlderThan(n: number, unit: LocalDateUnit, today?: LocalDateInput): boolean {
|
|
253
254
|
return this.isBefore(LocalDate.of(today || new Date()).add(-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()).add(-n, unit))
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* Checks if this localDate is younger (>) than "today" by X units.
|
|
258
266
|
*
|
|
259
267
|
* Example:
|
|
260
268
|
*
|
|
@@ -262,8 +270,15 @@ export class LocalDate {
|
|
|
262
270
|
*
|
|
263
271
|
* Third argument allows to override "today".
|
|
264
272
|
*/
|
|
265
|
-
isYoungerThan(n: number, unit:
|
|
266
|
-
return
|
|
273
|
+
isYoungerThan(n: number, unit: LocalDateUnit, today?: LocalDateInput): boolean {
|
|
274
|
+
return this.isAfter(LocalDate.of(today || new Date()).add(-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()).add(-n, unit))
|
|
267
282
|
}
|
|
268
283
|
|
|
269
284
|
/**
|
|
@@ -439,6 +454,13 @@ export class LocalDate {
|
|
|
439
454
|
return this.add(-num, unit, mutate)
|
|
440
455
|
}
|
|
441
456
|
|
|
457
|
+
/**
|
|
458
|
+
* Alias to add
|
|
459
|
+
*/
|
|
460
|
+
plus(num: number, unit: LocalDateUnit, mutate = false): LocalDate {
|
|
461
|
+
return this.add(num, unit, mutate)
|
|
462
|
+
}
|
|
463
|
+
|
|
442
464
|
startOf(unit: LocalDateUnitStrict): LocalDate {
|
|
443
465
|
if (unit === 'day') return this
|
|
444
466
|
if (unit === 'month') return LocalDate.create(this.$year, this.$month, 1)
|
|
@@ -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'
|
|
@@ -291,6 +292,13 @@ export class LocalTime {
|
|
|
291
292
|
return this.add(num * -1, unit, mutate)
|
|
292
293
|
}
|
|
293
294
|
|
|
295
|
+
/**
|
|
296
|
+
* Alias to add.
|
|
297
|
+
*/
|
|
298
|
+
plus(num: number, unit: LocalTimeUnit, mutate = false): LocalTime {
|
|
299
|
+
return this.add(num, unit, mutate)
|
|
300
|
+
}
|
|
301
|
+
|
|
294
302
|
absDiff(other: LocalTimeInput, unit: LocalTimeUnit): number {
|
|
295
303
|
return Math.abs(this.diff(other, unit))
|
|
296
304
|
}
|
|
@@ -382,8 +390,8 @@ export class LocalTime {
|
|
|
382
390
|
return mutate ? this : new LocalTime(d)
|
|
383
391
|
}
|
|
384
392
|
|
|
385
|
-
static sort(items: LocalTime[], mutate = false,
|
|
386
|
-
const mod =
|
|
393
|
+
static sort(items: LocalTime[], mutate = false, dir: SortDirection = 'asc'): LocalTime[] {
|
|
394
|
+
const mod = dir === 'desc' ? -1 : 1
|
|
387
395
|
return (mutate ? items : [...items]).sort((a, b) => {
|
|
388
396
|
const v1 = a.$date.valueOf()
|
|
389
397
|
const v2 = b.$date.valueOf()
|
|
@@ -448,7 +456,7 @@ export class LocalTime {
|
|
|
448
456
|
}
|
|
449
457
|
|
|
450
458
|
/**
|
|
451
|
-
* Checks if this localTime is older than "now" by X units.
|
|
459
|
+
* Checks if this localTime is older (<) than "now" by X units.
|
|
452
460
|
*
|
|
453
461
|
* Example:
|
|
454
462
|
*
|
|
@@ -461,7 +469,14 @@ export class LocalTime {
|
|
|
461
469
|
}
|
|
462
470
|
|
|
463
471
|
/**
|
|
464
|
-
* 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()).add(-n, unit))
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
/**
|
|
479
|
+
* Checks if this localTime is younger (>) than "now" by X units.
|
|
465
480
|
*
|
|
466
481
|
* Example:
|
|
467
482
|
*
|
|
@@ -470,7 +485,14 @@ export class LocalTime {
|
|
|
470
485
|
* Third argument allows to override "now".
|
|
471
486
|
*/
|
|
472
487
|
isYoungerThan(n: number, unit: LocalTimeUnit, now?: LocalTimeInput): boolean {
|
|
473
|
-
return
|
|
488
|
+
return this.isAfter(LocalTime.of(now ?? new Date()).add(-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()).add(-n, unit))
|
|
474
496
|
}
|
|
475
497
|
|
|
476
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'
|