radashi 12.2.2 → 12.3.0-beta.8c8abf6
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/radashi.cjs +87 -4
- package/dist/radashi.d.cts +167 -5
- package/dist/radashi.d.ts +167 -5
- package/dist/radashi.js +83 -5
- package/package.json +1 -1
package/dist/radashi.cjs
CHANGED
|
@@ -18,6 +18,23 @@ function boil(array, compareFunc) {
|
|
|
18
18
|
return array.reduce(compareFunc);
|
|
19
19
|
}
|
|
20
20
|
|
|
21
|
+
// src/array/cartesianProduct.ts
|
|
22
|
+
function cartesianProduct(...arrays) {
|
|
23
|
+
let out = [[]];
|
|
24
|
+
for (const array of arrays) {
|
|
25
|
+
const result = [];
|
|
26
|
+
for (const currentArray of out) {
|
|
27
|
+
for (const item of array) {
|
|
28
|
+
const currentArrayCopy = currentArray.slice();
|
|
29
|
+
currentArrayCopy.push(item);
|
|
30
|
+
result.push(currentArrayCopy);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
out = result;
|
|
34
|
+
}
|
|
35
|
+
return out;
|
|
36
|
+
}
|
|
37
|
+
|
|
21
38
|
// src/array/castArray.ts
|
|
22
39
|
function castArray(value) {
|
|
23
40
|
return Array.isArray(value) ? value.slice() : [value];
|
|
@@ -445,17 +462,27 @@ async function map(array, asyncMapFunc) {
|
|
|
445
462
|
}
|
|
446
463
|
|
|
447
464
|
// src/async/parallel.ts
|
|
448
|
-
async function parallel(
|
|
465
|
+
async function parallel(options, array, func) {
|
|
466
|
+
var _a;
|
|
449
467
|
const work = array.map((item, index) => ({
|
|
450
468
|
index,
|
|
451
469
|
item
|
|
452
470
|
}));
|
|
453
|
-
|
|
471
|
+
if (isNumber(options)) {
|
|
472
|
+
options = {
|
|
473
|
+
limit: options
|
|
474
|
+
};
|
|
475
|
+
}
|
|
476
|
+
(_a = options.signal) == null ? void 0 : _a.throwIfAborted();
|
|
477
|
+
const processor = async (resolve, reject) => {
|
|
478
|
+
var _a2, _b;
|
|
454
479
|
const results2 = [];
|
|
480
|
+
const abortListener = () => reject(new Error("This operation was aborted"));
|
|
481
|
+
(_a2 = options.signal) == null ? void 0 : _a2.addEventListener("abort", abortListener);
|
|
455
482
|
while (true) {
|
|
456
483
|
const next = work.pop();
|
|
457
484
|
if (!next) {
|
|
458
|
-
|
|
485
|
+
break;
|
|
459
486
|
}
|
|
460
487
|
const [error, result] = await tryit(func)(next.item);
|
|
461
488
|
results2.push({
|
|
@@ -464,8 +491,10 @@ async function parallel(limit, array, func) {
|
|
|
464
491
|
index: next.index
|
|
465
492
|
});
|
|
466
493
|
}
|
|
494
|
+
(_b = options.signal) == null ? void 0 : _b.removeEventListener("abort", abortListener);
|
|
495
|
+
return resolve(results2);
|
|
467
496
|
};
|
|
468
|
-
const queues = list(1, limit).map(() => new Promise(processor));
|
|
497
|
+
const queues = list(1, options.limit).map(() => new Promise(processor));
|
|
469
498
|
const itemResults = await Promise.all(queues);
|
|
470
499
|
const [errors, results] = fork(
|
|
471
500
|
sort(flat(itemResults), (r) => r.index),
|
|
@@ -496,11 +525,13 @@ async function retry(options, func) {
|
|
|
496
525
|
const times = (options == null ? void 0 : options.times) ?? 3;
|
|
497
526
|
const delay = options == null ? void 0 : options.delay;
|
|
498
527
|
const backoff = (options == null ? void 0 : options.backoff) ?? null;
|
|
528
|
+
const signal = options == null ? void 0 : options.signal;
|
|
499
529
|
let i = 0;
|
|
500
530
|
while (true) {
|
|
501
531
|
const [err, result] = await tryit(func)((err2) => {
|
|
502
532
|
throw { _exited: err2 };
|
|
503
533
|
});
|
|
534
|
+
signal == null ? void 0 : signal.throwIfAborted();
|
|
504
535
|
if (!err) {
|
|
505
536
|
return result;
|
|
506
537
|
}
|
|
@@ -524,6 +555,19 @@ function sleep(milliseconds) {
|
|
|
524
555
|
return new Promise((res) => setTimeout(res, milliseconds));
|
|
525
556
|
}
|
|
526
557
|
|
|
558
|
+
// src/async/timeout.ts
|
|
559
|
+
function timeout(milliseconds, error = "timeout") {
|
|
560
|
+
return new Promise(
|
|
561
|
+
(_, rej) => setTimeout(() => {
|
|
562
|
+
if (isString(error)) {
|
|
563
|
+
rej(new Error(error));
|
|
564
|
+
} else {
|
|
565
|
+
rej(error());
|
|
566
|
+
}
|
|
567
|
+
}, milliseconds)
|
|
568
|
+
);
|
|
569
|
+
}
|
|
570
|
+
|
|
527
571
|
// src/async/tryit.ts
|
|
528
572
|
function tryit(func) {
|
|
529
573
|
return (...args) => {
|
|
@@ -1369,6 +1413,30 @@ function dash(str) {
|
|
|
1369
1413
|
});
|
|
1370
1414
|
}
|
|
1371
1415
|
|
|
1416
|
+
// src/string/dedent.ts
|
|
1417
|
+
function dedent(text, ...values) {
|
|
1418
|
+
var _a;
|
|
1419
|
+
if (isArray(text)) {
|
|
1420
|
+
if (values.length > 0) {
|
|
1421
|
+
return dedent(
|
|
1422
|
+
text.reduce((acc, input, i) => {
|
|
1423
|
+
var _a2;
|
|
1424
|
+
let value = String(values[i] ?? "");
|
|
1425
|
+
const indent2 = value.includes("\n") && ((_a2 = input.match(/[ \t]*(?=[^\n]*$)/)) == null ? void 0 : _a2[0]);
|
|
1426
|
+
if (indent2) {
|
|
1427
|
+
value = value.replace(/\n(?=[^\n]*?\S)/g, "\n" + indent2);
|
|
1428
|
+
}
|
|
1429
|
+
return acc + input + value;
|
|
1430
|
+
}, "")
|
|
1431
|
+
);
|
|
1432
|
+
}
|
|
1433
|
+
text = text[0];
|
|
1434
|
+
}
|
|
1435
|
+
const indent = values[0] ?? ((_a = text.match(/^[ \t]*(?=\S)/m)) == null ? void 0 : _a[0]);
|
|
1436
|
+
const output = indent ? text.replace(new RegExp(`^${indent}`, "gm"), "") : text;
|
|
1437
|
+
return output.replace(/^[ \t]*\n|\n[ \t]*$/g, "");
|
|
1438
|
+
}
|
|
1439
|
+
|
|
1372
1440
|
// src/string/pascal.ts
|
|
1373
1441
|
function pascal(str) {
|
|
1374
1442
|
if (!str) {
|
|
@@ -1487,6 +1555,11 @@ function isBoolean(value) {
|
|
|
1487
1555
|
return typeof value === "boolean";
|
|
1488
1556
|
}
|
|
1489
1557
|
|
|
1558
|
+
// src/typed/isClass.ts
|
|
1559
|
+
function isClass(value) {
|
|
1560
|
+
return isFunction(value) && Function.prototype.toString.call(value).startsWith("class ");
|
|
1561
|
+
}
|
|
1562
|
+
|
|
1490
1563
|
// src/typed/isDate.ts
|
|
1491
1564
|
function isDate(value) {
|
|
1492
1565
|
return isTagged(value, "[object Date]");
|
|
@@ -1591,6 +1664,11 @@ function isMap(value) {
|
|
|
1591
1664
|
return isTagged(value, "[object Map]");
|
|
1592
1665
|
}
|
|
1593
1666
|
|
|
1667
|
+
// src/typed/isNullish.ts
|
|
1668
|
+
function isNullish(value) {
|
|
1669
|
+
return value === null || value === void 0;
|
|
1670
|
+
}
|
|
1671
|
+
|
|
1594
1672
|
// src/typed/isNumber.ts
|
|
1595
1673
|
function isNumber(value) {
|
|
1596
1674
|
return typeof value === "number" && !Number.isNaN(value);
|
|
@@ -1686,6 +1764,7 @@ exports.boil = boil;
|
|
|
1686
1764
|
exports.callable = callable;
|
|
1687
1765
|
exports.camel = camel;
|
|
1688
1766
|
exports.capitalize = capitalize;
|
|
1767
|
+
exports.cartesianProduct = cartesianProduct;
|
|
1689
1768
|
exports.castArray = castArray;
|
|
1690
1769
|
exports.castArrayIfExists = castArrayIfExists;
|
|
1691
1770
|
exports.castComparator = castComparator;
|
|
@@ -1701,6 +1780,7 @@ exports.counting = counting;
|
|
|
1701
1780
|
exports.crush = crush;
|
|
1702
1781
|
exports.dash = dash;
|
|
1703
1782
|
exports.debounce = debounce;
|
|
1783
|
+
exports.dedent = dedent;
|
|
1704
1784
|
exports.defer = defer;
|
|
1705
1785
|
exports.diff = diff;
|
|
1706
1786
|
exports.draw = draw;
|
|
@@ -1717,6 +1797,7 @@ exports.intersects = intersects;
|
|
|
1717
1797
|
exports.invert = invert;
|
|
1718
1798
|
exports.isArray = isArray;
|
|
1719
1799
|
exports.isBoolean = isBoolean;
|
|
1800
|
+
exports.isClass = isClass;
|
|
1720
1801
|
exports.isDate = isDate;
|
|
1721
1802
|
exports.isEmpty = isEmpty;
|
|
1722
1803
|
exports.isEqual = isEqual;
|
|
@@ -1727,6 +1808,7 @@ exports.isInt = isInt;
|
|
|
1727
1808
|
exports.isIntString = isIntString;
|
|
1728
1809
|
exports.isIterable = isIterable;
|
|
1729
1810
|
exports.isMap = isMap;
|
|
1811
|
+
exports.isNullish = isNullish;
|
|
1730
1812
|
exports.isNumber = isNumber;
|
|
1731
1813
|
exports.isObject = isObject;
|
|
1732
1814
|
exports.isPlainObject = isPlainObject;
|
|
@@ -1790,6 +1872,7 @@ exports.sort = sort;
|
|
|
1790
1872
|
exports.sum = sum;
|
|
1791
1873
|
exports.template = template;
|
|
1792
1874
|
exports.throttle = throttle;
|
|
1875
|
+
exports.timeout = timeout;
|
|
1793
1876
|
exports.title = title;
|
|
1794
1877
|
exports.toFloat = toFloat;
|
|
1795
1878
|
exports.toInt = toInt;
|
package/dist/radashi.d.cts
CHANGED
|
@@ -21,6 +21,29 @@ declare function alphabetical<T>(array: readonly T[], getter: (item: T) => strin
|
|
|
21
21
|
*/
|
|
22
22
|
declare function boil<T>(array: readonly T[], compareFunc: (a: T, b: T) => T): T | null;
|
|
23
23
|
|
|
24
|
+
/**
|
|
25
|
+
* Create an [n-ary Cartesian product](https://en.wikipedia.org/wiki/Cartesian_product#n-ary_Cartesian_product) from the given arrays.
|
|
26
|
+
*
|
|
27
|
+
* @see https://radashi.js.org/reference/array/cartesianProduct
|
|
28
|
+
* @example
|
|
29
|
+
* ```ts
|
|
30
|
+
* product([['red', 'blue'], ['big', 'small'], ['fast', 'slow']])
|
|
31
|
+
* // [
|
|
32
|
+
* // ['red', 'big', 'fast'],
|
|
33
|
+
* // ['red', 'big', 'slow'],
|
|
34
|
+
* // ['red', 'small', 'fast'],
|
|
35
|
+
* // ['red', 'small', 'slow'],
|
|
36
|
+
* // ['blue', 'big', 'fast'],
|
|
37
|
+
* // ['blue', 'big', 'slow'],
|
|
38
|
+
* // ['blue', 'small', 'fast'],
|
|
39
|
+
* // ['blue', 'small', 'slow']
|
|
40
|
+
* // ]
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
declare function cartesianProduct<T extends any[][]>(...arrays: [...T]): Array<{
|
|
44
|
+
[K in keyof T]: T[K][number];
|
|
45
|
+
}>;
|
|
46
|
+
|
|
24
47
|
/**
|
|
25
48
|
* Casts the given value to an array. If the value is already an
|
|
26
49
|
* array, a shallow copy is returned. Otherwise, a new array
|
|
@@ -645,23 +668,44 @@ declare function guard<TFunction extends () => any>(func: TFunction, shouldGuard
|
|
|
645
668
|
*/
|
|
646
669
|
declare function map<T, K>(array: readonly T[], asyncMapFunc: (item: T, index: number) => Promise<K>): Promise<K[]>;
|
|
647
670
|
|
|
671
|
+
type AbortSignal$1 = {
|
|
672
|
+
readonly aborted: boolean;
|
|
673
|
+
addEventListener(type: 'abort', listener: () => void): void;
|
|
674
|
+
removeEventListener(type: 'abort', listener: () => void): void;
|
|
675
|
+
throwIfAborted(): void;
|
|
676
|
+
};
|
|
677
|
+
type ParallelOptions = {
|
|
678
|
+
limit: number;
|
|
679
|
+
signal?: AbortSignal$1;
|
|
680
|
+
} | number;
|
|
648
681
|
/**
|
|
649
682
|
* Executes many async functions in parallel. Returns the results from
|
|
650
683
|
* all functions as an array. After all functions have resolved, if
|
|
651
684
|
* any errors were thrown, they are rethrown in an instance of
|
|
652
|
-
* AggregateError.
|
|
685
|
+
* AggregateError. The operation can be aborted by passing optional AbortSignal,
|
|
686
|
+
* which will throw an Error if aborted.
|
|
653
687
|
*
|
|
654
688
|
* @see https://radashi.js.org/reference/async/parallel
|
|
655
689
|
* @example
|
|
656
690
|
* ```ts
|
|
657
691
|
* // Process images concurrently, resizing each image to a standard size.
|
|
658
|
-
* const
|
|
692
|
+
* const abortController = new AbortController();
|
|
693
|
+
* const images = await parallel(
|
|
694
|
+
* {
|
|
695
|
+
* limit: 2,
|
|
696
|
+
* signal: abortController.signal,
|
|
697
|
+
* },
|
|
698
|
+
* imageFiles,
|
|
699
|
+
* async file => {
|
|
659
700
|
* return await resizeImage(file)
|
|
660
701
|
* })
|
|
702
|
+
*
|
|
703
|
+
* // To abort the operation:
|
|
704
|
+
* // abortController.abort()
|
|
661
705
|
* ```
|
|
662
706
|
* @version 12.1.0
|
|
663
707
|
*/
|
|
664
|
-
declare function parallel<T, K>(
|
|
708
|
+
declare function parallel<T, K>(options: ParallelOptions, array: readonly T[], func: (item: T) => Promise<K>): Promise<K[]>;
|
|
665
709
|
|
|
666
710
|
/**
|
|
667
711
|
* An async reduce function. Works like the built-in Array.reduce
|
|
@@ -678,10 +722,14 @@ declare function parallel<T, K>(limit: number, array: readonly T[], func: (item:
|
|
|
678
722
|
*/
|
|
679
723
|
declare function reduce<T, K>(array: readonly T[], asyncReducer: (acc: K, item: T, index: number) => Promise<K>, initValue?: K): Promise<K>;
|
|
680
724
|
|
|
725
|
+
type AbortSignal = {
|
|
726
|
+
throwIfAborted(): void;
|
|
727
|
+
};
|
|
681
728
|
type RetryOptions = {
|
|
682
729
|
times?: number;
|
|
683
730
|
delay?: number | null;
|
|
684
731
|
backoff?: (count: number) => number;
|
|
732
|
+
signal?: AbortSignal;
|
|
685
733
|
};
|
|
686
734
|
/**
|
|
687
735
|
* Retries the given function the specified number of times.
|
|
@@ -689,9 +737,12 @@ type RetryOptions = {
|
|
|
689
737
|
* @see https://radashi.js.org/reference/async/retry
|
|
690
738
|
* @example
|
|
691
739
|
* ```ts
|
|
692
|
-
* const
|
|
740
|
+
* const abortController = new AbortController();
|
|
741
|
+
* const result = await retry({ times: 3, delay: 1000, signal: abortController.signal }, async () => {
|
|
693
742
|
* return await fetch('https://example.com')
|
|
694
743
|
* })
|
|
744
|
+
* // To abort the operation:
|
|
745
|
+
* // abortController.abort()
|
|
695
746
|
* ```
|
|
696
747
|
* @version 12.1.0
|
|
697
748
|
*/
|
|
@@ -709,6 +760,39 @@ declare function retry<TResponse>(options: RetryOptions, func: (exit: (err: any)
|
|
|
709
760
|
*/
|
|
710
761
|
declare function sleep(milliseconds: number): Promise<void>;
|
|
711
762
|
|
|
763
|
+
/**
|
|
764
|
+
* Creates a promise that will reject after a specified amount of time.
|
|
765
|
+
* You can provide a custom error message or a function that returns an error.
|
|
766
|
+
*
|
|
767
|
+
* @see https://radashi.js.org/reference/async/timeout
|
|
768
|
+
*
|
|
769
|
+
* @example
|
|
770
|
+
* ```ts
|
|
771
|
+
* // Reject after 1000 milliseconds with default message "timeout"
|
|
772
|
+
* await timeout(1000)
|
|
773
|
+
*
|
|
774
|
+
* // Reject after 1000 milliseconds with a custom message
|
|
775
|
+
* await timeout(1000, "Optional message")
|
|
776
|
+
*
|
|
777
|
+
* // Reject after 1000 milliseconds with a custom error
|
|
778
|
+
* await timeout(1000, () => new Error("Custom error"))
|
|
779
|
+
*
|
|
780
|
+
* // Example usage with Promise.race to set a timeout for an asynchronous task
|
|
781
|
+
* await Promise.race([
|
|
782
|
+
* someAsyncTask(),
|
|
783
|
+
* timeout(1000, "Optional message"),
|
|
784
|
+
* ])
|
|
785
|
+
* ```
|
|
786
|
+
* @version 12.3.0
|
|
787
|
+
*/
|
|
788
|
+
declare function timeout<E extends Error>(milliseconds: number,
|
|
789
|
+
/**
|
|
790
|
+
* The error message to reject with.
|
|
791
|
+
*
|
|
792
|
+
* @default "timeout"
|
|
793
|
+
*/
|
|
794
|
+
error?: string | (() => E)): Promise<void>;
|
|
795
|
+
|
|
712
796
|
/**
|
|
713
797
|
* The result of a `tryit` function.
|
|
714
798
|
*
|
|
@@ -2099,6 +2183,44 @@ declare function capitalize(str: string): string;
|
|
|
2099
2183
|
*/
|
|
2100
2184
|
declare function dash(str: string): string;
|
|
2101
2185
|
|
|
2186
|
+
/**
|
|
2187
|
+
* Remove indentation from a string. The given string is expected to
|
|
2188
|
+
* be consistently indented (i.e. the leading whitespace of the first
|
|
2189
|
+
* non-empty line is the minimum required for all non-empty lines).
|
|
2190
|
+
*
|
|
2191
|
+
* If the `indent` argument is nullish, the indentation is detected
|
|
2192
|
+
* from the first non-empty line. Detection is cheap and robust for
|
|
2193
|
+
* most use cases, so you should only set an explicit `indent` if
|
|
2194
|
+
* necessary.
|
|
2195
|
+
*
|
|
2196
|
+
* @see https://radashi-org.github.io/reference/string/dedent
|
|
2197
|
+
* @example
|
|
2198
|
+
* ```ts
|
|
2199
|
+
* // This is indented with 4 spaces.
|
|
2200
|
+
* const input = `
|
|
2201
|
+
* Hello
|
|
2202
|
+
* World
|
|
2203
|
+
* `
|
|
2204
|
+
*
|
|
2205
|
+
* // Explicit indentation
|
|
2206
|
+
* dedent(input, ' ')
|
|
2207
|
+
* // => ' Hello\n World\n'
|
|
2208
|
+
*
|
|
2209
|
+
* // Detected indentation
|
|
2210
|
+
* dedent(input)
|
|
2211
|
+
* // => 'Hello\nWorld\n'
|
|
2212
|
+
*
|
|
2213
|
+
* // Tagged template strings
|
|
2214
|
+
* const str = dedent`
|
|
2215
|
+
* Foo ${1 + 1}
|
|
2216
|
+
* Bar ${2 * 2}
|
|
2217
|
+
* `
|
|
2218
|
+
* // => 'Foo 2\nBar 4'
|
|
2219
|
+
* ```
|
|
2220
|
+
*/
|
|
2221
|
+
declare function dedent(template: TemplateStringsArray, ...values: unknown[]): string;
|
|
2222
|
+
declare function dedent(text: string, indent?: string | null): string;
|
|
2223
|
+
|
|
2102
2224
|
/**
|
|
2103
2225
|
* Formats the given string in pascal case fashion.
|
|
2104
2226
|
*
|
|
@@ -2225,6 +2347,27 @@ type ExtractArray<T> = T extends any ? [StrictExtract<T, readonly any[]>] extend
|
|
|
2225
2347
|
|
|
2226
2348
|
declare function isBoolean(value: unknown): value is boolean;
|
|
2227
2349
|
|
|
2350
|
+
/**
|
|
2351
|
+
* Checks if the given value is a class. This function verifies
|
|
2352
|
+
* if the value was defined using the `class` syntax. Old school
|
|
2353
|
+
* classes (defined with constructor functions) will return false.
|
|
2354
|
+
* "Native classes" like `Error` will also return false.
|
|
2355
|
+
*
|
|
2356
|
+
* @see https://radashi.js.org/reference/typed/isClass
|
|
2357
|
+
* @example
|
|
2358
|
+
* ```ts
|
|
2359
|
+
* isClass(class CustomClass {}) // => true
|
|
2360
|
+
* isClass('abc') // => false
|
|
2361
|
+
* isClass({}) // => false
|
|
2362
|
+
* ```
|
|
2363
|
+
*/
|
|
2364
|
+
declare function isClass<T>(value: T): value is ExtractClass<T>;
|
|
2365
|
+
/**
|
|
2366
|
+
* Used by the `isClass` type guard. It handles type narrowing for
|
|
2367
|
+
* class constructors and even narrows `any` types.
|
|
2368
|
+
*/
|
|
2369
|
+
type ExtractClass<T> = [StrictExtract<T, Class>] extends [Class] ? Extract<T, Class> : T extends any ? Class<unknown[], unknown> extends T ? Class<unknown[], unknown> : never : never;
|
|
2370
|
+
|
|
2228
2371
|
/**
|
|
2229
2372
|
* Return true if the given value is a Date object.
|
|
2230
2373
|
*
|
|
@@ -2403,6 +2546,21 @@ type ExtractMap<T> = T extends any ? [StrictExtract<T, ReadonlyMap<unknown, unkn
|
|
|
2403
2546
|
ReadonlyMap<unknown, unknown>
|
|
2404
2547
|
] ? Extract<T, ReadonlyMap<unknown, unknown>> : [StrictExtract<T, Map<unknown, unknown>>] extends [Map<unknown, unknown>] ? Extract<T, Map<unknown, unknown>> : Map<unknown, unknown> extends T ? Map<unknown, unknown> : never : never;
|
|
2405
2548
|
|
|
2549
|
+
/**
|
|
2550
|
+
* Return true if the given value is null or undefined.
|
|
2551
|
+
*
|
|
2552
|
+
* @see https://radashi.js.org/reference/typed/isNullish
|
|
2553
|
+
* @example
|
|
2554
|
+
* ```ts
|
|
2555
|
+
* isNullish(null) // => true
|
|
2556
|
+
* isNullish(undefined) // => true
|
|
2557
|
+
* isNullish('') // => false
|
|
2558
|
+
* isNullish(0) // => false
|
|
2559
|
+
* ```
|
|
2560
|
+
* @version 12.2.0
|
|
2561
|
+
*/
|
|
2562
|
+
declare function isNullish(value: unknown): value is null | undefined;
|
|
2563
|
+
|
|
2406
2564
|
/**
|
|
2407
2565
|
* Return true if the given value is a number.
|
|
2408
2566
|
*
|
|
@@ -3182,6 +3340,10 @@ type Falsy = null | undefined | false | '' | 0 | 0n;
|
|
|
3182
3340
|
declare class Any {
|
|
3183
3341
|
private any;
|
|
3184
3342
|
}
|
|
3343
|
+
/**
|
|
3344
|
+
* Represents a class constructor.
|
|
3345
|
+
*/
|
|
3346
|
+
type Class<TArgs extends any[] = any[], TReturn = any> = new (...args: TArgs) => TReturn;
|
|
3185
3347
|
/**
|
|
3186
3348
|
* Extracts `T` if `T` is not `any`, otherwise `never`.
|
|
3187
3349
|
*
|
|
@@ -3379,4 +3541,4 @@ type GlobalObjectType<Identifier extends string> = [Identifier] extends [Any] ?
|
|
|
3379
3541
|
[P in Identifier]: any;
|
|
3380
3542
|
} ? InstanceType<(typeof globalThis)[Identifier]> : never;
|
|
3381
3543
|
|
|
3382
|
-
export { AggregateErrorOrPolyfill as AggregateError, Any, type Assign, type BoxedPrimitive, type BuiltInType, type CastArray, type CastArrayIfExists, type CloningStrategy, type Comparable, type ComparableProperty, type Comparator, type ComparatorMapping, type CompatibleProperty, type Crush, type CustomClass, type CustomClassRegistry, type DebounceFunction, type DebounceOptions, DefaultCloningStrategy, type Err, type ExtractArray, type ExtractMap, type ExtractNotAny, type ExtractSet, type Falsy, FastCloningStrategy, type FilteredKeys, type Flip, type Intersect, type IsExactType, type KeyFilter, type KeyFilterFunction, type LowercaseKeys, type MappedInput, type MappedOutput, type Mapping, type MappingFunction, type MemoOptions, type NoInfer, type Ok, type OnceFunction, type OptionalKeys, type OptionalMapping, type Primitive, type PromiseWithResolvers, type RequiredKeys, type Result, type ResultPromise, type RetryOptions, type Series, type Simplify, type StrictExtract, type SwitchAny, type SwitchNever, type ThrottledFunction, type ToEmpty, type ToEmptyAble, type TraverseContext, type TraverseOptions, type TraverseVisitor, type TryitResult, type TypedArray, type UppercaseKeys, all, alphabetical, always, assign, boil, callable, camel, capitalize, castArray, castArrayIfExists, castComparator, castMapping, chain, clamp, clone, cloneDeep, cluster, compose, construct, counting, crush, dash, debounce, defer, diff, draw, filterKey, first, flat, flip, fork, get, group, guard, inRange, intersects, invert, isArray, isBoolean, isDate, isEmpty, isEqual, isError, isFloat, isFunction, isInt, isIntString, isIterable, isMap, isNumber, isObject, isPlainObject, isPrimitive, isPromise, isRegExp, isResult, isResultErr, isResultOk, isSet, isString, isSymbol, isTagged, isWeakMap, isWeakSet, iterate, keys, last, lerp, list, listify, lowerize, map, mapEntries, mapKeys, mapValues, mapify, max, memo, merge, min, noop, objectify, omit, once, parallel, partial, partob, pascal, pick, proxied, random, range, reduce, replace, replaceOrAppend, retry, round, select, selectFirst, series, set, shake, shift, shuffle, sift, similarity, sleep, snake, sort, sum, template, throttle, title, toFloat, toInt, toggle, traverse, trim, tryit as try, tryit, uid, unique, unzip, upperize, withResolvers, zip, zipToObject };
|
|
3544
|
+
export { AggregateErrorOrPolyfill as AggregateError, Any, type Assign, type BoxedPrimitive, type BuiltInType, type CastArray, type CastArrayIfExists, type Class, type CloningStrategy, type Comparable, type ComparableProperty, type Comparator, type ComparatorMapping, type CompatibleProperty, type Crush, type CustomClass, type CustomClassRegistry, type DebounceFunction, type DebounceOptions, DefaultCloningStrategy, type Err, type ExtractArray, type ExtractClass, type ExtractMap, type ExtractNotAny, type ExtractSet, type Falsy, FastCloningStrategy, type FilteredKeys, type Flip, type Intersect, type IsExactType, type KeyFilter, type KeyFilterFunction, type LowercaseKeys, type MappedInput, type MappedOutput, type Mapping, type MappingFunction, type MemoOptions, type NoInfer, type Ok, type OnceFunction, type OptionalKeys, type OptionalMapping, type ParallelOptions, type Primitive, type PromiseWithResolvers, type RequiredKeys, type Result, type ResultPromise, type RetryOptions, type Series, type Simplify, type StrictExtract, type SwitchAny, type SwitchNever, type ThrottledFunction, type ToEmpty, type ToEmptyAble, type TraverseContext, type TraverseOptions, type TraverseVisitor, type TryitResult, type TypedArray, type UppercaseKeys, all, alphabetical, always, assign, boil, callable, camel, capitalize, cartesianProduct, castArray, castArrayIfExists, castComparator, castMapping, chain, clamp, clone, cloneDeep, cluster, compose, construct, counting, crush, dash, debounce, dedent, defer, diff, draw, filterKey, first, flat, flip, fork, get, group, guard, inRange, intersects, invert, isArray, isBoolean, isClass, isDate, isEmpty, isEqual, isError, isFloat, isFunction, isInt, isIntString, isIterable, isMap, isNullish, isNumber, isObject, isPlainObject, isPrimitive, isPromise, isRegExp, isResult, isResultErr, isResultOk, isSet, isString, isSymbol, isTagged, isWeakMap, isWeakSet, iterate, keys, last, lerp, list, listify, lowerize, map, mapEntries, mapKeys, mapValues, mapify, max, memo, merge, min, noop, objectify, omit, once, parallel, partial, partob, pascal, pick, proxied, random, range, reduce, replace, replaceOrAppend, retry, round, select, selectFirst, series, set, shake, shift, shuffle, sift, similarity, sleep, snake, sort, sum, template, throttle, timeout, title, toFloat, toInt, toggle, traverse, trim, tryit as try, tryit, uid, unique, unzip, upperize, withResolvers, zip, zipToObject };
|
package/dist/radashi.d.ts
CHANGED
|
@@ -21,6 +21,29 @@ declare function alphabetical<T>(array: readonly T[], getter: (item: T) => strin
|
|
|
21
21
|
*/
|
|
22
22
|
declare function boil<T>(array: readonly T[], compareFunc: (a: T, b: T) => T): T | null;
|
|
23
23
|
|
|
24
|
+
/**
|
|
25
|
+
* Create an [n-ary Cartesian product](https://en.wikipedia.org/wiki/Cartesian_product#n-ary_Cartesian_product) from the given arrays.
|
|
26
|
+
*
|
|
27
|
+
* @see https://radashi.js.org/reference/array/cartesianProduct
|
|
28
|
+
* @example
|
|
29
|
+
* ```ts
|
|
30
|
+
* product([['red', 'blue'], ['big', 'small'], ['fast', 'slow']])
|
|
31
|
+
* // [
|
|
32
|
+
* // ['red', 'big', 'fast'],
|
|
33
|
+
* // ['red', 'big', 'slow'],
|
|
34
|
+
* // ['red', 'small', 'fast'],
|
|
35
|
+
* // ['red', 'small', 'slow'],
|
|
36
|
+
* // ['blue', 'big', 'fast'],
|
|
37
|
+
* // ['blue', 'big', 'slow'],
|
|
38
|
+
* // ['blue', 'small', 'fast'],
|
|
39
|
+
* // ['blue', 'small', 'slow']
|
|
40
|
+
* // ]
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
declare function cartesianProduct<T extends any[][]>(...arrays: [...T]): Array<{
|
|
44
|
+
[K in keyof T]: T[K][number];
|
|
45
|
+
}>;
|
|
46
|
+
|
|
24
47
|
/**
|
|
25
48
|
* Casts the given value to an array. If the value is already an
|
|
26
49
|
* array, a shallow copy is returned. Otherwise, a new array
|
|
@@ -645,23 +668,44 @@ declare function guard<TFunction extends () => any>(func: TFunction, shouldGuard
|
|
|
645
668
|
*/
|
|
646
669
|
declare function map<T, K>(array: readonly T[], asyncMapFunc: (item: T, index: number) => Promise<K>): Promise<K[]>;
|
|
647
670
|
|
|
671
|
+
type AbortSignal$1 = {
|
|
672
|
+
readonly aborted: boolean;
|
|
673
|
+
addEventListener(type: 'abort', listener: () => void): void;
|
|
674
|
+
removeEventListener(type: 'abort', listener: () => void): void;
|
|
675
|
+
throwIfAborted(): void;
|
|
676
|
+
};
|
|
677
|
+
type ParallelOptions = {
|
|
678
|
+
limit: number;
|
|
679
|
+
signal?: AbortSignal$1;
|
|
680
|
+
} | number;
|
|
648
681
|
/**
|
|
649
682
|
* Executes many async functions in parallel. Returns the results from
|
|
650
683
|
* all functions as an array. After all functions have resolved, if
|
|
651
684
|
* any errors were thrown, they are rethrown in an instance of
|
|
652
|
-
* AggregateError.
|
|
685
|
+
* AggregateError. The operation can be aborted by passing optional AbortSignal,
|
|
686
|
+
* which will throw an Error if aborted.
|
|
653
687
|
*
|
|
654
688
|
* @see https://radashi.js.org/reference/async/parallel
|
|
655
689
|
* @example
|
|
656
690
|
* ```ts
|
|
657
691
|
* // Process images concurrently, resizing each image to a standard size.
|
|
658
|
-
* const
|
|
692
|
+
* const abortController = new AbortController();
|
|
693
|
+
* const images = await parallel(
|
|
694
|
+
* {
|
|
695
|
+
* limit: 2,
|
|
696
|
+
* signal: abortController.signal,
|
|
697
|
+
* },
|
|
698
|
+
* imageFiles,
|
|
699
|
+
* async file => {
|
|
659
700
|
* return await resizeImage(file)
|
|
660
701
|
* })
|
|
702
|
+
*
|
|
703
|
+
* // To abort the operation:
|
|
704
|
+
* // abortController.abort()
|
|
661
705
|
* ```
|
|
662
706
|
* @version 12.1.0
|
|
663
707
|
*/
|
|
664
|
-
declare function parallel<T, K>(
|
|
708
|
+
declare function parallel<T, K>(options: ParallelOptions, array: readonly T[], func: (item: T) => Promise<K>): Promise<K[]>;
|
|
665
709
|
|
|
666
710
|
/**
|
|
667
711
|
* An async reduce function. Works like the built-in Array.reduce
|
|
@@ -678,10 +722,14 @@ declare function parallel<T, K>(limit: number, array: readonly T[], func: (item:
|
|
|
678
722
|
*/
|
|
679
723
|
declare function reduce<T, K>(array: readonly T[], asyncReducer: (acc: K, item: T, index: number) => Promise<K>, initValue?: K): Promise<K>;
|
|
680
724
|
|
|
725
|
+
type AbortSignal = {
|
|
726
|
+
throwIfAborted(): void;
|
|
727
|
+
};
|
|
681
728
|
type RetryOptions = {
|
|
682
729
|
times?: number;
|
|
683
730
|
delay?: number | null;
|
|
684
731
|
backoff?: (count: number) => number;
|
|
732
|
+
signal?: AbortSignal;
|
|
685
733
|
};
|
|
686
734
|
/**
|
|
687
735
|
* Retries the given function the specified number of times.
|
|
@@ -689,9 +737,12 @@ type RetryOptions = {
|
|
|
689
737
|
* @see https://radashi.js.org/reference/async/retry
|
|
690
738
|
* @example
|
|
691
739
|
* ```ts
|
|
692
|
-
* const
|
|
740
|
+
* const abortController = new AbortController();
|
|
741
|
+
* const result = await retry({ times: 3, delay: 1000, signal: abortController.signal }, async () => {
|
|
693
742
|
* return await fetch('https://example.com')
|
|
694
743
|
* })
|
|
744
|
+
* // To abort the operation:
|
|
745
|
+
* // abortController.abort()
|
|
695
746
|
* ```
|
|
696
747
|
* @version 12.1.0
|
|
697
748
|
*/
|
|
@@ -709,6 +760,39 @@ declare function retry<TResponse>(options: RetryOptions, func: (exit: (err: any)
|
|
|
709
760
|
*/
|
|
710
761
|
declare function sleep(milliseconds: number): Promise<void>;
|
|
711
762
|
|
|
763
|
+
/**
|
|
764
|
+
* Creates a promise that will reject after a specified amount of time.
|
|
765
|
+
* You can provide a custom error message or a function that returns an error.
|
|
766
|
+
*
|
|
767
|
+
* @see https://radashi.js.org/reference/async/timeout
|
|
768
|
+
*
|
|
769
|
+
* @example
|
|
770
|
+
* ```ts
|
|
771
|
+
* // Reject after 1000 milliseconds with default message "timeout"
|
|
772
|
+
* await timeout(1000)
|
|
773
|
+
*
|
|
774
|
+
* // Reject after 1000 milliseconds with a custom message
|
|
775
|
+
* await timeout(1000, "Optional message")
|
|
776
|
+
*
|
|
777
|
+
* // Reject after 1000 milliseconds with a custom error
|
|
778
|
+
* await timeout(1000, () => new Error("Custom error"))
|
|
779
|
+
*
|
|
780
|
+
* // Example usage with Promise.race to set a timeout for an asynchronous task
|
|
781
|
+
* await Promise.race([
|
|
782
|
+
* someAsyncTask(),
|
|
783
|
+
* timeout(1000, "Optional message"),
|
|
784
|
+
* ])
|
|
785
|
+
* ```
|
|
786
|
+
* @version 12.3.0
|
|
787
|
+
*/
|
|
788
|
+
declare function timeout<E extends Error>(milliseconds: number,
|
|
789
|
+
/**
|
|
790
|
+
* The error message to reject with.
|
|
791
|
+
*
|
|
792
|
+
* @default "timeout"
|
|
793
|
+
*/
|
|
794
|
+
error?: string | (() => E)): Promise<void>;
|
|
795
|
+
|
|
712
796
|
/**
|
|
713
797
|
* The result of a `tryit` function.
|
|
714
798
|
*
|
|
@@ -2099,6 +2183,44 @@ declare function capitalize(str: string): string;
|
|
|
2099
2183
|
*/
|
|
2100
2184
|
declare function dash(str: string): string;
|
|
2101
2185
|
|
|
2186
|
+
/**
|
|
2187
|
+
* Remove indentation from a string. The given string is expected to
|
|
2188
|
+
* be consistently indented (i.e. the leading whitespace of the first
|
|
2189
|
+
* non-empty line is the minimum required for all non-empty lines).
|
|
2190
|
+
*
|
|
2191
|
+
* If the `indent` argument is nullish, the indentation is detected
|
|
2192
|
+
* from the first non-empty line. Detection is cheap and robust for
|
|
2193
|
+
* most use cases, so you should only set an explicit `indent` if
|
|
2194
|
+
* necessary.
|
|
2195
|
+
*
|
|
2196
|
+
* @see https://radashi-org.github.io/reference/string/dedent
|
|
2197
|
+
* @example
|
|
2198
|
+
* ```ts
|
|
2199
|
+
* // This is indented with 4 spaces.
|
|
2200
|
+
* const input = `
|
|
2201
|
+
* Hello
|
|
2202
|
+
* World
|
|
2203
|
+
* `
|
|
2204
|
+
*
|
|
2205
|
+
* // Explicit indentation
|
|
2206
|
+
* dedent(input, ' ')
|
|
2207
|
+
* // => ' Hello\n World\n'
|
|
2208
|
+
*
|
|
2209
|
+
* // Detected indentation
|
|
2210
|
+
* dedent(input)
|
|
2211
|
+
* // => 'Hello\nWorld\n'
|
|
2212
|
+
*
|
|
2213
|
+
* // Tagged template strings
|
|
2214
|
+
* const str = dedent`
|
|
2215
|
+
* Foo ${1 + 1}
|
|
2216
|
+
* Bar ${2 * 2}
|
|
2217
|
+
* `
|
|
2218
|
+
* // => 'Foo 2\nBar 4'
|
|
2219
|
+
* ```
|
|
2220
|
+
*/
|
|
2221
|
+
declare function dedent(template: TemplateStringsArray, ...values: unknown[]): string;
|
|
2222
|
+
declare function dedent(text: string, indent?: string | null): string;
|
|
2223
|
+
|
|
2102
2224
|
/**
|
|
2103
2225
|
* Formats the given string in pascal case fashion.
|
|
2104
2226
|
*
|
|
@@ -2225,6 +2347,27 @@ type ExtractArray<T> = T extends any ? [StrictExtract<T, readonly any[]>] extend
|
|
|
2225
2347
|
|
|
2226
2348
|
declare function isBoolean(value: unknown): value is boolean;
|
|
2227
2349
|
|
|
2350
|
+
/**
|
|
2351
|
+
* Checks if the given value is a class. This function verifies
|
|
2352
|
+
* if the value was defined using the `class` syntax. Old school
|
|
2353
|
+
* classes (defined with constructor functions) will return false.
|
|
2354
|
+
* "Native classes" like `Error` will also return false.
|
|
2355
|
+
*
|
|
2356
|
+
* @see https://radashi.js.org/reference/typed/isClass
|
|
2357
|
+
* @example
|
|
2358
|
+
* ```ts
|
|
2359
|
+
* isClass(class CustomClass {}) // => true
|
|
2360
|
+
* isClass('abc') // => false
|
|
2361
|
+
* isClass({}) // => false
|
|
2362
|
+
* ```
|
|
2363
|
+
*/
|
|
2364
|
+
declare function isClass<T>(value: T): value is ExtractClass<T>;
|
|
2365
|
+
/**
|
|
2366
|
+
* Used by the `isClass` type guard. It handles type narrowing for
|
|
2367
|
+
* class constructors and even narrows `any` types.
|
|
2368
|
+
*/
|
|
2369
|
+
type ExtractClass<T> = [StrictExtract<T, Class>] extends [Class] ? Extract<T, Class> : T extends any ? Class<unknown[], unknown> extends T ? Class<unknown[], unknown> : never : never;
|
|
2370
|
+
|
|
2228
2371
|
/**
|
|
2229
2372
|
* Return true if the given value is a Date object.
|
|
2230
2373
|
*
|
|
@@ -2403,6 +2546,21 @@ type ExtractMap<T> = T extends any ? [StrictExtract<T, ReadonlyMap<unknown, unkn
|
|
|
2403
2546
|
ReadonlyMap<unknown, unknown>
|
|
2404
2547
|
] ? Extract<T, ReadonlyMap<unknown, unknown>> : [StrictExtract<T, Map<unknown, unknown>>] extends [Map<unknown, unknown>] ? Extract<T, Map<unknown, unknown>> : Map<unknown, unknown> extends T ? Map<unknown, unknown> : never : never;
|
|
2405
2548
|
|
|
2549
|
+
/**
|
|
2550
|
+
* Return true if the given value is null or undefined.
|
|
2551
|
+
*
|
|
2552
|
+
* @see https://radashi.js.org/reference/typed/isNullish
|
|
2553
|
+
* @example
|
|
2554
|
+
* ```ts
|
|
2555
|
+
* isNullish(null) // => true
|
|
2556
|
+
* isNullish(undefined) // => true
|
|
2557
|
+
* isNullish('') // => false
|
|
2558
|
+
* isNullish(0) // => false
|
|
2559
|
+
* ```
|
|
2560
|
+
* @version 12.2.0
|
|
2561
|
+
*/
|
|
2562
|
+
declare function isNullish(value: unknown): value is null | undefined;
|
|
2563
|
+
|
|
2406
2564
|
/**
|
|
2407
2565
|
* Return true if the given value is a number.
|
|
2408
2566
|
*
|
|
@@ -3182,6 +3340,10 @@ type Falsy = null | undefined | false | '' | 0 | 0n;
|
|
|
3182
3340
|
declare class Any {
|
|
3183
3341
|
private any;
|
|
3184
3342
|
}
|
|
3343
|
+
/**
|
|
3344
|
+
* Represents a class constructor.
|
|
3345
|
+
*/
|
|
3346
|
+
type Class<TArgs extends any[] = any[], TReturn = any> = new (...args: TArgs) => TReturn;
|
|
3185
3347
|
/**
|
|
3186
3348
|
* Extracts `T` if `T` is not `any`, otherwise `never`.
|
|
3187
3349
|
*
|
|
@@ -3379,4 +3541,4 @@ type GlobalObjectType<Identifier extends string> = [Identifier] extends [Any] ?
|
|
|
3379
3541
|
[P in Identifier]: any;
|
|
3380
3542
|
} ? InstanceType<(typeof globalThis)[Identifier]> : never;
|
|
3381
3543
|
|
|
3382
|
-
export { AggregateErrorOrPolyfill as AggregateError, Any, type Assign, type BoxedPrimitive, type BuiltInType, type CastArray, type CastArrayIfExists, type CloningStrategy, type Comparable, type ComparableProperty, type Comparator, type ComparatorMapping, type CompatibleProperty, type Crush, type CustomClass, type CustomClassRegistry, type DebounceFunction, type DebounceOptions, DefaultCloningStrategy, type Err, type ExtractArray, type ExtractMap, type ExtractNotAny, type ExtractSet, type Falsy, FastCloningStrategy, type FilteredKeys, type Flip, type Intersect, type IsExactType, type KeyFilter, type KeyFilterFunction, type LowercaseKeys, type MappedInput, type MappedOutput, type Mapping, type MappingFunction, type MemoOptions, type NoInfer, type Ok, type OnceFunction, type OptionalKeys, type OptionalMapping, type Primitive, type PromiseWithResolvers, type RequiredKeys, type Result, type ResultPromise, type RetryOptions, type Series, type Simplify, type StrictExtract, type SwitchAny, type SwitchNever, type ThrottledFunction, type ToEmpty, type ToEmptyAble, type TraverseContext, type TraverseOptions, type TraverseVisitor, type TryitResult, type TypedArray, type UppercaseKeys, all, alphabetical, always, assign, boil, callable, camel, capitalize, castArray, castArrayIfExists, castComparator, castMapping, chain, clamp, clone, cloneDeep, cluster, compose, construct, counting, crush, dash, debounce, defer, diff, draw, filterKey, first, flat, flip, fork, get, group, guard, inRange, intersects, invert, isArray, isBoolean, isDate, isEmpty, isEqual, isError, isFloat, isFunction, isInt, isIntString, isIterable, isMap, isNumber, isObject, isPlainObject, isPrimitive, isPromise, isRegExp, isResult, isResultErr, isResultOk, isSet, isString, isSymbol, isTagged, isWeakMap, isWeakSet, iterate, keys, last, lerp, list, listify, lowerize, map, mapEntries, mapKeys, mapValues, mapify, max, memo, merge, min, noop, objectify, omit, once, parallel, partial, partob, pascal, pick, proxied, random, range, reduce, replace, replaceOrAppend, retry, round, select, selectFirst, series, set, shake, shift, shuffle, sift, similarity, sleep, snake, sort, sum, template, throttle, title, toFloat, toInt, toggle, traverse, trim, tryit as try, tryit, uid, unique, unzip, upperize, withResolvers, zip, zipToObject };
|
|
3544
|
+
export { AggregateErrorOrPolyfill as AggregateError, Any, type Assign, type BoxedPrimitive, type BuiltInType, type CastArray, type CastArrayIfExists, type Class, type CloningStrategy, type Comparable, type ComparableProperty, type Comparator, type ComparatorMapping, type CompatibleProperty, type Crush, type CustomClass, type CustomClassRegistry, type DebounceFunction, type DebounceOptions, DefaultCloningStrategy, type Err, type ExtractArray, type ExtractClass, type ExtractMap, type ExtractNotAny, type ExtractSet, type Falsy, FastCloningStrategy, type FilteredKeys, type Flip, type Intersect, type IsExactType, type KeyFilter, type KeyFilterFunction, type LowercaseKeys, type MappedInput, type MappedOutput, type Mapping, type MappingFunction, type MemoOptions, type NoInfer, type Ok, type OnceFunction, type OptionalKeys, type OptionalMapping, type ParallelOptions, type Primitive, type PromiseWithResolvers, type RequiredKeys, type Result, type ResultPromise, type RetryOptions, type Series, type Simplify, type StrictExtract, type SwitchAny, type SwitchNever, type ThrottledFunction, type ToEmpty, type ToEmptyAble, type TraverseContext, type TraverseOptions, type TraverseVisitor, type TryitResult, type TypedArray, type UppercaseKeys, all, alphabetical, always, assign, boil, callable, camel, capitalize, cartesianProduct, castArray, castArrayIfExists, castComparator, castMapping, chain, clamp, clone, cloneDeep, cluster, compose, construct, counting, crush, dash, debounce, dedent, defer, diff, draw, filterKey, first, flat, flip, fork, get, group, guard, inRange, intersects, invert, isArray, isBoolean, isClass, isDate, isEmpty, isEqual, isError, isFloat, isFunction, isInt, isIntString, isIterable, isMap, isNullish, isNumber, isObject, isPlainObject, isPrimitive, isPromise, isRegExp, isResult, isResultErr, isResultOk, isSet, isString, isSymbol, isTagged, isWeakMap, isWeakSet, iterate, keys, last, lerp, list, listify, lowerize, map, mapEntries, mapKeys, mapValues, mapify, max, memo, merge, min, noop, objectify, omit, once, parallel, partial, partob, pascal, pick, proxied, random, range, reduce, replace, replaceOrAppend, retry, round, select, selectFirst, series, set, shake, shift, shuffle, sift, similarity, sleep, snake, sort, sum, template, throttle, timeout, title, toFloat, toInt, toggle, traverse, trim, tryit as try, tryit, uid, unique, unzip, upperize, withResolvers, zip, zipToObject };
|
package/dist/radashi.js
CHANGED
|
@@ -16,6 +16,23 @@ function boil(array, compareFunc) {
|
|
|
16
16
|
return array.reduce(compareFunc);
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
+
// src/array/cartesianProduct.ts
|
|
20
|
+
function cartesianProduct(...arrays) {
|
|
21
|
+
let out = [[]];
|
|
22
|
+
for (const array of arrays) {
|
|
23
|
+
const result = [];
|
|
24
|
+
for (const currentArray of out) {
|
|
25
|
+
for (const item of array) {
|
|
26
|
+
const currentArrayCopy = currentArray.slice();
|
|
27
|
+
currentArrayCopy.push(item);
|
|
28
|
+
result.push(currentArrayCopy);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
out = result;
|
|
32
|
+
}
|
|
33
|
+
return out;
|
|
34
|
+
}
|
|
35
|
+
|
|
19
36
|
// src/array/castArray.ts
|
|
20
37
|
function castArray(value) {
|
|
21
38
|
return Array.isArray(value) ? value.slice() : [value];
|
|
@@ -443,17 +460,27 @@ async function map(array, asyncMapFunc) {
|
|
|
443
460
|
}
|
|
444
461
|
|
|
445
462
|
// src/async/parallel.ts
|
|
446
|
-
async function parallel(
|
|
463
|
+
async function parallel(options, array, func) {
|
|
464
|
+
var _a;
|
|
447
465
|
const work = array.map((item, index) => ({
|
|
448
466
|
index,
|
|
449
467
|
item
|
|
450
468
|
}));
|
|
451
|
-
|
|
469
|
+
if (isNumber(options)) {
|
|
470
|
+
options = {
|
|
471
|
+
limit: options
|
|
472
|
+
};
|
|
473
|
+
}
|
|
474
|
+
(_a = options.signal) == null ? void 0 : _a.throwIfAborted();
|
|
475
|
+
const processor = async (resolve, reject) => {
|
|
476
|
+
var _a2, _b;
|
|
452
477
|
const results2 = [];
|
|
478
|
+
const abortListener = () => reject(new Error("This operation was aborted"));
|
|
479
|
+
(_a2 = options.signal) == null ? void 0 : _a2.addEventListener("abort", abortListener);
|
|
453
480
|
while (true) {
|
|
454
481
|
const next = work.pop();
|
|
455
482
|
if (!next) {
|
|
456
|
-
|
|
483
|
+
break;
|
|
457
484
|
}
|
|
458
485
|
const [error, result] = await tryit(func)(next.item);
|
|
459
486
|
results2.push({
|
|
@@ -462,8 +489,10 @@ async function parallel(limit, array, func) {
|
|
|
462
489
|
index: next.index
|
|
463
490
|
});
|
|
464
491
|
}
|
|
492
|
+
(_b = options.signal) == null ? void 0 : _b.removeEventListener("abort", abortListener);
|
|
493
|
+
return resolve(results2);
|
|
465
494
|
};
|
|
466
|
-
const queues = list(1, limit).map(() => new Promise(processor));
|
|
495
|
+
const queues = list(1, options.limit).map(() => new Promise(processor));
|
|
467
496
|
const itemResults = await Promise.all(queues);
|
|
468
497
|
const [errors, results] = fork(
|
|
469
498
|
sort(flat(itemResults), (r) => r.index),
|
|
@@ -494,11 +523,13 @@ async function retry(options, func) {
|
|
|
494
523
|
const times = (options == null ? void 0 : options.times) ?? 3;
|
|
495
524
|
const delay = options == null ? void 0 : options.delay;
|
|
496
525
|
const backoff = (options == null ? void 0 : options.backoff) ?? null;
|
|
526
|
+
const signal = options == null ? void 0 : options.signal;
|
|
497
527
|
let i = 0;
|
|
498
528
|
while (true) {
|
|
499
529
|
const [err, result] = await tryit(func)((err2) => {
|
|
500
530
|
throw { _exited: err2 };
|
|
501
531
|
});
|
|
532
|
+
signal == null ? void 0 : signal.throwIfAborted();
|
|
502
533
|
if (!err) {
|
|
503
534
|
return result;
|
|
504
535
|
}
|
|
@@ -522,6 +553,19 @@ function sleep(milliseconds) {
|
|
|
522
553
|
return new Promise((res) => setTimeout(res, milliseconds));
|
|
523
554
|
}
|
|
524
555
|
|
|
556
|
+
// src/async/timeout.ts
|
|
557
|
+
function timeout(milliseconds, error = "timeout") {
|
|
558
|
+
return new Promise(
|
|
559
|
+
(_, rej) => setTimeout(() => {
|
|
560
|
+
if (isString(error)) {
|
|
561
|
+
rej(new Error(error));
|
|
562
|
+
} else {
|
|
563
|
+
rej(error());
|
|
564
|
+
}
|
|
565
|
+
}, milliseconds)
|
|
566
|
+
);
|
|
567
|
+
}
|
|
568
|
+
|
|
525
569
|
// src/async/tryit.ts
|
|
526
570
|
function tryit(func) {
|
|
527
571
|
return (...args) => {
|
|
@@ -1367,6 +1411,30 @@ function dash(str) {
|
|
|
1367
1411
|
});
|
|
1368
1412
|
}
|
|
1369
1413
|
|
|
1414
|
+
// src/string/dedent.ts
|
|
1415
|
+
function dedent(text, ...values) {
|
|
1416
|
+
var _a;
|
|
1417
|
+
if (isArray(text)) {
|
|
1418
|
+
if (values.length > 0) {
|
|
1419
|
+
return dedent(
|
|
1420
|
+
text.reduce((acc, input, i) => {
|
|
1421
|
+
var _a2;
|
|
1422
|
+
let value = String(values[i] ?? "");
|
|
1423
|
+
const indent2 = value.includes("\n") && ((_a2 = input.match(/[ \t]*(?=[^\n]*$)/)) == null ? void 0 : _a2[0]);
|
|
1424
|
+
if (indent2) {
|
|
1425
|
+
value = value.replace(/\n(?=[^\n]*?\S)/g, "\n" + indent2);
|
|
1426
|
+
}
|
|
1427
|
+
return acc + input + value;
|
|
1428
|
+
}, "")
|
|
1429
|
+
);
|
|
1430
|
+
}
|
|
1431
|
+
text = text[0];
|
|
1432
|
+
}
|
|
1433
|
+
const indent = values[0] ?? ((_a = text.match(/^[ \t]*(?=\S)/m)) == null ? void 0 : _a[0]);
|
|
1434
|
+
const output = indent ? text.replace(new RegExp(`^${indent}`, "gm"), "") : text;
|
|
1435
|
+
return output.replace(/^[ \t]*\n|\n[ \t]*$/g, "");
|
|
1436
|
+
}
|
|
1437
|
+
|
|
1370
1438
|
// src/string/pascal.ts
|
|
1371
1439
|
function pascal(str) {
|
|
1372
1440
|
if (!str) {
|
|
@@ -1485,6 +1553,11 @@ function isBoolean(value) {
|
|
|
1485
1553
|
return typeof value === "boolean";
|
|
1486
1554
|
}
|
|
1487
1555
|
|
|
1556
|
+
// src/typed/isClass.ts
|
|
1557
|
+
function isClass(value) {
|
|
1558
|
+
return isFunction(value) && Function.prototype.toString.call(value).startsWith("class ");
|
|
1559
|
+
}
|
|
1560
|
+
|
|
1488
1561
|
// src/typed/isDate.ts
|
|
1489
1562
|
function isDate(value) {
|
|
1490
1563
|
return isTagged(value, "[object Date]");
|
|
@@ -1589,6 +1662,11 @@ function isMap(value) {
|
|
|
1589
1662
|
return isTagged(value, "[object Map]");
|
|
1590
1663
|
}
|
|
1591
1664
|
|
|
1665
|
+
// src/typed/isNullish.ts
|
|
1666
|
+
function isNullish(value) {
|
|
1667
|
+
return value === null || value === void 0;
|
|
1668
|
+
}
|
|
1669
|
+
|
|
1592
1670
|
// src/typed/isNumber.ts
|
|
1593
1671
|
function isNumber(value) {
|
|
1594
1672
|
return typeof value === "number" && !Number.isNaN(value);
|
|
@@ -1673,4 +1751,4 @@ function isWeakSet(value) {
|
|
|
1673
1751
|
return isTagged(value, "[object WeakSet]");
|
|
1674
1752
|
}
|
|
1675
1753
|
|
|
1676
|
-
export { AggregateErrorOrPolyfill as AggregateError, DefaultCloningStrategy, FastCloningStrategy, all, alphabetical, always, assign, boil, callable, camel, capitalize, castArray, castArrayIfExists, castComparator, castMapping, chain, clamp, clone, cloneDeep, cluster, compose, construct, counting, crush, dash, debounce, defer, diff, draw, filterKey, first, flat, flip, fork, get, group, guard, inRange, intersects, invert, isArray, isBoolean, isDate, isEmpty, isEqual, isError, isFloat, isFunction, isInt, isIntString, isIterable, isMap, isNumber, isObject, isPlainObject, isPrimitive, isPromise, isRegExp, isResult, isResultErr, isResultOk, isSet, isString, isSymbol, isTagged, isWeakMap, isWeakSet, iterate, keys, last, lerp, list, listify, lowerize, map, mapEntries, mapKeys, mapValues, mapify, max, memo, merge, min, noop, objectify, omit, once, parallel, partial, partob, pascal, pick, proxied, random, range, reduce, replace, replaceOrAppend, retry, round, select, selectFirst, series, set, shake, shift, shuffle, sift, similarity, sleep, snake, sort, sum, template, throttle, title, toFloat, toInt, toggle, traverse, trim, tryit as try, tryit, uid, unique, unzip, upperize, withResolvers, zip, zipToObject };
|
|
1754
|
+
export { AggregateErrorOrPolyfill as AggregateError, DefaultCloningStrategy, FastCloningStrategy, all, alphabetical, always, assign, boil, callable, camel, capitalize, cartesianProduct, castArray, castArrayIfExists, castComparator, castMapping, chain, clamp, clone, cloneDeep, cluster, compose, construct, counting, crush, dash, debounce, dedent, defer, diff, draw, filterKey, first, flat, flip, fork, get, group, guard, inRange, intersects, invert, isArray, isBoolean, isClass, isDate, isEmpty, isEqual, isError, isFloat, isFunction, isInt, isIntString, isIterable, isMap, isNullish, isNumber, isObject, isPlainObject, isPrimitive, isPromise, isRegExp, isResult, isResultErr, isResultOk, isSet, isString, isSymbol, isTagged, isWeakMap, isWeakSet, iterate, keys, last, lerp, list, listify, lowerize, map, mapEntries, mapKeys, mapValues, mapify, max, memo, merge, min, noop, objectify, omit, once, parallel, partial, partob, pascal, pick, proxied, random, range, reduce, replace, replaceOrAppend, retry, round, select, selectFirst, series, set, shake, shift, shuffle, sift, similarity, sleep, snake, sort, sum, template, throttle, timeout, title, toFloat, toInt, toggle, traverse, trim, tryit as try, tryit, uid, unique, unzip, upperize, withResolvers, zip, zipToObject };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "radashi",
|
|
3
|
-
"version": "12.
|
|
3
|
+
"version": "12.3.0-beta.8c8abf6",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "The modern, community-first TypeScript toolkit with all of the fast, readable, and minimal utility functions you need. Type-safe, dependency-free, tree-shakeable, fully tested.",
|
|
6
6
|
"repository": {
|