es-toolkit 1.30.1 → 1.31.0-dev.1001
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/CHANGELOG.md +13 -0
- package/dist/_chunk/AbortError-Cg4ZQ1.js +10 -0
- package/dist/_chunk/delay-_VMfFa.js +25 -0
- package/dist/_chunk/isPlainObject-Xaozpc.js +93 -0
- package/dist/_chunk/{isWeakSet-CvIdTA.js → isWeakSet-BW79-8.js} +37 -71
- package/dist/_chunk/{toMerged-DGFrN7.js → toMerged-BQTfB8.js} +39 -8
- package/dist/_chunk/{unary-CcTNuC.js → unary-c1NFA5.js} +38 -0
- package/dist/_chunk/{zipWith-nbzldx.js → zipWith-Bdyzuy.js} +47 -4
- package/dist/array/at.d.mts +1 -1
- package/dist/array/at.d.ts +1 -1
- package/dist/array/groupBy.mjs +2 -2
- package/dist/array/index.d.mts +2 -0
- package/dist/array/index.d.ts +2 -0
- package/dist/array/index.js +3 -1
- package/dist/array/index.mjs +2 -0
- package/dist/array/pull.mjs +9 -2
- package/dist/array/pullAt.d.mts +1 -1
- package/dist/array/pullAt.d.ts +1 -1
- package/dist/array/remove.d.mts +19 -0
- package/dist/array/remove.d.ts +19 -0
- package/dist/array/remove.mjs +20 -0
- package/dist/array/windowed.d.mts +50 -0
- package/dist/array/windowed.d.ts +50 -0
- package/dist/array/windowed.mjs +16 -0
- package/dist/browser.global.js +1 -1
- package/dist/browser.global.js.map +1 -1
- package/dist/compat/array/find.d.mts +16 -8
- package/dist/compat/array/find.d.ts +16 -8
- package/dist/compat/array/find.mjs +5 -2
- package/dist/compat/array/pull.d.mts +1 -1
- package/dist/compat/array/pull.d.ts +1 -1
- package/dist/compat/array/pull.mjs +1 -2
- package/dist/compat/array/pullAll.d.mts +19 -0
- package/dist/compat/array/pullAll.d.ts +19 -0
- package/dist/compat/array/pullAll.mjs +7 -0
- package/dist/compat/array/remove.d.mts +50 -0
- package/dist/compat/array/remove.d.ts +50 -0
- package/dist/compat/array/remove.mjs +8 -0
- package/dist/compat/array/reverse.d.mts +24 -0
- package/dist/compat/array/reverse.d.ts +24 -0
- package/dist/compat/array/reverse.mjs +8 -0
- package/dist/compat/function/negate.d.mts +16 -0
- package/dist/compat/function/negate.d.ts +16 -0
- package/dist/compat/function/negate.mjs +10 -0
- package/dist/compat/index.d.mts +14 -2
- package/dist/compat/index.d.ts +14 -2
- package/dist/compat/index.js +114 -41
- package/dist/compat/index.mjs +14 -2
- package/dist/compat/math/multiply.d.mts +18 -0
- package/dist/compat/math/multiply.d.ts +18 -0
- package/dist/compat/math/multiply.mjs +21 -0
- package/dist/compat/math/subtract.d.mts +17 -0
- package/dist/compat/math/subtract.d.ts +17 -0
- package/dist/compat/math/subtract.mjs +5 -0
- package/dist/compat/object/mergeWith.mjs +6 -2
- package/dist/compat/predicate/isBuffer.d.mts +21 -0
- package/dist/compat/predicate/isBuffer.d.ts +21 -0
- package/dist/compat/predicate/isBuffer.mjs +7 -0
- package/dist/compat/predicate/isMatch.mjs +1 -4
- package/dist/compat/util/methodOf.d.mts +22 -0
- package/dist/compat/util/methodOf.d.ts +22 -0
- package/dist/compat/util/methodOf.mjs +9 -0
- package/dist/error/index.d.mts +2 -0
- package/dist/error/index.d.ts +2 -0
- package/dist/error/index.js +15 -0
- package/dist/error/index.mjs +2 -0
- package/dist/function/asyncNoop.d.mts +12 -0
- package/dist/function/asyncNoop.d.ts +12 -0
- package/dist/function/asyncNoop.mjs +3 -0
- package/dist/function/index.d.mts +2 -0
- package/dist/function/index.d.ts +2 -0
- package/dist/function/index.js +3 -1
- package/dist/function/index.mjs +2 -0
- package/dist/function/retry.d.mts +59 -0
- package/dist/function/retry.d.ts +59 -0
- package/dist/function/retry.mjs +35 -0
- package/dist/index.d.mts +7 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +21 -11
- package/dist/index.mjs +7 -0
- package/dist/object/cloneDeepWith.mjs +34 -1
- package/dist/object/flattenObject.d.mts +10 -2
- package/dist/object/flattenObject.d.ts +10 -2
- package/dist/object/flattenObject.mjs +6 -6
- package/dist/object/index.js +1 -1
- package/dist/predicate/index.d.mts +1 -0
- package/dist/predicate/index.d.ts +1 -0
- package/dist/predicate/index.js +3 -2
- package/dist/predicate/index.mjs +1 -0
- package/dist/predicate/isPromise.d.mts +20 -0
- package/dist/predicate/isPromise.d.ts +20 -0
- package/dist/predicate/isPromise.mjs +5 -0
- package/dist/promise/index.d.mts +2 -0
- package/dist/promise/index.d.ts +2 -0
- package/dist/promise/index.js +56 -4
- package/dist/promise/index.mjs +2 -0
- package/dist/promise/mutex.d.mts +64 -0
- package/dist/promise/mutex.d.ts +64 -0
- package/dist/promise/mutex.mjs +16 -0
- package/dist/promise/semaphore.d.mts +81 -0
- package/dist/promise/semaphore.d.ts +81 -0
- package/dist/promise/semaphore.mjs +30 -0
- package/error.d.ts +1 -0
- package/package.json +42 -21
- package/dist/_chunk/index-BGZDR9.js +0 -50
- package/dist/_chunk/isPlainObject-octpoD.js +0 -32
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Subtracts one number from another.
|
|
3
|
+
*
|
|
4
|
+
* If either of the numbers is `NaN`, the function returns `NaN`.
|
|
5
|
+
*
|
|
6
|
+
* @param {number} value The first number. (minuend)
|
|
7
|
+
* @param {number} other The second number.(subtrahend)
|
|
8
|
+
* @returns {number} The difference of the two numbers, or `NaN` if any input is `NaN`.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* subtract(6, 3); // => 3
|
|
12
|
+
* subtract(6, NaN); // => NaN
|
|
13
|
+
* subtract(NaN, 3); // => NaN
|
|
14
|
+
*/
|
|
15
|
+
declare function subtract(value: number, other: number): number;
|
|
16
|
+
|
|
17
|
+
export { subtract };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Subtracts one number from another.
|
|
3
|
+
*
|
|
4
|
+
* If either of the numbers is `NaN`, the function returns `NaN`.
|
|
5
|
+
*
|
|
6
|
+
* @param {number} value The first number. (minuend)
|
|
7
|
+
* @param {number} other The second number.(subtrahend)
|
|
8
|
+
* @returns {number} The difference of the two numbers, or `NaN` if any input is `NaN`.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* subtract(6, 3); // => 3
|
|
12
|
+
* subtract(6, NaN); // => NaN
|
|
13
|
+
* subtract(NaN, 3); // => NaN
|
|
14
|
+
*/
|
|
15
|
+
declare function subtract(value: number, other: number): number;
|
|
16
|
+
|
|
17
|
+
export { subtract };
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { cloneDeep } from './cloneDeep.mjs';
|
|
2
2
|
import { clone } from '../../object/clone.mjs';
|
|
3
|
+
import { isPrimitive } from '../../predicate/isPrimitive.mjs';
|
|
3
4
|
import { getSymbols } from '../_internal/getSymbols.mjs';
|
|
4
5
|
import { isArguments } from '../predicate/isArguments.mjs';
|
|
5
6
|
import { isObjectLike } from '../predicate/isObjectLike.mjs';
|
|
@@ -12,11 +13,14 @@ function mergeWith(object, ...otherArgs) {
|
|
|
12
13
|
let result = object;
|
|
13
14
|
for (let i = 0; i < sources.length; i++) {
|
|
14
15
|
const source = sources[i];
|
|
15
|
-
result = mergeWithDeep(
|
|
16
|
+
result = mergeWithDeep(result, source, merge, new Map());
|
|
16
17
|
}
|
|
17
18
|
return result;
|
|
18
19
|
}
|
|
19
20
|
function mergeWithDeep(target, source, merge, stack) {
|
|
21
|
+
if (isPrimitive(target)) {
|
|
22
|
+
target = Object(target);
|
|
23
|
+
}
|
|
20
24
|
if (source == null || typeof source !== 'object') {
|
|
21
25
|
return target;
|
|
22
26
|
}
|
|
@@ -45,7 +49,7 @@ function mergeWithDeep(target, source, merge, stack) {
|
|
|
45
49
|
sourceValue = cloneDeep(sourceValue);
|
|
46
50
|
}
|
|
47
51
|
if (Array.isArray(sourceValue)) {
|
|
48
|
-
if (typeof targetValue === 'object') {
|
|
52
|
+
if (typeof targetValue === 'object' && targetValue != null) {
|
|
49
53
|
const cloned = [];
|
|
50
54
|
const targetKeys = Reflect.ownKeys(targetValue);
|
|
51
55
|
for (let i = 0; i < targetKeys.length; i++) {
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Checks if the given value is a Buffer instance.
|
|
3
|
+
*
|
|
4
|
+
* This function tests whether the provided value is an instance of Buffer.
|
|
5
|
+
* It returns `true` if the value is a Buffer, and `false` otherwise.
|
|
6
|
+
*
|
|
7
|
+
* This function can also serve as a type predicate in TypeScript, narrowing the type of the argument to `Buffer`.
|
|
8
|
+
*
|
|
9
|
+
* @param {unknown} x - The value to check if it is a Buffer.
|
|
10
|
+
* @returns {boolean} Returns `true` if `x` is a Buffer, else `false`.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* const buffer = Buffer.from("test");
|
|
14
|
+
* console.log(isBuffer(buffer)); // true
|
|
15
|
+
*
|
|
16
|
+
* const notBuffer = "not a buffer";
|
|
17
|
+
* console.log(isBuffer(notBuffer)); // false
|
|
18
|
+
*/
|
|
19
|
+
declare function isBuffer(x?: unknown): boolean;
|
|
20
|
+
|
|
21
|
+
export { isBuffer };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Checks if the given value is a Buffer instance.
|
|
3
|
+
*
|
|
4
|
+
* This function tests whether the provided value is an instance of Buffer.
|
|
5
|
+
* It returns `true` if the value is a Buffer, and `false` otherwise.
|
|
6
|
+
*
|
|
7
|
+
* This function can also serve as a type predicate in TypeScript, narrowing the type of the argument to `Buffer`.
|
|
8
|
+
*
|
|
9
|
+
* @param {unknown} x - The value to check if it is a Buffer.
|
|
10
|
+
* @returns {boolean} Returns `true` if `x` is a Buffer, else `false`.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* const buffer = Buffer.from("test");
|
|
14
|
+
* console.log(isBuffer(buffer)); // true
|
|
15
|
+
*
|
|
16
|
+
* const notBuffer = "not a buffer";
|
|
17
|
+
* console.log(isBuffer(notBuffer)); // false
|
|
18
|
+
*/
|
|
19
|
+
declare function isBuffer(x?: unknown): boolean;
|
|
20
|
+
|
|
21
|
+
export { isBuffer };
|
|
@@ -13,10 +13,7 @@ function isMatch(target, source) {
|
|
|
13
13
|
}
|
|
14
14
|
const keys = Object.keys(source);
|
|
15
15
|
if (target == null) {
|
|
16
|
-
|
|
17
|
-
return true;
|
|
18
|
-
}
|
|
19
|
-
return false;
|
|
16
|
+
return keys.length === 0;
|
|
20
17
|
}
|
|
21
18
|
if (Array.isArray(source)) {
|
|
22
19
|
return isArrayMatch(target, source);
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a function that invokes the method at a given path of `object` with the provided arguments.
|
|
3
|
+
*
|
|
4
|
+
* @param {object} object - The object to query.
|
|
5
|
+
* @param {...any} args - The arguments to invoke the method with.
|
|
6
|
+
* @returns {(path: PropertyKey | PropertyKey[]) => any} - Returns a new function that takes a path and invokes the method at `path` with `args`.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* const object = {
|
|
10
|
+
* a: {
|
|
11
|
+
* b: function (x, y) {
|
|
12
|
+
* return x + y;
|
|
13
|
+
* }
|
|
14
|
+
* }
|
|
15
|
+
* };
|
|
16
|
+
*
|
|
17
|
+
* const add = methodOf(object, 1, 2);
|
|
18
|
+
* console.log(add('a.b')); // => 3
|
|
19
|
+
*/
|
|
20
|
+
declare function methodOf(object: object, ...args: any[]): (path: PropertyKey | PropertyKey[]) => any;
|
|
21
|
+
|
|
22
|
+
export { methodOf };
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a function that invokes the method at a given path of `object` with the provided arguments.
|
|
3
|
+
*
|
|
4
|
+
* @param {object} object - The object to query.
|
|
5
|
+
* @param {...any} args - The arguments to invoke the method with.
|
|
6
|
+
* @returns {(path: PropertyKey | PropertyKey[]) => any} - Returns a new function that takes a path and invokes the method at `path` with `args`.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* const object = {
|
|
10
|
+
* a: {
|
|
11
|
+
* b: function (x, y) {
|
|
12
|
+
* return x + y;
|
|
13
|
+
* }
|
|
14
|
+
* }
|
|
15
|
+
* };
|
|
16
|
+
*
|
|
17
|
+
* const add = methodOf(object, 1, 2);
|
|
18
|
+
* console.log(add('a.b')); // => 3
|
|
19
|
+
*/
|
|
20
|
+
declare function methodOf(object: object, ...args: any[]): (path: PropertyKey | PropertyKey[]) => any;
|
|
21
|
+
|
|
22
|
+
export { methodOf };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
4
|
+
|
|
5
|
+
const AbortError = require('../_chunk/AbortError-Cg4ZQ1.js');
|
|
6
|
+
|
|
7
|
+
class TimeoutError extends Error {
|
|
8
|
+
constructor(message = 'The operation was timed out') {
|
|
9
|
+
super(message);
|
|
10
|
+
this.name = 'TimeoutError';
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
exports.AbortError = AbortError.AbortError;
|
|
15
|
+
exports.TimeoutError = TimeoutError;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An asynchronous no-operation function that does nothing.
|
|
3
|
+
* This can be used as a placeholder or default function.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* asyncNoop(); // Does nothing
|
|
7
|
+
*
|
|
8
|
+
* @returns {Promise<void>} This function returns a Promise that resolves to undefined.
|
|
9
|
+
*/
|
|
10
|
+
declare function asyncNoop(): Promise<void>;
|
|
11
|
+
|
|
12
|
+
export { asyncNoop };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An asynchronous no-operation function that does nothing.
|
|
3
|
+
* This can be used as a placeholder or default function.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* asyncNoop(); // Does nothing
|
|
7
|
+
*
|
|
8
|
+
* @returns {Promise<void>} This function returns a Promise that resolves to undefined.
|
|
9
|
+
*/
|
|
10
|
+
declare function asyncNoop(): Promise<void>;
|
|
11
|
+
|
|
12
|
+
export { asyncNoop };
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export { after } from './after.mjs';
|
|
2
2
|
export { ary } from './ary.mjs';
|
|
3
|
+
export { asyncNoop } from './asyncNoop.mjs';
|
|
3
4
|
export { before } from './before.mjs';
|
|
4
5
|
export { curry } from './curry.mjs';
|
|
5
6
|
export { curryRight } from './curryRight.mjs';
|
|
@@ -14,6 +15,7 @@ export { once } from './once.mjs';
|
|
|
14
15
|
export { partial } from './partial.mjs';
|
|
15
16
|
export { partialRight } from './partialRight.mjs';
|
|
16
17
|
export { rest } from './rest.mjs';
|
|
18
|
+
export { retry } from './retry.mjs';
|
|
17
19
|
export { spread } from './spread.mjs';
|
|
18
20
|
export { ThrottledFunction, throttle } from './throttle.mjs';
|
|
19
21
|
export { unary } from './unary.mjs';
|
package/dist/function/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export { after } from './after.js';
|
|
2
2
|
export { ary } from './ary.js';
|
|
3
|
+
export { asyncNoop } from './asyncNoop.js';
|
|
3
4
|
export { before } from './before.js';
|
|
4
5
|
export { curry } from './curry.js';
|
|
5
6
|
export { curryRight } from './curryRight.js';
|
|
@@ -14,6 +15,7 @@ export { once } from './once.js';
|
|
|
14
15
|
export { partial } from './partial.js';
|
|
15
16
|
export { partialRight } from './partialRight.js';
|
|
16
17
|
export { rest } from './rest.js';
|
|
18
|
+
export { retry } from './retry.js';
|
|
17
19
|
export { spread } from './spread.js';
|
|
18
20
|
export { ThrottledFunction, throttle } from './throttle.js';
|
|
19
21
|
export { unary } from './unary.js';
|
package/dist/function/index.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
4
4
|
|
|
5
|
-
const unary = require('../_chunk/unary-
|
|
5
|
+
const unary = require('../_chunk/unary-c1NFA5.js');
|
|
6
6
|
const noop = require('../_chunk/noop-2IwLUk.js');
|
|
7
7
|
|
|
8
8
|
function before(n, func) {
|
|
@@ -87,6 +87,7 @@ function throttle(func, throttleMs, { signal, edges = ['leading', 'trailing'] }
|
|
|
87
87
|
|
|
88
88
|
exports.after = unary.after;
|
|
89
89
|
exports.ary = unary.ary;
|
|
90
|
+
exports.asyncNoop = unary.asyncNoop;
|
|
90
91
|
exports.debounce = unary.debounce;
|
|
91
92
|
exports.flow = unary.flow;
|
|
92
93
|
exports.flowRight = unary.flowRight;
|
|
@@ -97,6 +98,7 @@ exports.once = unary.once;
|
|
|
97
98
|
exports.partial = unary.partial;
|
|
98
99
|
exports.partialRight = unary.partialRight;
|
|
99
100
|
exports.rest = unary.rest;
|
|
101
|
+
exports.retry = unary.retry;
|
|
100
102
|
exports.unary = unary.unary;
|
|
101
103
|
exports.noop = noop.noop;
|
|
102
104
|
exports.before = before;
|
package/dist/function/index.mjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export { after } from './after.mjs';
|
|
2
2
|
export { ary } from './ary.mjs';
|
|
3
|
+
export { asyncNoop } from './asyncNoop.mjs';
|
|
3
4
|
export { before } from './before.mjs';
|
|
4
5
|
export { curry } from './curry.mjs';
|
|
5
6
|
export { curryRight } from './curryRight.mjs';
|
|
@@ -14,6 +15,7 @@ export { once } from './once.mjs';
|
|
|
14
15
|
export { partial } from './partial.mjs';
|
|
15
16
|
export { partialRight } from './partialRight.mjs';
|
|
16
17
|
export { rest } from './rest.mjs';
|
|
18
|
+
export { retry } from './retry.mjs';
|
|
17
19
|
export { spread } from './spread.mjs';
|
|
18
20
|
export { throttle } from './throttle.mjs';
|
|
19
21
|
export { unary } from './unary.mjs';
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
interface RetryOptions {
|
|
2
|
+
/**
|
|
3
|
+
* The number of milliseconds to interval delay.
|
|
4
|
+
* @default 0
|
|
5
|
+
*/
|
|
6
|
+
delay?: number;
|
|
7
|
+
/**
|
|
8
|
+
* The number of retries to attempt.
|
|
9
|
+
* @default Number.POSITIVE_INFINITY
|
|
10
|
+
*/
|
|
11
|
+
retries?: number;
|
|
12
|
+
/**
|
|
13
|
+
* An AbortSignal to cancel the retry operation.
|
|
14
|
+
*/
|
|
15
|
+
signal?: AbortSignal;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Retries a function that returns a promise until it resolves successfully.
|
|
19
|
+
*
|
|
20
|
+
* @template T
|
|
21
|
+
* @param {() => Promise<T>} func - The function to retry.
|
|
22
|
+
* @returns {Promise<T>} A promise that resolves with the value of the successful function call.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* // Basic usage with default retry options
|
|
26
|
+
* retry(() => fetchData()).then(data => console.log(data));
|
|
27
|
+
*/
|
|
28
|
+
declare function retry<T>(func: () => Promise<T>): Promise<T>;
|
|
29
|
+
/**
|
|
30
|
+
* Retries a function that returns a promise a specified number of times.
|
|
31
|
+
*
|
|
32
|
+
* @template T
|
|
33
|
+
* @param {() => Promise<T>} func - The function to retry. It should return a promise.
|
|
34
|
+
* @param {number} retries - The number of retries to attempt. Default is Infinity.
|
|
35
|
+
* @returns {Promise<T>} A promise that resolves with the value of the successful function call.
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* // Retry a function up to 3 times
|
|
39
|
+
* retry(() => fetchData(), 3).then(data => console.log(data));
|
|
40
|
+
*/
|
|
41
|
+
declare function retry<T>(func: () => Promise<T>, retries: number): Promise<T>;
|
|
42
|
+
/**
|
|
43
|
+
* Retries a function that returns a promise with specified options.
|
|
44
|
+
*
|
|
45
|
+
* @template T
|
|
46
|
+
* @param {() => Promise<T>} func - The function to retry. It should return a promise.
|
|
47
|
+
* @param {RetryOptions} options - Options to configure the retry behavior.
|
|
48
|
+
* @param {number} [options.delay=0] - The number of milliseconds to wait between retries.
|
|
49
|
+
* @param {number} [options.retries=Infinity] - The number of retries to attempt.
|
|
50
|
+
* @param {AbortSignal} [options.signal] - An AbortSignal to cancel the retry operation.
|
|
51
|
+
* @returns {Promise<T>} A promise that resolves with the value of the successful function call.
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* // Retry a function with a delay of 1000ms between attempts
|
|
55
|
+
* retry(() => fetchData(), { delay: 1000, times: 5 }).then(data => console.log(data));
|
|
56
|
+
*/
|
|
57
|
+
declare function retry<T>(func: () => Promise<T>, options: RetryOptions): Promise<T>;
|
|
58
|
+
|
|
59
|
+
export { retry };
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
interface RetryOptions {
|
|
2
|
+
/**
|
|
3
|
+
* The number of milliseconds to interval delay.
|
|
4
|
+
* @default 0
|
|
5
|
+
*/
|
|
6
|
+
delay?: number;
|
|
7
|
+
/**
|
|
8
|
+
* The number of retries to attempt.
|
|
9
|
+
* @default Number.POSITIVE_INFINITY
|
|
10
|
+
*/
|
|
11
|
+
retries?: number;
|
|
12
|
+
/**
|
|
13
|
+
* An AbortSignal to cancel the retry operation.
|
|
14
|
+
*/
|
|
15
|
+
signal?: AbortSignal;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Retries a function that returns a promise until it resolves successfully.
|
|
19
|
+
*
|
|
20
|
+
* @template T
|
|
21
|
+
* @param {() => Promise<T>} func - The function to retry.
|
|
22
|
+
* @returns {Promise<T>} A promise that resolves with the value of the successful function call.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* // Basic usage with default retry options
|
|
26
|
+
* retry(() => fetchData()).then(data => console.log(data));
|
|
27
|
+
*/
|
|
28
|
+
declare function retry<T>(func: () => Promise<T>): Promise<T>;
|
|
29
|
+
/**
|
|
30
|
+
* Retries a function that returns a promise a specified number of times.
|
|
31
|
+
*
|
|
32
|
+
* @template T
|
|
33
|
+
* @param {() => Promise<T>} func - The function to retry. It should return a promise.
|
|
34
|
+
* @param {number} retries - The number of retries to attempt. Default is Infinity.
|
|
35
|
+
* @returns {Promise<T>} A promise that resolves with the value of the successful function call.
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* // Retry a function up to 3 times
|
|
39
|
+
* retry(() => fetchData(), 3).then(data => console.log(data));
|
|
40
|
+
*/
|
|
41
|
+
declare function retry<T>(func: () => Promise<T>, retries: number): Promise<T>;
|
|
42
|
+
/**
|
|
43
|
+
* Retries a function that returns a promise with specified options.
|
|
44
|
+
*
|
|
45
|
+
* @template T
|
|
46
|
+
* @param {() => Promise<T>} func - The function to retry. It should return a promise.
|
|
47
|
+
* @param {RetryOptions} options - Options to configure the retry behavior.
|
|
48
|
+
* @param {number} [options.delay=0] - The number of milliseconds to wait between retries.
|
|
49
|
+
* @param {number} [options.retries=Infinity] - The number of retries to attempt.
|
|
50
|
+
* @param {AbortSignal} [options.signal] - An AbortSignal to cancel the retry operation.
|
|
51
|
+
* @returns {Promise<T>} A promise that resolves with the value of the successful function call.
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* // Retry a function with a delay of 1000ms between attempts
|
|
55
|
+
* retry(() => fetchData(), { delay: 1000, times: 5 }).then(data => console.log(data));
|
|
56
|
+
*/
|
|
57
|
+
declare function retry<T>(func: () => Promise<T>, options: RetryOptions): Promise<T>;
|
|
58
|
+
|
|
59
|
+
export { retry };
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { delay } from '../promise/delay.mjs';
|
|
2
|
+
|
|
3
|
+
const DEFAULT_DELAY = 0;
|
|
4
|
+
const DEFAULT_RETRIES = Number.POSITIVE_INFINITY;
|
|
5
|
+
async function retry(func, _options) {
|
|
6
|
+
let delay$1;
|
|
7
|
+
let retries;
|
|
8
|
+
let signal;
|
|
9
|
+
if (typeof _options === 'number') {
|
|
10
|
+
delay$1 = DEFAULT_DELAY;
|
|
11
|
+
retries = _options;
|
|
12
|
+
signal = undefined;
|
|
13
|
+
}
|
|
14
|
+
else {
|
|
15
|
+
delay$1 = _options?.delay ?? DEFAULT_DELAY;
|
|
16
|
+
retries = _options?.retries ?? DEFAULT_RETRIES;
|
|
17
|
+
signal = _options?.signal;
|
|
18
|
+
}
|
|
19
|
+
let error;
|
|
20
|
+
for (let i = 0; i < retries; i++) {
|
|
21
|
+
if (signal?.aborted) {
|
|
22
|
+
throw error ?? new Error(`The retry operation was aborted due to an abort signal.`);
|
|
23
|
+
}
|
|
24
|
+
try {
|
|
25
|
+
return await func();
|
|
26
|
+
}
|
|
27
|
+
catch (err) {
|
|
28
|
+
error = err;
|
|
29
|
+
await delay(delay$1);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
throw error;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export { retry };
|
package/dist/index.d.mts
CHANGED
|
@@ -31,6 +31,7 @@ export { orderBy } from './array/orderBy.mjs';
|
|
|
31
31
|
export { partition } from './array/partition.mjs';
|
|
32
32
|
export { pull } from './array/pull.mjs';
|
|
33
33
|
export { pullAt } from './array/pullAt.mjs';
|
|
34
|
+
export { remove } from './array/remove.mjs';
|
|
34
35
|
export { sample } from './array/sample.mjs';
|
|
35
36
|
export { sampleSize } from './array/sampleSize.mjs';
|
|
36
37
|
export { shuffle } from './array/shuffle.mjs';
|
|
@@ -49,6 +50,7 @@ export { uniqBy } from './array/uniqBy.mjs';
|
|
|
49
50
|
export { uniqWith } from './array/uniqWith.mjs';
|
|
50
51
|
export { unzip } from './array/unzip.mjs';
|
|
51
52
|
export { unzipWith } from './array/unzipWith.mjs';
|
|
53
|
+
export { windowed } from './array/windowed.mjs';
|
|
52
54
|
export { without } from './array/without.mjs';
|
|
53
55
|
export { xor } from './array/xor.mjs';
|
|
54
56
|
export { xorBy } from './array/xorBy.mjs';
|
|
@@ -60,6 +62,7 @@ export { AbortError } from './error/AbortError.mjs';
|
|
|
60
62
|
export { TimeoutError } from './error/TimeoutError.mjs';
|
|
61
63
|
export { after } from './function/after.mjs';
|
|
62
64
|
export { ary } from './function/ary.mjs';
|
|
65
|
+
export { asyncNoop } from './function/asyncNoop.mjs';
|
|
63
66
|
export { before } from './function/before.mjs';
|
|
64
67
|
export { curry } from './function/curry.mjs';
|
|
65
68
|
export { curryRight } from './function/curryRight.mjs';
|
|
@@ -74,6 +77,7 @@ export { once } from './function/once.mjs';
|
|
|
74
77
|
export { partial } from './function/partial.mjs';
|
|
75
78
|
export { partialRight } from './function/partialRight.mjs';
|
|
76
79
|
export { rest } from './function/rest.mjs';
|
|
80
|
+
export { retry } from './function/retry.mjs';
|
|
77
81
|
export { spread } from './function/spread.mjs';
|
|
78
82
|
export { ThrottledFunction, throttle } from './function/throttle.mjs';
|
|
79
83
|
export { unary } from './function/unary.mjs';
|
|
@@ -123,6 +127,7 @@ export { isNotNil } from './predicate/isNotNil.mjs';
|
|
|
123
127
|
export { isNull } from './predicate/isNull.mjs';
|
|
124
128
|
export { isPlainObject } from './predicate/isPlainObject.mjs';
|
|
125
129
|
export { isPrimitive } from './predicate/isPrimitive.mjs';
|
|
130
|
+
export { isPromise } from './predicate/isPromise.mjs';
|
|
126
131
|
export { isRegExp } from './predicate/isRegExp.mjs';
|
|
127
132
|
export { isSet } from './predicate/isSet.mjs';
|
|
128
133
|
export { isString } from './predicate/isString.mjs';
|
|
@@ -132,6 +137,8 @@ export { isUndefined } from './predicate/isUndefined.mjs';
|
|
|
132
137
|
export { isWeakMap } from './predicate/isWeakMap.mjs';
|
|
133
138
|
export { isWeakSet } from './predicate/isWeakSet.mjs';
|
|
134
139
|
export { delay } from './promise/delay.mjs';
|
|
140
|
+
export { Mutex } from './promise/mutex.mjs';
|
|
141
|
+
export { Semaphore } from './promise/semaphore.mjs';
|
|
135
142
|
export { timeout } from './promise/timeout.mjs';
|
|
136
143
|
export { withTimeout } from './promise/withTimeout.mjs';
|
|
137
144
|
export { camelCase } from './string/camelCase.mjs';
|
package/dist/index.d.ts
CHANGED
|
@@ -31,6 +31,7 @@ export { orderBy } from './array/orderBy.js';
|
|
|
31
31
|
export { partition } from './array/partition.js';
|
|
32
32
|
export { pull } from './array/pull.js';
|
|
33
33
|
export { pullAt } from './array/pullAt.js';
|
|
34
|
+
export { remove } from './array/remove.js';
|
|
34
35
|
export { sample } from './array/sample.js';
|
|
35
36
|
export { sampleSize } from './array/sampleSize.js';
|
|
36
37
|
export { shuffle } from './array/shuffle.js';
|
|
@@ -49,6 +50,7 @@ export { uniqBy } from './array/uniqBy.js';
|
|
|
49
50
|
export { uniqWith } from './array/uniqWith.js';
|
|
50
51
|
export { unzip } from './array/unzip.js';
|
|
51
52
|
export { unzipWith } from './array/unzipWith.js';
|
|
53
|
+
export { windowed } from './array/windowed.js';
|
|
52
54
|
export { without } from './array/without.js';
|
|
53
55
|
export { xor } from './array/xor.js';
|
|
54
56
|
export { xorBy } from './array/xorBy.js';
|
|
@@ -60,6 +62,7 @@ export { AbortError } from './error/AbortError.js';
|
|
|
60
62
|
export { TimeoutError } from './error/TimeoutError.js';
|
|
61
63
|
export { after } from './function/after.js';
|
|
62
64
|
export { ary } from './function/ary.js';
|
|
65
|
+
export { asyncNoop } from './function/asyncNoop.js';
|
|
63
66
|
export { before } from './function/before.js';
|
|
64
67
|
export { curry } from './function/curry.js';
|
|
65
68
|
export { curryRight } from './function/curryRight.js';
|
|
@@ -74,6 +77,7 @@ export { once } from './function/once.js';
|
|
|
74
77
|
export { partial } from './function/partial.js';
|
|
75
78
|
export { partialRight } from './function/partialRight.js';
|
|
76
79
|
export { rest } from './function/rest.js';
|
|
80
|
+
export { retry } from './function/retry.js';
|
|
77
81
|
export { spread } from './function/spread.js';
|
|
78
82
|
export { ThrottledFunction, throttle } from './function/throttle.js';
|
|
79
83
|
export { unary } from './function/unary.js';
|
|
@@ -123,6 +127,7 @@ export { isNotNil } from './predicate/isNotNil.js';
|
|
|
123
127
|
export { isNull } from './predicate/isNull.js';
|
|
124
128
|
export { isPlainObject } from './predicate/isPlainObject.js';
|
|
125
129
|
export { isPrimitive } from './predicate/isPrimitive.js';
|
|
130
|
+
export { isPromise } from './predicate/isPromise.js';
|
|
126
131
|
export { isRegExp } from './predicate/isRegExp.js';
|
|
127
132
|
export { isSet } from './predicate/isSet.js';
|
|
128
133
|
export { isString } from './predicate/isString.js';
|
|
@@ -132,6 +137,8 @@ export { isUndefined } from './predicate/isUndefined.js';
|
|
|
132
137
|
export { isWeakMap } from './predicate/isWeakMap.js';
|
|
133
138
|
export { isWeakSet } from './predicate/isWeakSet.js';
|
|
134
139
|
export { delay } from './promise/delay.js';
|
|
140
|
+
export { Mutex } from './promise/mutex.js';
|
|
141
|
+
export { Semaphore } from './promise/semaphore.js';
|
|
135
142
|
export { timeout } from './promise/timeout.js';
|
|
136
143
|
export { withTimeout } from './promise/withTimeout.js';
|
|
137
144
|
export { camelCase } from './string/camelCase.js';
|