es-toolkit 1.43.0-dev.1719 → 1.43.0-dev.1721

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.
@@ -17,6 +17,18 @@ interface RetryOptions {
17
17
  * An AbortSignal to cancel the retry operation.
18
18
  */
19
19
  signal?: AbortSignal;
20
+ /**
21
+ * A function that determines whether to retry based on the error and attempt number.
22
+ * If not provided, all errors will trigger a retry.
23
+ *
24
+ * @param {unknown} error - The error that occurred.
25
+ * @param {number} attempt - The current attempt number (0-indexed).
26
+ * @returns {boolean} Whether to retry.
27
+ *
28
+ * @example
29
+ * shouldRetry: (error, attempt) => error.status >= 500
30
+ */
31
+ shouldRetry?: (error: unknown, attempt: number) => boolean;
20
32
  }
21
33
  /**
22
34
  * Retries a function that returns a promise until it resolves successfully.
@@ -52,6 +64,7 @@ declare function retry<T>(func: () => Promise<T>, retries: number): Promise<T>;
52
64
  * @param {number | ((attempts: number) => number)} [options.delay=0] - Delay(milliseconds) between retries.
53
65
  * @param {number} [options.retries=Infinity] - The number of retries to attempt.
54
66
  * @param {AbortSignal} [options.signal] - An AbortSignal to cancel the retry operation.
67
+ * @param {(error: unknown, attempt: number) => boolean} [options.shouldRetry] - A function that determines whether to retry.
55
68
  * @returns {Promise<T>} A promise that resolves with the value of the successful function call.
56
69
  *
57
70
  * @example
@@ -17,6 +17,18 @@ interface RetryOptions {
17
17
  * An AbortSignal to cancel the retry operation.
18
18
  */
19
19
  signal?: AbortSignal;
20
+ /**
21
+ * A function that determines whether to retry based on the error and attempt number.
22
+ * If not provided, all errors will trigger a retry.
23
+ *
24
+ * @param {unknown} error - The error that occurred.
25
+ * @param {number} attempt - The current attempt number (0-indexed).
26
+ * @returns {boolean} Whether to retry.
27
+ *
28
+ * @example
29
+ * shouldRetry: (error, attempt) => error.status >= 500
30
+ */
31
+ shouldRetry?: (error: unknown, attempt: number) => boolean;
20
32
  }
21
33
  /**
22
34
  * Retries a function that returns a promise until it resolves successfully.
@@ -52,6 +64,7 @@ declare function retry<T>(func: () => Promise<T>, retries: number): Promise<T>;
52
64
  * @param {number | ((attempts: number) => number)} [options.delay=0] - Delay(milliseconds) between retries.
53
65
  * @param {number} [options.retries=Infinity] - The number of retries to attempt.
54
66
  * @param {AbortSignal} [options.signal] - An AbortSignal to cancel the retry operation.
67
+ * @param {(error: unknown, attempt: number) => boolean} [options.shouldRetry] - A function that determines whether to retry.
55
68
  * @returns {Promise<T>} A promise that resolves with the value of the successful function call.
56
69
  *
57
70
  * @example
@@ -6,19 +6,23 @@ const delay = require('../promise/delay.js');
6
6
 
7
7
  const DEFAULT_DELAY = 0;
8
8
  const DEFAULT_RETRIES = Number.POSITIVE_INFINITY;
9
+ const DEFAULT_SHOULD_RETRY = () => true;
9
10
  async function retry(func, _options) {
10
11
  let delay$1;
11
12
  let retries;
12
13
  let signal;
14
+ let shouldRetry;
13
15
  if (typeof _options === 'number') {
14
16
  delay$1 = DEFAULT_DELAY;
15
17
  retries = _options;
16
18
  signal = undefined;
19
+ shouldRetry = DEFAULT_SHOULD_RETRY;
17
20
  }
18
21
  else {
19
22
  delay$1 = _options?.delay ?? DEFAULT_DELAY;
20
23
  retries = _options?.retries ?? DEFAULT_RETRIES;
21
24
  signal = _options?.signal;
25
+ shouldRetry = _options?.shouldRetry ?? DEFAULT_SHOULD_RETRY;
22
26
  }
23
27
  let error;
24
28
  for (let attempts = 0; attempts < retries; attempts++) {
@@ -30,6 +34,9 @@ async function retry(func, _options) {
30
34
  }
31
35
  catch (err) {
32
36
  error = err;
37
+ if (!shouldRetry(err, attempts)) {
38
+ throw err;
39
+ }
33
40
  const currentDelay = typeof delay$1 === 'function' ? delay$1(attempts) : delay$1;
34
41
  await delay.delay(currentDelay);
35
42
  }
@@ -2,19 +2,23 @@ import { delay } from '../promise/delay.mjs';
2
2
 
3
3
  const DEFAULT_DELAY = 0;
4
4
  const DEFAULT_RETRIES = Number.POSITIVE_INFINITY;
5
+ const DEFAULT_SHOULD_RETRY = () => true;
5
6
  async function retry(func, _options) {
6
7
  let delay$1;
7
8
  let retries;
8
9
  let signal;
10
+ let shouldRetry;
9
11
  if (typeof _options === 'number') {
10
12
  delay$1 = DEFAULT_DELAY;
11
13
  retries = _options;
12
14
  signal = undefined;
15
+ shouldRetry = DEFAULT_SHOULD_RETRY;
13
16
  }
14
17
  else {
15
18
  delay$1 = _options?.delay ?? DEFAULT_DELAY;
16
19
  retries = _options?.retries ?? DEFAULT_RETRIES;
17
20
  signal = _options?.signal;
21
+ shouldRetry = _options?.shouldRetry ?? DEFAULT_SHOULD_RETRY;
18
22
  }
19
23
  let error;
20
24
  for (let attempts = 0; attempts < retries; attempts++) {
@@ -26,6 +30,9 @@ async function retry(func, _options) {
26
30
  }
27
31
  catch (err) {
28
32
  error = err;
33
+ if (!shouldRetry(err, attempts)) {
34
+ throw err;
35
+ }
29
36
  const currentDelay = typeof delay$1 === 'function' ? delay$1(attempts) : delay$1;
30
37
  await delay(currentDelay);
31
38
  }
@@ -18,7 +18,7 @@ function flattenObjectImpl(object, prefix, delimiter) {
18
18
  Object.assign(result, flattenObjectImpl(value, prefixedKey, delimiter));
19
19
  continue;
20
20
  }
21
- if (Array.isArray(value)) {
21
+ if (Array.isArray(value) && value.length > 0) {
22
22
  Object.assign(result, flattenObjectImpl(value, prefixedKey, delimiter));
23
23
  continue;
24
24
  }
@@ -14,7 +14,7 @@ function flattenObjectImpl(object, prefix, delimiter) {
14
14
  Object.assign(result, flattenObjectImpl(value, prefixedKey, delimiter));
15
15
  continue;
16
16
  }
17
- if (Array.isArray(value)) {
17
+ if (Array.isArray(value) && value.length > 0) {
18
18
  Object.assign(result, flattenObjectImpl(value, prefixedKey, delimiter));
19
19
  continue;
20
20
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "es-toolkit",
3
- "version": "1.43.0-dev.1719+50339c32",
3
+ "version": "1.43.0-dev.1721+bc2b3b7a",
4
4
  "description": "A state-of-the-art, high-performance JavaScript utility library with a small bundle size and strong type annotations.",
5
5
  "homepage": "https://es-toolkit.dev",
6
6
  "bugs": "https://github.com/toss/es-toolkit/issues",