@naturalcycles/js-lib 14.188.2 → 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 +12 -4
- package/dist/datetime/localDate.js +17 -5
- package/dist/datetime/localTime.d.ts +12 -4
- package/dist/datetime/localTime.js +17 -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 +17 -5
- package/dist-esm/datetime/localTime.js +17 -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 +20 -5
- package/src/datetime/localTime.ts +20 -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
|
*
|
|
@@ -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
|
|
@@ -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
|
|
@@ -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';
|
|
@@ -84,7 +84,7 @@ export declare class LocalTime {
|
|
|
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
|
|
@@ -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
|
*
|
|
@@ -382,7 +382,13 @@ class LocalTime {
|
|
|
382
382
|
return this.isBefore(LocalTime.of(now ?? new Date()).add(-n, unit));
|
|
383
383
|
}
|
|
384
384
|
/**
|
|
385
|
-
* 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.
|
|
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()).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));
|
|
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) {
|
|
@@ -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
|
|
@@ -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
|
*
|
|
@@ -380,7 +380,13 @@ export class LocalTime {
|
|
|
380
380
|
return this.isBefore(LocalTime.of(now !== null && now !== void 0 ? now : new Date()).add(-n, unit));
|
|
381
381
|
}
|
|
382
382
|
/**
|
|
383
|
-
* 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.
|
|
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()).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));
|
|
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
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
|
*
|
|
@@ -254,7 +255,14 @@ export class LocalDate {
|
|
|
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
|
*
|
|
@@ -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()).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
|
/**
|
|
@@ -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'
|
|
@@ -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
|
*
|
|
@@ -468,7 +469,14 @@ export class LocalTime {
|
|
|
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()).add(-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()).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))
|
|
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'
|