@naturalcycles/js-lib 14.225.0 → 14.227.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 +23 -26
- package/dist/array/array.util.js +24 -15
- package/dist/datetime/localDate.d.ts +0 -8
- package/dist/datetime/localDate.js +0 -12
- package/dist/datetime/localTime.d.ts +0 -8
- package/dist/datetime/localTime.js +0 -12
- package/dist/promise/pRetry.js +0 -1
- package/dist/string/stringify.d.ts +0 -4
- package/dist/string/stringify.js +1 -5
- package/dist-esm/array/array.util.js +24 -15
- package/dist-esm/datetime/localDate.js +0 -12
- package/dist-esm/datetime/localTime.js +0 -12
- package/dist-esm/promise/pRetry.js +0 -1
- package/dist-esm/string/stringify.js +0 -4
- package/package.json +1 -1
- package/src/array/array.util.ts +53 -38
- package/src/datetime/localDate.ts +0 -14
- package/src/datetime/localTime.ts +0 -14
- package/src/promise/pRetry.ts +0 -1
- package/src/string/stringify.ts +0 -5
|
@@ -107,7 +107,7 @@ export declare function _sortDescBy<T>(items: T[], mapper: Mapper<T, any>, mutat
|
|
|
107
107
|
*
|
|
108
108
|
* Use `Array.find` if you don't need to stop the iteration early.
|
|
109
109
|
*/
|
|
110
|
-
export declare function _find<T>(items: T[], predicate: AbortablePredicate<T>): T | undefined;
|
|
110
|
+
export declare function _find<T>(items: readonly T[], predicate: AbortablePredicate<T>): T | undefined;
|
|
111
111
|
/**
|
|
112
112
|
* Similar to `Array.findLast`, but the `predicate` may return `END` to stop the iteration early.
|
|
113
113
|
*
|
|
@@ -115,21 +115,18 @@ export declare function _find<T>(items: T[], predicate: AbortablePredicate<T>):
|
|
|
115
115
|
* - in Node since 18+
|
|
116
116
|
* - in iOS Safari since 15.4
|
|
117
117
|
*/
|
|
118
|
-
export declare function _findLast<T>(items: T[], predicate: AbortablePredicate<T>): T | undefined;
|
|
119
|
-
export declare function _takeWhile<T>(items: T[], predicate: Predicate<T>): T[];
|
|
120
|
-
export declare function _takeRightWhile<T>(items: T[], predicate: Predicate<T>): T[];
|
|
121
|
-
export declare function _dropWhile<T>(items: T[], predicate: Predicate<T>): T[];
|
|
122
|
-
export declare function _dropRightWhile<T>(items: T[], predicate: Predicate<T>): T[];
|
|
118
|
+
export declare function _findLast<T>(items: readonly T[], predicate: AbortablePredicate<T>): T | undefined;
|
|
119
|
+
export declare function _takeWhile<T>(items: readonly T[], predicate: Predicate<T>): T[];
|
|
120
|
+
export declare function _takeRightWhile<T>(items: readonly T[], predicate: Predicate<T>): T[];
|
|
121
|
+
export declare function _dropWhile<T>(items: readonly T[], predicate: Predicate<T>): T[];
|
|
122
|
+
export declare function _dropRightWhile<T>(items: readonly T[], predicate: Predicate<T>): T[];
|
|
123
123
|
/**
|
|
124
124
|
* Counts how many items match the predicate.
|
|
125
125
|
*
|
|
126
126
|
* `limit` allows to exit early when limit count is reached, skipping further iterations (perf optimization).
|
|
127
127
|
*/
|
|
128
|
-
export declare function _count<T>(items: T
|
|
129
|
-
|
|
130
|
-
* `limit` allows to exit early when limit count is reached, skipping further iterations (perf optimization).
|
|
131
|
-
*/
|
|
132
|
-
export declare function _countBy<T>(items: T[], mapper: Mapper<T, any>): StringMap<number>;
|
|
128
|
+
export declare function _count<T>(items: Iterable<T>, predicate: AbortablePredicate<T>, limit?: number): number;
|
|
129
|
+
export declare function _countBy<T>(items: Iterable<T>, mapper: Mapper<T, any>): StringMap<number>;
|
|
133
130
|
/**
|
|
134
131
|
* Returns an intersection between 2 arrays.
|
|
135
132
|
*
|
|
@@ -160,8 +157,8 @@ export declare function _difference<T>(source: T[], ...diffs: T[][]): T[];
|
|
|
160
157
|
/**
|
|
161
158
|
* Returns the sum of items, or 0 for empty array.
|
|
162
159
|
*/
|
|
163
|
-
export declare function _sum(items: number
|
|
164
|
-
export declare function _sumBy<T>(items: T
|
|
160
|
+
export declare function _sum(items: Iterable<number>): number;
|
|
161
|
+
export declare function _sumBy<T>(items: Iterable<T>, mapper: Mapper<T, number | undefined>): number;
|
|
165
162
|
/**
|
|
166
163
|
* Map an array of T to a StringMap<V>,
|
|
167
164
|
* by returning a tuple of [key, value] from a mapper function.
|
|
@@ -175,7 +172,7 @@ export declare function _sumBy<T>(items: T[], mapper: Mapper<T, number | undefin
|
|
|
175
172
|
* _mapToObject([1, 2, 3], n => [n, `id${n}`])
|
|
176
173
|
* // { '1': 'id1, '2': 'id2', '3': 'id3' }
|
|
177
174
|
*/
|
|
178
|
-
export declare function _mapToObject<T, V>(array: T
|
|
175
|
+
export declare function _mapToObject<T, V>(array: Iterable<T>, mapper: (item: T) => [key: any, value: V] | FalsyValue): StringMap<V>;
|
|
179
176
|
/**
|
|
180
177
|
* Randomly shuffle an array values.
|
|
181
178
|
* Fisher–Yates algorithm.
|
|
@@ -186,28 +183,28 @@ export declare function _shuffle<T>(array: T[], mutate?: boolean): T[];
|
|
|
186
183
|
* Returns last item of non-empty array.
|
|
187
184
|
* Throws if array is empty.
|
|
188
185
|
*/
|
|
189
|
-
export declare function _last<T>(array: T[]): T;
|
|
186
|
+
export declare function _last<T>(array: readonly T[]): T;
|
|
190
187
|
/**
|
|
191
188
|
* Returns last item of the array (or undefined if array is empty).
|
|
192
189
|
*/
|
|
193
|
-
export declare function _lastOrUndefined<T>(array: T[]): T | undefined;
|
|
190
|
+
export declare function _lastOrUndefined<T>(array: readonly T[]): T | undefined;
|
|
194
191
|
/**
|
|
195
192
|
* Returns the first item of non-empty array.
|
|
196
193
|
* Throws if array is empty.
|
|
197
194
|
*/
|
|
198
|
-
export declare function _first<T>(array: T[]): T;
|
|
199
|
-
export declare function _minOrUndefined<T>(array: T[]): NonNullable<T> | undefined;
|
|
195
|
+
export declare function _first<T>(array: readonly T[]): T;
|
|
196
|
+
export declare function _minOrUndefined<T>(array: readonly T[]): NonNullable<T> | undefined;
|
|
200
197
|
/**
|
|
201
198
|
* Filters out nullish values (undefined and null).
|
|
202
199
|
*/
|
|
203
|
-
export declare function _min<T>(array: T[]): NonNullable<T>;
|
|
204
|
-
export declare function _maxOrUndefined<T>(array: T[]): NonNullable<T> | undefined;
|
|
200
|
+
export declare function _min<T>(array: readonly T[]): NonNullable<T>;
|
|
201
|
+
export declare function _maxOrUndefined<T>(array: readonly T[]): NonNullable<T> | undefined;
|
|
205
202
|
/**
|
|
206
203
|
* Filters out nullish values (undefined and null).
|
|
207
204
|
*/
|
|
208
|
-
export declare function _max<T>(array: T[]): NonNullable<T>;
|
|
209
|
-
export declare function _maxBy<T>(array: T[], mapper: Mapper<T, number | string | undefined>): T;
|
|
210
|
-
export declare function _minBy<T>(array: T[], mapper: Mapper<T, number | string | undefined>): T;
|
|
211
|
-
export declare function _maxByOrUndefined<T>(array: T[], mapper: Mapper<T, number | string | undefined>): T | undefined;
|
|
212
|
-
export declare function _minByOrUndefined<T>(array: T[], mapper: Mapper<T, number | string | undefined>): T | undefined;
|
|
213
|
-
export declare function _zip<T1, T2>(array1: T1[], array2: T2[]): [T1, T2][];
|
|
205
|
+
export declare function _max<T>(array: readonly T[]): NonNullable<T>;
|
|
206
|
+
export declare function _maxBy<T>(array: readonly T[], mapper: Mapper<T, number | string | undefined>): T;
|
|
207
|
+
export declare function _minBy<T>(array: readonly T[], mapper: Mapper<T, number | string | undefined>): T;
|
|
208
|
+
export declare function _maxByOrUndefined<T>(array: readonly T[], mapper: Mapper<T, number | string | undefined>): T | undefined;
|
|
209
|
+
export declare function _minByOrUndefined<T>(array: readonly T[], mapper: Mapper<T, number | string | undefined>): T | undefined;
|
|
210
|
+
export declare function _zip<T1, T2>(array1: readonly T1[], array2: readonly T2[]): [T1, T2][];
|
package/dist/array/array.util.js
CHANGED
|
@@ -229,8 +229,9 @@ function _count(items, predicate, limit) {
|
|
|
229
229
|
if (limit === 0)
|
|
230
230
|
return 0;
|
|
231
231
|
let count = 0;
|
|
232
|
-
|
|
233
|
-
|
|
232
|
+
let i = 0;
|
|
233
|
+
for (const item of items) {
|
|
234
|
+
const r = predicate(item, i++);
|
|
234
235
|
if (r === types_1.END)
|
|
235
236
|
break;
|
|
236
237
|
if (r) {
|
|
@@ -242,15 +243,13 @@ function _count(items, predicate, limit) {
|
|
|
242
243
|
return count;
|
|
243
244
|
}
|
|
244
245
|
exports._count = _count;
|
|
245
|
-
/**
|
|
246
|
-
* `limit` allows to exit early when limit count is reached, skipping further iterations (perf optimization).
|
|
247
|
-
*/
|
|
248
246
|
function _countBy(items, mapper) {
|
|
249
247
|
const map = {};
|
|
250
|
-
|
|
251
|
-
|
|
248
|
+
let i = 0;
|
|
249
|
+
for (const item of items) {
|
|
250
|
+
const key = mapper(item, i++);
|
|
252
251
|
map[key] = (map[key] || 0) + 1;
|
|
253
|
-
}
|
|
252
|
+
}
|
|
254
253
|
return map;
|
|
255
254
|
}
|
|
256
255
|
exports._countBy = _countBy;
|
|
@@ -297,14 +296,24 @@ exports._difference = _difference;
|
|
|
297
296
|
* Returns the sum of items, or 0 for empty array.
|
|
298
297
|
*/
|
|
299
298
|
function _sum(items) {
|
|
300
|
-
|
|
299
|
+
let sum = 0;
|
|
300
|
+
for (const n of items) {
|
|
301
|
+
sum += n;
|
|
302
|
+
}
|
|
303
|
+
return sum;
|
|
301
304
|
}
|
|
302
305
|
exports._sum = _sum;
|
|
303
306
|
function _sumBy(items, mapper) {
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
307
|
+
let sum = 0;
|
|
308
|
+
let i = 0;
|
|
309
|
+
for (const n of items) {
|
|
310
|
+
const v = mapper(n, i++);
|
|
311
|
+
if (typeof v === 'number') {
|
|
312
|
+
// count only numbers, nothing else
|
|
313
|
+
sum += v;
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
return sum;
|
|
308
317
|
}
|
|
309
318
|
exports._sumBy = _sumBy;
|
|
310
319
|
/**
|
|
@@ -376,7 +385,7 @@ function _minOrUndefined(array) {
|
|
|
376
385
|
const a = array.filter(is_util_1._isNotNullish);
|
|
377
386
|
if (!a.length)
|
|
378
387
|
return;
|
|
379
|
-
return
|
|
388
|
+
return a.reduce((min, item) => (min <= item ? min : item));
|
|
380
389
|
}
|
|
381
390
|
exports._minOrUndefined = _minOrUndefined;
|
|
382
391
|
/**
|
|
@@ -393,7 +402,7 @@ function _maxOrUndefined(array) {
|
|
|
393
402
|
const a = array.filter(is_util_1._isNotNullish);
|
|
394
403
|
if (!a.length)
|
|
395
404
|
return;
|
|
396
|
-
return
|
|
405
|
+
return a.reduce((max, item) => (max >= item ? max : item));
|
|
397
406
|
}
|
|
398
407
|
exports._maxOrUndefined = _maxOrUndefined;
|
|
399
408
|
/**
|
|
@@ -94,15 +94,7 @@ export declare class LocalDate {
|
|
|
94
94
|
*/
|
|
95
95
|
diff(d: LocalDateInput, unit: LocalDateUnit): number;
|
|
96
96
|
plus(num: number, unit: LocalDateUnit, mutate?: boolean): LocalDate;
|
|
97
|
-
/**
|
|
98
|
-
* @deprecated use `minus` instead
|
|
99
|
-
*/
|
|
100
|
-
subtract(num: number, unit: LocalDateUnit, mutate?: boolean): LocalDate;
|
|
101
97
|
minus(num: number, unit: LocalDateUnit, mutate?: boolean): LocalDate;
|
|
102
|
-
/**
|
|
103
|
-
* @deprecated use `plus` instead
|
|
104
|
-
*/
|
|
105
|
-
add(num: number, unit: LocalDateUnit, mutate?: boolean): LocalDate;
|
|
106
98
|
startOf(unit: LocalDateUnitStrict): LocalDate;
|
|
107
99
|
endOf(unit: LocalDateUnitStrict): LocalDate;
|
|
108
100
|
static getYearLength(year: number): number;
|
|
@@ -342,21 +342,9 @@ class LocalDate {
|
|
|
342
342
|
}
|
|
343
343
|
return new LocalDate($year, $month, $day);
|
|
344
344
|
}
|
|
345
|
-
/**
|
|
346
|
-
* @deprecated use `minus` instead
|
|
347
|
-
*/
|
|
348
|
-
subtract(num, unit, mutate = false) {
|
|
349
|
-
return this.plus(-num, unit, mutate);
|
|
350
|
-
}
|
|
351
345
|
minus(num, unit, mutate = false) {
|
|
352
346
|
return this.plus(-num, unit, mutate);
|
|
353
347
|
}
|
|
354
|
-
/**
|
|
355
|
-
* @deprecated use `plus` instead
|
|
356
|
-
*/
|
|
357
|
-
add(num, unit, mutate = false) {
|
|
358
|
-
return this.plus(num, unit, mutate);
|
|
359
|
-
}
|
|
360
348
|
startOf(unit) {
|
|
361
349
|
if (unit === 'day')
|
|
362
350
|
return this;
|
|
@@ -70,15 +70,7 @@ export declare class LocalTime {
|
|
|
70
70
|
second(v: number): LocalTime;
|
|
71
71
|
setComponents(c: Partial<LocalTimeComponents>, mutate?: boolean): LocalTime;
|
|
72
72
|
plus(num: number, unit: LocalTimeUnit, mutate?: boolean): LocalTime;
|
|
73
|
-
/**
|
|
74
|
-
* @deprecated use `minus` instead
|
|
75
|
-
*/
|
|
76
|
-
subtract(num: number, unit: LocalTimeUnit, mutate?: boolean): LocalTime;
|
|
77
73
|
minus(num: number, unit: LocalTimeUnit, mutate?: boolean): LocalTime;
|
|
78
|
-
/**
|
|
79
|
-
* @deprecated use `plus` instead
|
|
80
|
-
*/
|
|
81
|
-
add(num: number, unit: LocalTimeUnit, mutate?: boolean): LocalTime;
|
|
82
74
|
absDiff(other: LocalTimeInput, unit: LocalTimeUnit): number;
|
|
83
75
|
diff(other: LocalTimeInput, unit: LocalTimeUnit): number;
|
|
84
76
|
startOf(unit: LocalTimeUnit, mutate?: boolean): LocalTime;
|
|
@@ -211,21 +211,9 @@ class LocalTime {
|
|
|
211
211
|
}
|
|
212
212
|
return this.set(unit, this.get(unit) + num, mutate);
|
|
213
213
|
}
|
|
214
|
-
/**
|
|
215
|
-
* @deprecated use `minus` instead
|
|
216
|
-
*/
|
|
217
|
-
subtract(num, unit, mutate = false) {
|
|
218
|
-
return this.plus(num * -1, unit, mutate);
|
|
219
|
-
}
|
|
220
214
|
minus(num, unit, mutate = false) {
|
|
221
215
|
return this.plus(num * -1, unit, mutate);
|
|
222
216
|
}
|
|
223
|
-
/**
|
|
224
|
-
* @deprecated use `plus` instead
|
|
225
|
-
*/
|
|
226
|
-
add(num, unit, mutate = false) {
|
|
227
|
-
return this.plus(num, unit, mutate);
|
|
228
|
-
}
|
|
229
217
|
absDiff(other, unit) {
|
|
230
218
|
return Math.abs(this.diff(other, unit));
|
|
231
219
|
}
|
package/dist/promise/pRetry.js
CHANGED
|
@@ -65,7 +65,3 @@ export interface StringifyOptions {
|
|
|
65
65
|
* Returns 'undefined' if undefined is passed (default util.inspect behavior).
|
|
66
66
|
*/
|
|
67
67
|
export declare function _stringify(obj: any, opt?: StringifyOptions): string;
|
|
68
|
-
/**
|
|
69
|
-
* @deprecated renamed to _stringify
|
|
70
|
-
*/
|
|
71
|
-
export declare const _stringifyAny: typeof _stringify;
|
package/dist/string/stringify.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports._stringify = exports.setGlobalStringifyFunction = void 0;
|
|
4
4
|
const error_util_1 = require("../error/error.util");
|
|
5
5
|
const json_util_1 = require("./json.util");
|
|
6
6
|
const safeJsonStringify_1 = require("./safeJsonStringify");
|
|
@@ -136,7 +136,3 @@ function _stringify(obj, opt = {}) {
|
|
|
136
136
|
return s;
|
|
137
137
|
}
|
|
138
138
|
exports._stringify = _stringify;
|
|
139
|
-
/**
|
|
140
|
-
* @deprecated renamed to _stringify
|
|
141
|
-
*/
|
|
142
|
-
exports._stringifyAny = _stringify;
|
|
@@ -210,8 +210,9 @@ export function _count(items, predicate, limit) {
|
|
|
210
210
|
if (limit === 0)
|
|
211
211
|
return 0;
|
|
212
212
|
let count = 0;
|
|
213
|
-
|
|
214
|
-
|
|
213
|
+
let i = 0;
|
|
214
|
+
for (const item of items) {
|
|
215
|
+
const r = predicate(item, i++);
|
|
215
216
|
if (r === END)
|
|
216
217
|
break;
|
|
217
218
|
if (r) {
|
|
@@ -222,15 +223,13 @@ export function _count(items, predicate, limit) {
|
|
|
222
223
|
}
|
|
223
224
|
return count;
|
|
224
225
|
}
|
|
225
|
-
/**
|
|
226
|
-
* `limit` allows to exit early when limit count is reached, skipping further iterations (perf optimization).
|
|
227
|
-
*/
|
|
228
226
|
export function _countBy(items, mapper) {
|
|
229
227
|
const map = {};
|
|
230
|
-
|
|
231
|
-
|
|
228
|
+
let i = 0;
|
|
229
|
+
for (const item of items) {
|
|
230
|
+
const key = mapper(item, i++);
|
|
232
231
|
map[key] = (map[key] || 0) + 1;
|
|
233
|
-
}
|
|
232
|
+
}
|
|
234
233
|
return map;
|
|
235
234
|
}
|
|
236
235
|
// investigate: _groupBy
|
|
@@ -273,13 +272,23 @@ export function _difference(source, ...diffs) {
|
|
|
273
272
|
* Returns the sum of items, or 0 for empty array.
|
|
274
273
|
*/
|
|
275
274
|
export function _sum(items) {
|
|
276
|
-
|
|
275
|
+
let sum = 0;
|
|
276
|
+
for (const n of items) {
|
|
277
|
+
sum += n;
|
|
278
|
+
}
|
|
279
|
+
return sum;
|
|
277
280
|
}
|
|
278
281
|
export function _sumBy(items, mapper) {
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
282
|
+
let sum = 0;
|
|
283
|
+
let i = 0;
|
|
284
|
+
for (const n of items) {
|
|
285
|
+
const v = mapper(n, i++);
|
|
286
|
+
if (typeof v === 'number') {
|
|
287
|
+
// count only numbers, nothing else
|
|
288
|
+
sum += v;
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
return sum;
|
|
283
292
|
}
|
|
284
293
|
/**
|
|
285
294
|
* Map an array of T to a StringMap<V>,
|
|
@@ -345,7 +354,7 @@ export function _minOrUndefined(array) {
|
|
|
345
354
|
const a = array.filter(_isNotNullish);
|
|
346
355
|
if (!a.length)
|
|
347
356
|
return;
|
|
348
|
-
return
|
|
357
|
+
return a.reduce((min, item) => (min <= item ? min : item));
|
|
349
358
|
}
|
|
350
359
|
/**
|
|
351
360
|
* Filters out nullish values (undefined and null).
|
|
@@ -360,7 +369,7 @@ export function _maxOrUndefined(array) {
|
|
|
360
369
|
const a = array.filter(_isNotNullish);
|
|
361
370
|
if (!a.length)
|
|
362
371
|
return;
|
|
363
|
-
return
|
|
372
|
+
return a.reduce((max, item) => (max >= item ? max : item));
|
|
364
373
|
}
|
|
365
374
|
/**
|
|
366
375
|
* Filters out nullish values (undefined and null).
|
|
@@ -339,21 +339,9 @@ export class LocalDate {
|
|
|
339
339
|
}
|
|
340
340
|
return new LocalDate($year, $month, $day);
|
|
341
341
|
}
|
|
342
|
-
/**
|
|
343
|
-
* @deprecated use `minus` instead
|
|
344
|
-
*/
|
|
345
|
-
subtract(num, unit, mutate = false) {
|
|
346
|
-
return this.plus(-num, unit, mutate);
|
|
347
|
-
}
|
|
348
342
|
minus(num, unit, mutate = false) {
|
|
349
343
|
return this.plus(-num, unit, mutate);
|
|
350
344
|
}
|
|
351
|
-
/**
|
|
352
|
-
* @deprecated use `plus` instead
|
|
353
|
-
*/
|
|
354
|
-
add(num, unit, mutate = false) {
|
|
355
|
-
return this.plus(num, unit, mutate);
|
|
356
|
-
}
|
|
357
345
|
startOf(unit) {
|
|
358
346
|
if (unit === 'day')
|
|
359
347
|
return this;
|
|
@@ -209,21 +209,9 @@ export class LocalTime {
|
|
|
209
209
|
}
|
|
210
210
|
return this.set(unit, this.get(unit) + num, mutate);
|
|
211
211
|
}
|
|
212
|
-
/**
|
|
213
|
-
* @deprecated use `minus` instead
|
|
214
|
-
*/
|
|
215
|
-
subtract(num, unit, mutate = false) {
|
|
216
|
-
return this.plus(num * -1, unit, mutate);
|
|
217
|
-
}
|
|
218
212
|
minus(num, unit, mutate = false) {
|
|
219
213
|
return this.plus(num * -1, unit, mutate);
|
|
220
214
|
}
|
|
221
|
-
/**
|
|
222
|
-
* @deprecated use `plus` instead
|
|
223
|
-
*/
|
|
224
|
-
add(num, unit, mutate = false) {
|
|
225
|
-
return this.plus(num, unit, mutate);
|
|
226
|
-
}
|
|
227
215
|
absDiff(other, unit) {
|
|
228
216
|
return Math.abs(this.diff(other, unit));
|
|
229
217
|
}
|
package/package.json
CHANGED
package/src/array/array.util.ts
CHANGED
|
@@ -199,7 +199,7 @@ export function _sortDescBy<T>(items: T[], mapper: Mapper<T, any>, mutate = fals
|
|
|
199
199
|
*
|
|
200
200
|
* Use `Array.find` if you don't need to stop the iteration early.
|
|
201
201
|
*/
|
|
202
|
-
export function _find<T>(items: T[], predicate: AbortablePredicate<T>): T | undefined {
|
|
202
|
+
export function _find<T>(items: readonly T[], predicate: AbortablePredicate<T>): T | undefined {
|
|
203
203
|
for (const [i, item] of items.entries()) {
|
|
204
204
|
const result = predicate(item, i)
|
|
205
205
|
if (result === END) return
|
|
@@ -214,26 +214,26 @@ export function _find<T>(items: T[], predicate: AbortablePredicate<T>): T | unde
|
|
|
214
214
|
* - in Node since 18+
|
|
215
215
|
* - in iOS Safari since 15.4
|
|
216
216
|
*/
|
|
217
|
-
export function _findLast<T>(items: T[], predicate: AbortablePredicate<T>): T | undefined {
|
|
217
|
+
export function _findLast<T>(items: readonly T[], predicate: AbortablePredicate<T>): T | undefined {
|
|
218
218
|
return _find(items.slice().reverse(), predicate)
|
|
219
219
|
}
|
|
220
220
|
|
|
221
|
-
export function _takeWhile<T>(items: T[], predicate: Predicate<T>): T[] {
|
|
221
|
+
export function _takeWhile<T>(items: readonly T[], predicate: Predicate<T>): T[] {
|
|
222
222
|
let proceed = true
|
|
223
223
|
return items.filter((v, index) => (proceed &&= predicate(v, index)))
|
|
224
224
|
}
|
|
225
225
|
|
|
226
|
-
export function _takeRightWhile<T>(items: T[], predicate: Predicate<T>): T[] {
|
|
226
|
+
export function _takeRightWhile<T>(items: readonly T[], predicate: Predicate<T>): T[] {
|
|
227
227
|
let proceed = true
|
|
228
228
|
return [...items].reverse().filter((v, index) => (proceed &&= predicate(v, index)))
|
|
229
229
|
}
|
|
230
230
|
|
|
231
|
-
export function _dropWhile<T>(items: T[], predicate: Predicate<T>): T[] {
|
|
231
|
+
export function _dropWhile<T>(items: readonly T[], predicate: Predicate<T>): T[] {
|
|
232
232
|
let proceed = false
|
|
233
233
|
return items.filter((v, index) => (proceed ||= !predicate(v, index)))
|
|
234
234
|
}
|
|
235
235
|
|
|
236
|
-
export function _dropRightWhile<T>(items: T[], predicate: Predicate<T>): T[] {
|
|
236
|
+
export function _dropRightWhile<T>(items: readonly T[], predicate: Predicate<T>): T[] {
|
|
237
237
|
let proceed = false
|
|
238
238
|
return [...items]
|
|
239
239
|
.reverse()
|
|
@@ -246,12 +246,17 @@ export function _dropRightWhile<T>(items: T[], predicate: Predicate<T>): T[] {
|
|
|
246
246
|
*
|
|
247
247
|
* `limit` allows to exit early when limit count is reached, skipping further iterations (perf optimization).
|
|
248
248
|
*/
|
|
249
|
-
export function _count<T>(
|
|
249
|
+
export function _count<T>(
|
|
250
|
+
items: Iterable<T>,
|
|
251
|
+
predicate: AbortablePredicate<T>,
|
|
252
|
+
limit?: number,
|
|
253
|
+
): number {
|
|
250
254
|
if (limit === 0) return 0
|
|
251
255
|
let count = 0
|
|
256
|
+
let i = 0
|
|
252
257
|
|
|
253
|
-
for (const
|
|
254
|
-
const r = predicate(item, i)
|
|
258
|
+
for (const item of items) {
|
|
259
|
+
const r = predicate(item, i++)
|
|
255
260
|
if (r === END) break
|
|
256
261
|
if (r) {
|
|
257
262
|
count++
|
|
@@ -262,16 +267,14 @@ export function _count<T>(items: T[], predicate: AbortablePredicate<T>, limit?:
|
|
|
262
267
|
return count
|
|
263
268
|
}
|
|
264
269
|
|
|
265
|
-
|
|
266
|
-
* `limit` allows to exit early when limit count is reached, skipping further iterations (perf optimization).
|
|
267
|
-
*/
|
|
268
|
-
export function _countBy<T>(items: T[], mapper: Mapper<T, any>): StringMap<number> {
|
|
270
|
+
export function _countBy<T>(items: Iterable<T>, mapper: Mapper<T, any>): StringMap<number> {
|
|
269
271
|
const map: StringMap<number> = {}
|
|
270
272
|
|
|
271
|
-
|
|
272
|
-
|
|
273
|
+
let i = 0
|
|
274
|
+
for (const item of items) {
|
|
275
|
+
const key = mapper(item, i++)
|
|
273
276
|
map[key] = (map[key] || 0) + 1
|
|
274
|
-
}
|
|
277
|
+
}
|
|
275
278
|
|
|
276
279
|
return map
|
|
277
280
|
}
|
|
@@ -319,15 +322,27 @@ export function _difference<T>(source: T[], ...diffs: T[][]): T[] {
|
|
|
319
322
|
/**
|
|
320
323
|
* Returns the sum of items, or 0 for empty array.
|
|
321
324
|
*/
|
|
322
|
-
export function _sum(items: number
|
|
323
|
-
|
|
325
|
+
export function _sum(items: Iterable<number>): number {
|
|
326
|
+
let sum = 0
|
|
327
|
+
for (const n of items) {
|
|
328
|
+
sum += n
|
|
329
|
+
}
|
|
330
|
+
return sum
|
|
324
331
|
}
|
|
325
332
|
|
|
326
|
-
export function _sumBy<T>(items: T
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
333
|
+
export function _sumBy<T>(items: Iterable<T>, mapper: Mapper<T, number | undefined>): number {
|
|
334
|
+
let sum = 0
|
|
335
|
+
let i = 0
|
|
336
|
+
|
|
337
|
+
for (const n of items) {
|
|
338
|
+
const v = mapper(n, i++)
|
|
339
|
+
if (typeof v === 'number') {
|
|
340
|
+
// count only numbers, nothing else
|
|
341
|
+
sum += v
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
return sum
|
|
331
346
|
}
|
|
332
347
|
|
|
333
348
|
/**
|
|
@@ -344,7 +359,7 @@ export function _sumBy<T>(items: T[], mapper: Mapper<T, number | undefined>): nu
|
|
|
344
359
|
* // { '1': 'id1, '2': 'id2', '3': 'id3' }
|
|
345
360
|
*/
|
|
346
361
|
export function _mapToObject<T, V>(
|
|
347
|
-
array: T
|
|
362
|
+
array: Iterable<T>,
|
|
348
363
|
mapper: (item: T) => [key: any, value: V] | FalsyValue,
|
|
349
364
|
): StringMap<V> {
|
|
350
365
|
const m: StringMap<V> = {}
|
|
@@ -379,7 +394,7 @@ export function _shuffle<T>(array: T[], mutate = false): T[] {
|
|
|
379
394
|
* Returns last item of non-empty array.
|
|
380
395
|
* Throws if array is empty.
|
|
381
396
|
*/
|
|
382
|
-
export function _last<T>(array: T[]): T {
|
|
397
|
+
export function _last<T>(array: readonly T[]): T {
|
|
383
398
|
if (!array.length) throw new Error('_last called on empty array')
|
|
384
399
|
return array[array.length - 1]!
|
|
385
400
|
}
|
|
@@ -387,7 +402,7 @@ export function _last<T>(array: T[]): T {
|
|
|
387
402
|
/**
|
|
388
403
|
* Returns last item of the array (or undefined if array is empty).
|
|
389
404
|
*/
|
|
390
|
-
export function _lastOrUndefined<T>(array: T[]): T | undefined {
|
|
405
|
+
export function _lastOrUndefined<T>(array: readonly T[]): T | undefined {
|
|
391
406
|
return array[array.length - 1]
|
|
392
407
|
}
|
|
393
408
|
|
|
@@ -395,48 +410,48 @@ export function _lastOrUndefined<T>(array: T[]): T | undefined {
|
|
|
395
410
|
* Returns the first item of non-empty array.
|
|
396
411
|
* Throws if array is empty.
|
|
397
412
|
*/
|
|
398
|
-
export function _first<T>(array: T[]): T {
|
|
413
|
+
export function _first<T>(array: readonly T[]): T {
|
|
399
414
|
if (!array.length) throw new Error('_first called on empty array')
|
|
400
415
|
return array[0]!
|
|
401
416
|
}
|
|
402
417
|
|
|
403
|
-
export function _minOrUndefined<T>(array: T[]): NonNullable<T> | undefined {
|
|
418
|
+
export function _minOrUndefined<T>(array: readonly T[]): NonNullable<T> | undefined {
|
|
404
419
|
const a = array.filter(_isNotNullish)
|
|
405
420
|
if (!a.length) return
|
|
406
|
-
return
|
|
421
|
+
return a.reduce((min, item) => (min <= item ? min : item))
|
|
407
422
|
}
|
|
408
423
|
|
|
409
424
|
/**
|
|
410
425
|
* Filters out nullish values (undefined and null).
|
|
411
426
|
*/
|
|
412
|
-
export function _min<T>(array: T[]): NonNullable<T> {
|
|
427
|
+
export function _min<T>(array: readonly T[]): NonNullable<T> {
|
|
413
428
|
const a = array.filter(_isNotNullish)
|
|
414
429
|
if (!a.length) throw new Error('_min called on empty array')
|
|
415
430
|
return a.reduce((min, item) => (min <= item ? min : item))
|
|
416
431
|
}
|
|
417
432
|
|
|
418
|
-
export function _maxOrUndefined<T>(array: T[]): NonNullable<T> | undefined {
|
|
433
|
+
export function _maxOrUndefined<T>(array: readonly T[]): NonNullable<T> | undefined {
|
|
419
434
|
const a = array.filter(_isNotNullish)
|
|
420
435
|
if (!a.length) return
|
|
421
|
-
return
|
|
436
|
+
return a.reduce((max, item) => (max >= item ? max : item))
|
|
422
437
|
}
|
|
423
438
|
|
|
424
439
|
/**
|
|
425
440
|
* Filters out nullish values (undefined and null).
|
|
426
441
|
*/
|
|
427
|
-
export function _max<T>(array: T[]): NonNullable<T> {
|
|
442
|
+
export function _max<T>(array: readonly T[]): NonNullable<T> {
|
|
428
443
|
const a = array.filter(_isNotNullish)
|
|
429
444
|
if (!a.length) throw new Error('_max called on empty array')
|
|
430
445
|
return a.reduce((max, item) => (max >= item ? max : item))
|
|
431
446
|
}
|
|
432
447
|
|
|
433
|
-
export function _maxBy<T>(array: T[], mapper: Mapper<T, number | string | undefined>): T {
|
|
448
|
+
export function _maxBy<T>(array: readonly T[], mapper: Mapper<T, number | string | undefined>): T {
|
|
434
449
|
const max = _maxByOrUndefined(array, mapper)
|
|
435
450
|
if (max === undefined) throw new Error(`_maxBy returned undefined`)
|
|
436
451
|
return max
|
|
437
452
|
}
|
|
438
453
|
|
|
439
|
-
export function _minBy<T>(array: T[], mapper: Mapper<T, number | string | undefined>): T {
|
|
454
|
+
export function _minBy<T>(array: readonly T[], mapper: Mapper<T, number | string | undefined>): T {
|
|
440
455
|
const min = _minByOrUndefined(array, mapper)
|
|
441
456
|
if (min === undefined) throw new Error(`_minBy returned undefined`)
|
|
442
457
|
return min
|
|
@@ -445,7 +460,7 @@ export function _minBy<T>(array: T[], mapper: Mapper<T, number | string | undefi
|
|
|
445
460
|
// todo: looks like it _maxByOrUndefined/_minByOrUndefined can be DRYer
|
|
446
461
|
|
|
447
462
|
export function _maxByOrUndefined<T>(
|
|
448
|
-
array: T[],
|
|
463
|
+
array: readonly T[],
|
|
449
464
|
mapper: Mapper<T, number | string | undefined>,
|
|
450
465
|
): T | undefined {
|
|
451
466
|
if (!array.length) return
|
|
@@ -464,7 +479,7 @@ export function _maxByOrUndefined<T>(
|
|
|
464
479
|
}
|
|
465
480
|
|
|
466
481
|
export function _minByOrUndefined<T>(
|
|
467
|
-
array: T[],
|
|
482
|
+
array: readonly T[],
|
|
468
483
|
mapper: Mapper<T, number | string | undefined>,
|
|
469
484
|
): T | undefined {
|
|
470
485
|
if (!array.length) return
|
|
@@ -482,7 +497,7 @@ export function _minByOrUndefined<T>(
|
|
|
482
497
|
return minItem
|
|
483
498
|
}
|
|
484
499
|
|
|
485
|
-
export function _zip<T1, T2>(array1: T1[], array2: T2[]): [T1, T2][] {
|
|
500
|
+
export function _zip<T1, T2>(array1: readonly T1[], array2: readonly T2[]): [T1, T2][] {
|
|
486
501
|
const len = Math.min(array1.length, array2.length)
|
|
487
502
|
const res: [T1, T2][] = []
|
|
488
503
|
|
|
@@ -415,24 +415,10 @@ export class LocalDate {
|
|
|
415
415
|
return new LocalDate($year, $month, $day)
|
|
416
416
|
}
|
|
417
417
|
|
|
418
|
-
/**
|
|
419
|
-
* @deprecated use `minus` instead
|
|
420
|
-
*/
|
|
421
|
-
subtract(num: number, unit: LocalDateUnit, mutate = false): LocalDate {
|
|
422
|
-
return this.plus(-num, unit, mutate)
|
|
423
|
-
}
|
|
424
|
-
|
|
425
418
|
minus(num: number, unit: LocalDateUnit, mutate = false): LocalDate {
|
|
426
419
|
return this.plus(-num, unit, mutate)
|
|
427
420
|
}
|
|
428
421
|
|
|
429
|
-
/**
|
|
430
|
-
* @deprecated use `plus` instead
|
|
431
|
-
*/
|
|
432
|
-
add(num: number, unit: LocalDateUnit, mutate = false): LocalDate {
|
|
433
|
-
return this.plus(num, unit, mutate)
|
|
434
|
-
}
|
|
435
|
-
|
|
436
422
|
startOf(unit: LocalDateUnitStrict): LocalDate {
|
|
437
423
|
if (unit === 'day') return this
|
|
438
424
|
if (unit === 'month') return LocalDate.create(this.$year, this.$month, 1)
|
|
@@ -281,24 +281,10 @@ export class LocalTime {
|
|
|
281
281
|
return this.set(unit, this.get(unit) + num, mutate)
|
|
282
282
|
}
|
|
283
283
|
|
|
284
|
-
/**
|
|
285
|
-
* @deprecated use `minus` instead
|
|
286
|
-
*/
|
|
287
|
-
subtract(num: number, unit: LocalTimeUnit, mutate = false): LocalTime {
|
|
288
|
-
return this.plus(num * -1, unit, mutate)
|
|
289
|
-
}
|
|
290
|
-
|
|
291
284
|
minus(num: number, unit: LocalTimeUnit, mutate = false): LocalTime {
|
|
292
285
|
return this.plus(num * -1, unit, mutate)
|
|
293
286
|
}
|
|
294
287
|
|
|
295
|
-
/**
|
|
296
|
-
* @deprecated use `plus` instead
|
|
297
|
-
*/
|
|
298
|
-
add(num: number, unit: LocalTimeUnit, mutate = false): LocalTime {
|
|
299
|
-
return this.plus(num, unit, mutate)
|
|
300
|
-
}
|
|
301
|
-
|
|
302
288
|
absDiff(other: LocalTimeInput, unit: LocalTimeUnit): number {
|
|
303
289
|
return Math.abs(this.diff(other, unit))
|
|
304
290
|
}
|
package/src/promise/pRetry.ts
CHANGED