es-toolkit 1.31.0 → 1.32.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.
Files changed (70) hide show
  1. package/CHANGELOG.md +10 -0
  2. package/dist/_chunk/AbortError-Cg4ZQ1.js +10 -0
  3. package/dist/_chunk/delay-_VMfFa.js +25 -0
  4. package/dist/_chunk/isPlainObject-Xaozpc.js +93 -0
  5. package/dist/_chunk/{isWeakSet-CvIdTA.js → isWeakSet-DoHqUM.js} +42 -71
  6. package/dist/_chunk/{toMerged-DGFrN7.js → toMerged-BQTfB8.js} +39 -8
  7. package/dist/_chunk/{unary-B6qG7C.js → unary-c1NFA5.js} +35 -0
  8. package/dist/array/at.d.mts +1 -1
  9. package/dist/array/at.d.ts +1 -1
  10. package/dist/array/pullAt.d.mts +1 -1
  11. package/dist/array/pullAt.d.ts +1 -1
  12. package/dist/browser.global.js +1 -1
  13. package/dist/browser.global.js.map +1 -1
  14. package/dist/compat/array/find.d.mts +16 -8
  15. package/dist/compat/array/find.d.ts +16 -8
  16. package/dist/compat/array/find.mjs +5 -2
  17. package/dist/compat/array/sortedIndex.d.mts +16 -0
  18. package/dist/compat/array/sortedIndex.d.ts +16 -0
  19. package/dist/compat/array/sortedIndex.mjs +30 -0
  20. package/dist/compat/array/sortedIndexBy.d.mts +19 -0
  21. package/dist/compat/array/sortedIndexBy.d.ts +19 -0
  22. package/dist/compat/array/sortedIndexBy.mjs +56 -0
  23. package/dist/compat/index.d.mts +7 -0
  24. package/dist/compat/index.d.ts +7 -0
  25. package/dist/compat/index.js +145 -43
  26. package/dist/compat/index.mjs +7 -0
  27. package/dist/compat/math/multiply.d.mts +18 -0
  28. package/dist/compat/math/multiply.d.ts +18 -0
  29. package/dist/compat/math/multiply.mjs +21 -0
  30. package/dist/error/index.d.mts +2 -0
  31. package/dist/error/index.d.ts +2 -0
  32. package/dist/error/index.js +15 -0
  33. package/dist/error/index.mjs +2 -0
  34. package/dist/function/index.d.mts +1 -0
  35. package/dist/function/index.d.ts +1 -0
  36. package/dist/function/index.js +2 -1
  37. package/dist/function/index.mjs +1 -0
  38. package/dist/function/retry.d.mts +59 -0
  39. package/dist/function/retry.d.ts +59 -0
  40. package/dist/function/retry.mjs +35 -0
  41. package/dist/index.d.mts +4 -0
  42. package/dist/index.d.ts +4 -0
  43. package/dist/index.js +18 -11
  44. package/dist/index.mjs +4 -0
  45. package/dist/object/cloneDeepWith.mjs +34 -1
  46. package/dist/object/flattenObject.d.mts +10 -2
  47. package/dist/object/flattenObject.d.ts +10 -2
  48. package/dist/object/flattenObject.mjs +6 -6
  49. package/dist/object/index.js +1 -1
  50. package/dist/predicate/index.d.mts +1 -0
  51. package/dist/predicate/index.d.ts +1 -0
  52. package/dist/predicate/index.js +4 -7
  53. package/dist/predicate/index.mjs +1 -0
  54. package/dist/predicate/isPromise.d.mts +20 -0
  55. package/dist/predicate/isPromise.d.ts +20 -0
  56. package/dist/predicate/isPromise.mjs +5 -0
  57. package/dist/promise/index.d.mts +2 -0
  58. package/dist/promise/index.d.ts +2 -0
  59. package/dist/promise/index.js +56 -4
  60. package/dist/promise/index.mjs +2 -0
  61. package/dist/promise/mutex.d.mts +64 -0
  62. package/dist/promise/mutex.d.ts +64 -0
  63. package/dist/promise/mutex.mjs +16 -0
  64. package/dist/promise/semaphore.d.mts +81 -0
  65. package/dist/promise/semaphore.d.ts +81 -0
  66. package/dist/promise/semaphore.mjs +30 -0
  67. package/error.d.ts +1 -0
  68. package/package.json +42 -21
  69. package/dist/_chunk/index-BGZDR9.js +0 -50
  70. package/dist/_chunk/isPlainObject-octpoD.js +0 -32
@@ -3,14 +3,16 @@
3
3
  Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
4
 
5
5
  const zipWith = require('../_chunk/zipWith-Bdyzuy.js');
6
- const promise_index = require('../_chunk/index-BGZDR9.js');
7
- const unary = require('../_chunk/unary-B6qG7C.js');
6
+ const AbortError = require('../_chunk/AbortError-Cg4ZQ1.js');
7
+ const error_index = require('../error/index.js');
8
+ const unary = require('../_chunk/unary-c1NFA5.js');
8
9
  const noop = require('../_chunk/noop-2IwLUk.js');
9
10
  const range$1 = require('../_chunk/range-HnEIT7.js');
10
11
  const randomInt = require('../_chunk/randomInt-CF7bZK.js');
11
- const toMerged = require('../_chunk/toMerged-DGFrN7.js');
12
- const isPlainObject$1 = require('../_chunk/isPlainObject-octpoD.js');
13
- const isWeakSet$1 = require('../_chunk/isWeakSet-CvIdTA.js');
12
+ const toMerged = require('../_chunk/toMerged-BQTfB8.js');
13
+ const isPlainObject$1 = require('../_chunk/isPlainObject-Xaozpc.js');
14
+ const isWeakSet$1 = require('../_chunk/isWeakSet-DoHqUM.js');
15
+ const promise_index = require('../promise/index.js');
14
16
  const upperFirst$1 = require('../_chunk/upperFirst-CorAVn.js');
15
17
  const util_index = require('../util/index.js');
16
18
 
@@ -361,14 +363,14 @@ function cloneDeepWith(obj, cloneValue) {
361
363
  return undefined;
362
364
  }
363
365
  switch (Object.prototype.toString.call(obj)) {
364
- case isWeakSet$1.numberTag:
365
- case isWeakSet$1.stringTag:
366
- case isWeakSet$1.booleanTag: {
366
+ case isPlainObject$1.numberTag:
367
+ case isPlainObject$1.stringTag:
368
+ case isPlainObject$1.booleanTag: {
367
369
  const result = new obj.constructor(obj?.valueOf());
368
370
  toMerged.copyProperties(result, obj);
369
371
  return result;
370
372
  }
371
- case isWeakSet$1.argumentsTag: {
373
+ case isPlainObject$1.argumentsTag: {
372
374
  const result = {};
373
375
  toMerged.copyProperties(result, obj);
374
376
  result.length = obj.length;
@@ -402,7 +404,7 @@ function isIndex(value, length = Number.MAX_SAFE_INTEGER) {
402
404
  }
403
405
 
404
406
  function isArguments(value) {
405
- return value !== null && typeof value === 'object' && isWeakSet$1.getTag(value) === '[object Arguments]';
407
+ return value !== null && typeof value === 'object' && isPlainObject$1.getTag(value) === '[object Arguments]';
406
408
  }
407
409
 
408
410
  function has(object, path) {
@@ -726,11 +728,14 @@ function filter(source, predicate) {
726
728
  }
727
729
  }
728
730
 
729
- function find(source, doesMatch) {
731
+ function find(source, doesMatch, fromIndex = 0) {
730
732
  if (!source) {
731
733
  return undefined;
732
734
  }
733
- const values = Array.isArray(source) ? source : Object.values(source);
735
+ if (fromIndex < 0) {
736
+ fromIndex = Math.max(source.length + fromIndex, 0);
737
+ }
738
+ const values = Array.isArray(source) ? source.slice(fromIndex) : Object.values(source).slice(fromIndex);
734
739
  switch (typeof doesMatch) {
735
740
  case 'function': {
736
741
  if (!Array.isArray(source)) {
@@ -1292,6 +1297,90 @@ function sortBy(collection, ...criteria) {
1292
1297
  return orderBy(collection, zipWith.flatten(criteria), ['asc']);
1293
1298
  }
1294
1299
 
1300
+ function isNaN(value) {
1301
+ return Number.isNaN(value);
1302
+ }
1303
+
1304
+ function isNil(x) {
1305
+ return x == null;
1306
+ }
1307
+
1308
+ const MAX_ARRAY_LENGTH$2 = 4294967295;
1309
+ const MAX_ARRAY_INDEX = MAX_ARRAY_LENGTH$2 - 1;
1310
+ function sortedIndexBy(array, value, iteratee, retHighest) {
1311
+ let low = 0;
1312
+ let high = array == null ? 0 : array.length;
1313
+ if (high === 0 || isNil(array)) {
1314
+ return 0;
1315
+ }
1316
+ const transformedValue = iteratee?.(value);
1317
+ const valIsNaN = isNaN(transformedValue);
1318
+ const valIsNull = isWeakSet$1.isNull(transformedValue);
1319
+ const valIsSymbol = isSymbol(transformedValue);
1320
+ const valIsUndefined = isWeakSet$1.isUndefined(transformedValue);
1321
+ while (low < high) {
1322
+ let setLow;
1323
+ const mid = Math.floor((low + high) / 2);
1324
+ const computed = iteratee?.(array[mid]);
1325
+ const othIsDefined = !isWeakSet$1.isUndefined(computed);
1326
+ const othIsNull = isWeakSet$1.isNull(computed);
1327
+ const othIsReflexive = !isNaN(computed);
1328
+ const othIsSymbol = isSymbol(computed);
1329
+ if (valIsNaN) {
1330
+ setLow = retHighest || othIsReflexive;
1331
+ }
1332
+ else if (valIsUndefined) {
1333
+ setLow = othIsReflexive && (retHighest || othIsDefined);
1334
+ }
1335
+ else if (valIsNull) {
1336
+ setLow = othIsReflexive && othIsDefined && (retHighest || !othIsNull);
1337
+ }
1338
+ else if (valIsSymbol) {
1339
+ setLow = othIsReflexive && othIsDefined && !othIsNull && (retHighest || !othIsSymbol);
1340
+ }
1341
+ else if (othIsNull || othIsSymbol) {
1342
+ setLow = false;
1343
+ }
1344
+ else {
1345
+ setLow = retHighest ? computed <= transformedValue : computed < transformedValue;
1346
+ }
1347
+ if (setLow) {
1348
+ low = mid + 1;
1349
+ }
1350
+ else {
1351
+ high = mid;
1352
+ }
1353
+ }
1354
+ return Math.min(high, MAX_ARRAY_INDEX);
1355
+ }
1356
+
1357
+ function isNumber(value) {
1358
+ return typeof value === 'number' || value instanceof Number;
1359
+ }
1360
+
1361
+ const MAX_ARRAY_LENGTH$1 = 4294967295;
1362
+ const HALF_MAX_ARRAY_LENGTH = MAX_ARRAY_LENGTH$1 >>> 1;
1363
+ function sortedIndex(array, value) {
1364
+ if (isWeakSet$1.isNil(array)) {
1365
+ return 0;
1366
+ }
1367
+ let low = 0, high = isWeakSet$1.isNil(array) ? low : array.length;
1368
+ if (isNumber(value) && value === value && high <= HALF_MAX_ARRAY_LENGTH) {
1369
+ while (low < high) {
1370
+ const mid = (low + high) >>> 1;
1371
+ const compute = array[mid];
1372
+ if (!isWeakSet$1.isNull(compute) && !isWeakSet$1.isSymbol(compute) && compute < value) {
1373
+ low = mid + 1;
1374
+ }
1375
+ else {
1376
+ high = mid;
1377
+ }
1378
+ }
1379
+ return high;
1380
+ }
1381
+ return sortedIndexBy(array, value, value => value);
1382
+ }
1383
+
1295
1384
  function tail(arr) {
1296
1385
  if (!isArrayLike(arr)) {
1297
1386
  return [];
@@ -1855,6 +1944,38 @@ function min(items = []) {
1855
1944
  return minElement;
1856
1945
  }
1857
1946
 
1947
+ function toString(value) {
1948
+ if (value == null) {
1949
+ return '';
1950
+ }
1951
+ if (Array.isArray(value)) {
1952
+ return value.map(toString).join(',');
1953
+ }
1954
+ const result = String(value);
1955
+ if (result === '0' && Object.is(Number(value), -0)) {
1956
+ return '-0';
1957
+ }
1958
+ return result;
1959
+ }
1960
+
1961
+ function multiply(value, other) {
1962
+ if (value === undefined && other === undefined) {
1963
+ return 1;
1964
+ }
1965
+ if (value === undefined || other === undefined) {
1966
+ return value || other;
1967
+ }
1968
+ if (typeof value === 'string' || typeof other === 'string') {
1969
+ value = toString(value);
1970
+ other = toString(other);
1971
+ }
1972
+ else {
1973
+ value = Number(value);
1974
+ other = Number(other);
1975
+ }
1976
+ return value * other;
1977
+ }
1978
+
1858
1979
  function parseInt(string, radix = 0, guard) {
1859
1980
  if (guard) {
1860
1981
  radix = 0;
@@ -2415,10 +2536,6 @@ function omit(obj, ...keysArr) {
2415
2536
  return result;
2416
2537
  }
2417
2538
 
2418
- function isNil(x) {
2419
- return x == null;
2420
- }
2421
-
2422
2539
  function pick(obj, ...keysArr) {
2423
2540
  if (isNil(obj)) {
2424
2541
  return {};
@@ -2559,7 +2676,7 @@ function isEqualWith(a, b, areValuesEqual = noop.noop) {
2559
2676
  }
2560
2677
 
2561
2678
  function isError(value) {
2562
- return isWeakSet$1.getTag(value) === '[object Error]';
2679
+ return isPlainObject$1.getTag(value) === '[object Error]';
2563
2680
  }
2564
2681
 
2565
2682
  function isFinite(value) {
@@ -2574,14 +2691,6 @@ function isMap(value) {
2574
2691
  return isWeakSet$1.isMap(value);
2575
2692
  }
2576
2693
 
2577
- function isNaN(value) {
2578
- return Number.isNaN(value);
2579
- }
2580
-
2581
- function isNumber(value) {
2582
- return typeof value === 'number' || value instanceof Number;
2583
- }
2584
-
2585
2694
  function isRegExp(value) {
2586
2695
  return isWeakSet$1.isRegExp(value);
2587
2696
  }
@@ -2602,20 +2711,6 @@ function isWeakSet(value) {
2602
2711
  return isWeakSet$1.isWeakSet(value);
2603
2712
  }
2604
2713
 
2605
- function toString(value) {
2606
- if (value == null) {
2607
- return '';
2608
- }
2609
- if (Array.isArray(value)) {
2610
- return value.map(toString).join(',');
2611
- }
2612
- const result = String(value);
2613
- if (result === '0' && Object.is(Number(value), -0)) {
2614
- return '-0';
2615
- }
2616
- return result;
2617
- }
2618
-
2619
2714
  function normalizeForCase(str) {
2620
2715
  if (typeof str !== 'string') {
2621
2716
  str = toString(str);
@@ -3081,16 +3176,15 @@ exports.xorBy = zipWith.xorBy;
3081
3176
  exports.xorWith = zipWith.xorWith;
3082
3177
  exports.zipObject = zipWith.zipObject;
3083
3178
  exports.zipWith = zipWith.zipWith;
3084
- exports.AbortError = promise_index.AbortError;
3085
- exports.TimeoutError = promise_index.TimeoutError;
3086
- exports.timeout = promise_index.timeout;
3087
- exports.withTimeout = promise_index.withTimeout;
3179
+ exports.AbortError = AbortError.AbortError;
3180
+ exports.TimeoutError = error_index.TimeoutError;
3088
3181
  exports.asyncNoop = unary.asyncNoop;
3089
3182
  exports.identity = unary.identity;
3090
3183
  exports.memoize = unary.memoize;
3091
3184
  exports.once = unary.once;
3092
3185
  exports.partial = unary.partial;
3093
3186
  exports.partialRight = unary.partialRight;
3187
+ exports.retry = unary.retry;
3094
3188
  exports.unary = unary.unary;
3095
3189
  exports.noop = noop.noop;
3096
3190
  exports.mean = range$1.mean;
@@ -3117,7 +3211,12 @@ exports.isJSONValue = isWeakSet$1.isJSONValue;
3117
3211
  exports.isLength = isWeakSet$1.isLength;
3118
3212
  exports.isNotNil = isWeakSet$1.isNotNil;
3119
3213
  exports.isNull = isWeakSet$1.isNull;
3214
+ exports.isPromise = isWeakSet$1.isPromise;
3120
3215
  exports.isUndefined = isWeakSet$1.isUndefined;
3216
+ exports.Mutex = promise_index.Mutex;
3217
+ exports.Semaphore = promise_index.Semaphore;
3218
+ exports.timeout = promise_index.timeout;
3219
+ exports.withTimeout = promise_index.withTimeout;
3121
3220
  exports.capitalize = upperFirst$1.capitalize;
3122
3221
  exports.constantCase = upperFirst$1.constantCase;
3123
3222
  exports.pascalCase = upperFirst$1.pascalCase;
@@ -3242,6 +3341,7 @@ exports.mergeWith = mergeWith;
3242
3341
  exports.method = method;
3243
3342
  exports.methodOf = methodOf;
3244
3343
  exports.min = min;
3344
+ exports.multiply = multiply;
3245
3345
  exports.negate = negate;
3246
3346
  exports.now = now;
3247
3347
  exports.nth = nth;
@@ -3274,6 +3374,8 @@ exports.slice = slice;
3274
3374
  exports.snakeCase = snakeCase;
3275
3375
  exports.some = some;
3276
3376
  exports.sortBy = sortBy;
3377
+ exports.sortedIndex = sortedIndex;
3378
+ exports.sortedIndexBy = sortedIndexBy;
3277
3379
  exports.spread = spread;
3278
3380
  exports.startCase = startCase;
3279
3381
  exports.startsWith = startsWith;
@@ -36,6 +36,7 @@ export { noop } from '../function/noop.mjs';
36
36
  export { once } from '../function/once.mjs';
37
37
  export { partial } from '../function/partial.mjs';
38
38
  export { partialRight } from '../function/partialRight.mjs';
39
+ export { retry } from '../function/retry.mjs';
39
40
  export { unary } from '../function/unary.mjs';
40
41
  export { mean } from '../math/mean.mjs';
41
42
  export { meanBy } from '../math/meanBy.mjs';
@@ -59,7 +60,10 @@ export { isJSONArray, isJSONObject, isJSONValue } from '../predicate/isJSONValue
59
60
  export { isLength } from '../predicate/isLength.mjs';
60
61
  export { isNotNil } from '../predicate/isNotNil.mjs';
61
62
  export { isNull } from '../predicate/isNull.mjs';
63
+ export { isPromise } from '../predicate/isPromise.mjs';
62
64
  export { isUndefined } from '../predicate/isUndefined.mjs';
65
+ export { Mutex } from '../promise/mutex.mjs';
66
+ export { Semaphore } from '../promise/semaphore.mjs';
63
67
  export { timeout } from '../promise/timeout.mjs';
64
68
  export { withTimeout } from '../promise/withTimeout.mjs';
65
69
  export { capitalize } from '../string/capitalize.mjs';
@@ -108,6 +112,8 @@ export { size } from './array/size.mjs';
108
112
  export { slice } from './array/slice.mjs';
109
113
  export { some } from './array/some.mjs';
110
114
  export { sortBy } from './array/sortBy.mjs';
115
+ export { sortedIndex } from './array/sortedIndex.mjs';
116
+ export { sortedIndexBy } from './array/sortedIndexBy.mjs';
111
117
  export { tail } from './array/tail.mjs';
112
118
  export { take } from './array/take.mjs';
113
119
  export { takeRight } from './array/takeRight.mjs';
@@ -146,6 +152,7 @@ export { floor } from './math/floor.mjs';
146
152
  export { inRange } from './math/inRange.mjs';
147
153
  export { max } from './math/max.mjs';
148
154
  export { min } from './math/min.mjs';
155
+ export { multiply } from './math/multiply.mjs';
149
156
  export { parseInt } from './math/parseInt.mjs';
150
157
  export { random } from './math/random.mjs';
151
158
  export { range } from './math/range.mjs';
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Multiply two numbers.
3
+ *
4
+ * If either of the numbers is `NaN`, the function returns `NaN`.
5
+ *
6
+ * @param {number} value The first number in a multiplication
7
+ * @param {number} other The second number in a multiplication
8
+ * @returns {number} The product of value and other
9
+ *
10
+ * @example
11
+ * multiply(2, 3); // => 6
12
+ * multiply(2, NaN); // => NaN
13
+ * multiply(NaN, 3); // => NaN
14
+ * multiply(NaN, NaN); // => NaN
15
+ */
16
+ declare function multiply(value: number, other: number): number;
17
+
18
+ export { multiply };
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Multiply two numbers.
3
+ *
4
+ * If either of the numbers is `NaN`, the function returns `NaN`.
5
+ *
6
+ * @param {number} value The first number in a multiplication
7
+ * @param {number} other The second number in a multiplication
8
+ * @returns {number} The product of value and other
9
+ *
10
+ * @example
11
+ * multiply(2, 3); // => 6
12
+ * multiply(2, NaN); // => NaN
13
+ * multiply(NaN, 3); // => NaN
14
+ * multiply(NaN, NaN); // => NaN
15
+ */
16
+ declare function multiply(value: number, other: number): number;
17
+
18
+ export { multiply };
@@ -0,0 +1,21 @@
1
+ import { toString } from '../util/toString.mjs';
2
+
3
+ function multiply(value, other) {
4
+ if (value === undefined && other === undefined) {
5
+ return 1;
6
+ }
7
+ if (value === undefined || other === undefined) {
8
+ return value || other;
9
+ }
10
+ if (typeof value === 'string' || typeof other === 'string') {
11
+ value = toString(value);
12
+ other = toString(other);
13
+ }
14
+ else {
15
+ value = Number(value);
16
+ other = Number(other);
17
+ }
18
+ return value * other;
19
+ }
20
+
21
+ export { multiply };
@@ -0,0 +1,2 @@
1
+ export { AbortError } from './AbortError.mjs';
2
+ export { TimeoutError } from './TimeoutError.mjs';
@@ -0,0 +1,2 @@
1
+ export { AbortError } from './AbortError.js';
2
+ export { TimeoutError } from './TimeoutError.js';
@@ -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,2 @@
1
+ export { AbortError } from './AbortError.mjs';
2
+ export { TimeoutError } from './TimeoutError.mjs';
@@ -15,6 +15,7 @@ export { once } from './once.mjs';
15
15
  export { partial } from './partial.mjs';
16
16
  export { partialRight } from './partialRight.mjs';
17
17
  export { rest } from './rest.mjs';
18
+ export { retry } from './retry.mjs';
18
19
  export { spread } from './spread.mjs';
19
20
  export { ThrottledFunction, throttle } from './throttle.mjs';
20
21
  export { unary } from './unary.mjs';
@@ -15,6 +15,7 @@ export { once } from './once.js';
15
15
  export { partial } from './partial.js';
16
16
  export { partialRight } from './partialRight.js';
17
17
  export { rest } from './rest.js';
18
+ export { retry } from './retry.js';
18
19
  export { spread } from './spread.js';
19
20
  export { ThrottledFunction, throttle } from './throttle.js';
20
21
  export { unary } from './unary.js';
@@ -2,7 +2,7 @@
2
2
 
3
3
  Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
4
 
5
- const unary = require('../_chunk/unary-B6qG7C.js');
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) {
@@ -98,6 +98,7 @@ exports.once = unary.once;
98
98
  exports.partial = unary.partial;
99
99
  exports.partialRight = unary.partialRight;
100
100
  exports.rest = unary.rest;
101
+ exports.retry = unary.retry;
101
102
  exports.unary = unary.unary;
102
103
  exports.noop = noop.noop;
103
104
  exports.before = before;
@@ -15,6 +15,7 @@ export { once } from './once.mjs';
15
15
  export { partial } from './partial.mjs';
16
16
  export { partialRight } from './partialRight.mjs';
17
17
  export { rest } from './rest.mjs';
18
+ export { retry } from './retry.mjs';
18
19
  export { spread } from './spread.mjs';
19
20
  export { throttle } from './throttle.mjs';
20
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
@@ -77,6 +77,7 @@ export { once } from './function/once.mjs';
77
77
  export { partial } from './function/partial.mjs';
78
78
  export { partialRight } from './function/partialRight.mjs';
79
79
  export { rest } from './function/rest.mjs';
80
+ export { retry } from './function/retry.mjs';
80
81
  export { spread } from './function/spread.mjs';
81
82
  export { ThrottledFunction, throttle } from './function/throttle.mjs';
82
83
  export { unary } from './function/unary.mjs';
@@ -126,6 +127,7 @@ export { isNotNil } from './predicate/isNotNil.mjs';
126
127
  export { isNull } from './predicate/isNull.mjs';
127
128
  export { isPlainObject } from './predicate/isPlainObject.mjs';
128
129
  export { isPrimitive } from './predicate/isPrimitive.mjs';
130
+ export { isPromise } from './predicate/isPromise.mjs';
129
131
  export { isRegExp } from './predicate/isRegExp.mjs';
130
132
  export { isSet } from './predicate/isSet.mjs';
131
133
  export { isString } from './predicate/isString.mjs';
@@ -135,6 +137,8 @@ export { isUndefined } from './predicate/isUndefined.mjs';
135
137
  export { isWeakMap } from './predicate/isWeakMap.mjs';
136
138
  export { isWeakSet } from './predicate/isWeakSet.mjs';
137
139
  export { delay } from './promise/delay.mjs';
140
+ export { Mutex } from './promise/mutex.mjs';
141
+ export { Semaphore } from './promise/semaphore.mjs';
138
142
  export { timeout } from './promise/timeout.mjs';
139
143
  export { withTimeout } from './promise/withTimeout.mjs';
140
144
  export { camelCase } from './string/camelCase.mjs';
package/dist/index.d.ts CHANGED
@@ -77,6 +77,7 @@ export { once } from './function/once.js';
77
77
  export { partial } from './function/partial.js';
78
78
  export { partialRight } from './function/partialRight.js';
79
79
  export { rest } from './function/rest.js';
80
+ export { retry } from './function/retry.js';
80
81
  export { spread } from './function/spread.js';
81
82
  export { ThrottledFunction, throttle } from './function/throttle.js';
82
83
  export { unary } from './function/unary.js';
@@ -126,6 +127,7 @@ export { isNotNil } from './predicate/isNotNil.js';
126
127
  export { isNull } from './predicate/isNull.js';
127
128
  export { isPlainObject } from './predicate/isPlainObject.js';
128
129
  export { isPrimitive } from './predicate/isPrimitive.js';
130
+ export { isPromise } from './predicate/isPromise.js';
129
131
  export { isRegExp } from './predicate/isRegExp.js';
130
132
  export { isSet } from './predicate/isSet.js';
131
133
  export { isString } from './predicate/isString.js';
@@ -135,6 +137,8 @@ export { isUndefined } from './predicate/isUndefined.js';
135
137
  export { isWeakMap } from './predicate/isWeakMap.js';
136
138
  export { isWeakSet } from './predicate/isWeakSet.js';
137
139
  export { delay } from './promise/delay.js';
140
+ export { Mutex } from './promise/mutex.js';
141
+ export { Semaphore } from './promise/semaphore.js';
138
142
  export { timeout } from './promise/timeout.js';
139
143
  export { withTimeout } from './promise/withTimeout.js';
140
144
  export { camelCase } from './string/camelCase.js';