@ls-stack/utils 2.4.0 → 2.6.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.
@@ -29,11 +29,15 @@ function omit(obj, keys) {
29
29
  }
30
30
  return result;
31
31
  }
32
+ function looseGetObjectProperty(obj, key) {
33
+ return obj[key];
34
+ }
32
35
 
33
36
  export {
34
37
  objectTypedEntries,
35
38
  pick,
36
39
  mapArrayToObject,
37
40
  mapObjectToObject,
38
- omit
41
+ omit,
42
+ looseGetObjectProperty
39
43
  };
package/dist/main.d.ts CHANGED
@@ -9,6 +9,7 @@
9
9
  ///<reference path="deepEqual.d.ts" />
10
10
  ///<reference path="enhancedMap.d.ts" />
11
11
  ///<reference path="exhaustiveMatch.d.ts" />
12
+ ///<reference path="internalUtils.d.ts" />
12
13
  ///<reference path="interpolate.d.ts" />
13
14
  ///<reference path="levenshtein.d.ts" />
14
15
  ///<reference path="main.d.ts" />
@@ -16,6 +17,7 @@
16
17
  ///<reference path="objUtils.d.ts" />
17
18
  ///<reference path="parallelAsyncCalls.d.ts" />
18
19
  ///<reference path="promiseUtils.d.ts" />
20
+ ///<reference path="retryOnError.d.ts" />
19
21
  ///<reference path="rsResult.d.ts" />
20
22
  ///<reference path="runShellCmd.d.ts" />
21
23
  ///<reference path="safeJson.d.ts" />
package/dist/objUtils.cjs CHANGED
@@ -20,6 +20,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/objUtils.ts
21
21
  var objUtils_exports = {};
22
22
  __export(objUtils_exports, {
23
+ looseGetObjectProperty: () => looseGetObjectProperty,
23
24
  mapArrayToObject: () => mapArrayToObject,
24
25
  mapObjectToObject: () => mapObjectToObject,
25
26
  objectTypedEntries: () => objectTypedEntries,
@@ -57,8 +58,12 @@ function omit(obj, keys) {
57
58
  }
58
59
  return result;
59
60
  }
61
+ function looseGetObjectProperty(obj, key) {
62
+ return obj[key];
63
+ }
60
64
  // Annotate the CommonJS export names for ESM import in node:
61
65
  0 && (module.exports = {
66
+ looseGetObjectProperty,
62
67
  mapArrayToObject,
63
68
  mapObjectToObject,
64
69
  objectTypedEntries,
@@ -3,5 +3,6 @@ declare function pick<T, K extends keyof T>(obj: T | undefined, keys: K[], renam
3
3
  declare function mapArrayToObject<T, K extends string, O>(array: T[], mapper: (item: T, index: number) => [K, O]): Record<K, O>;
4
4
  declare function mapObjectToObject<I extends Record<string | number | symbol, unknown>, K extends string | number | symbol, O>(obj: I, mapper: (key: keyof I, value: I[keyof I]) => [K, O]): Record<K, O>;
5
5
  declare function omit<T extends Record<string, unknown>, K extends keyof T>(obj: T, keys: K[]): Omit<T, K>;
6
+ declare function looseGetObjectProperty<T extends Record<string, unknown>>(obj: T, key: string): T[keyof T] | undefined;
6
7
 
7
- export { mapArrayToObject, mapObjectToObject, objectTypedEntries, omit, pick };
8
+ export { looseGetObjectProperty, mapArrayToObject, mapObjectToObject, objectTypedEntries, omit, pick };
@@ -3,5 +3,6 @@ declare function pick<T, K extends keyof T>(obj: T | undefined, keys: K[], renam
3
3
  declare function mapArrayToObject<T, K extends string, O>(array: T[], mapper: (item: T, index: number) => [K, O]): Record<K, O>;
4
4
  declare function mapObjectToObject<I extends Record<string | number | symbol, unknown>, K extends string | number | symbol, O>(obj: I, mapper: (key: keyof I, value: I[keyof I]) => [K, O]): Record<K, O>;
5
5
  declare function omit<T extends Record<string, unknown>, K extends keyof T>(obj: T, keys: K[]): Omit<T, K>;
6
+ declare function looseGetObjectProperty<T extends Record<string, unknown>>(obj: T, key: string): T[keyof T] | undefined;
6
7
 
7
- export { mapArrayToObject, mapObjectToObject, objectTypedEntries, omit, pick };
8
+ export { looseGetObjectProperty, mapArrayToObject, mapObjectToObject, objectTypedEntries, omit, pick };
package/dist/objUtils.js CHANGED
@@ -1,11 +1,13 @@
1
1
  import {
2
+ looseGetObjectProperty,
2
3
  mapArrayToObject,
3
4
  mapObjectToObject,
4
5
  objectTypedEntries,
5
6
  omit,
6
7
  pick
7
- } from "./chunk-RVVH3MDA.js";
8
+ } from "./chunk-ZE3DLPMN.js";
8
9
  export {
10
+ looseGetObjectProperty,
9
11
  mapArrayToObject,
10
12
  mapObjectToObject,
11
13
  objectTypedEntries,
@@ -0,0 +1,76 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/retryOnError.ts
21
+ var retryOnError_exports = {};
22
+ __export(retryOnError_exports, {
23
+ retryOnError: () => retryOnError
24
+ });
25
+ module.exports = __toCommonJS(retryOnError_exports);
26
+
27
+ // src/sleep.ts
28
+ function sleep(ms) {
29
+ return new Promise((resolve) => setTimeout(resolve, ms));
30
+ }
31
+
32
+ // src/retryOnError.ts
33
+ async function retryOnError(fn, maxRetries, options = {}, retry = 0) {
34
+ const {
35
+ delayBetweenRetriesMs,
36
+ retryCondition,
37
+ maxErrorDurationMs = 400
38
+ } = options;
39
+ if (options.debugId) {
40
+ if (retry > 0) {
41
+ console.info(
42
+ `Retrying ${options.debugId} (retry ${retry}/${maxRetries}) after error`
43
+ );
44
+ }
45
+ }
46
+ const startTime = Date.now();
47
+ try {
48
+ return await fn();
49
+ } catch (error) {
50
+ if (maxRetries > 0) {
51
+ const errorDuration = Date.now() - startTime;
52
+ const shouldRetry = retryCondition ? retryCondition(error) : true;
53
+ let maxErrorDurationMsToUse = maxErrorDurationMs;
54
+ if (typeof shouldRetry === "boolean") {
55
+ if (!shouldRetry) throw error;
56
+ } else {
57
+ maxErrorDurationMsToUse = shouldRetry.maxErrorDurationMs;
58
+ }
59
+ if (errorDuration > maxErrorDurationMsToUse) {
60
+ throw error;
61
+ }
62
+ if (delayBetweenRetriesMs) {
63
+ await sleep(
64
+ typeof delayBetweenRetriesMs === "function" ? delayBetweenRetriesMs(retry) : delayBetweenRetriesMs
65
+ );
66
+ }
67
+ return retryOnError(fn, maxRetries - 1, options, retry + 1);
68
+ } else {
69
+ throw error;
70
+ }
71
+ }
72
+ }
73
+ // Annotate the CommonJS export names for ESM import in node:
74
+ 0 && (module.exports = {
75
+ retryOnError
76
+ });
@@ -0,0 +1,10 @@
1
+ declare function retryOnError<T>(fn: () => Promise<T>, maxRetries: number, options?: {
2
+ delayBetweenRetriesMs?: number | ((retry: number) => number);
3
+ retryCondition?: (error: unknown) => boolean | {
4
+ maxErrorDurationMs: number;
5
+ };
6
+ maxErrorDurationMs?: number;
7
+ debugId?: string;
8
+ }, retry?: number): Promise<T>;
9
+
10
+ export { retryOnError };
@@ -0,0 +1,10 @@
1
+ declare function retryOnError<T>(fn: () => Promise<T>, maxRetries: number, options?: {
2
+ delayBetweenRetriesMs?: number | ((retry: number) => number);
3
+ retryCondition?: (error: unknown) => boolean | {
4
+ maxErrorDurationMs: number;
5
+ };
6
+ maxErrorDurationMs?: number;
7
+ debugId?: string;
8
+ }, retry?: number): Promise<T>;
9
+
10
+ export { retryOnError };
@@ -0,0 +1,48 @@
1
+ import {
2
+ sleep
3
+ } from "./chunk-5DZT3Z5Z.js";
4
+
5
+ // src/retryOnError.ts
6
+ async function retryOnError(fn, maxRetries, options = {}, retry = 0) {
7
+ const {
8
+ delayBetweenRetriesMs,
9
+ retryCondition,
10
+ maxErrorDurationMs = 400
11
+ } = options;
12
+ if (options.debugId) {
13
+ if (retry > 0) {
14
+ console.info(
15
+ `Retrying ${options.debugId} (retry ${retry}/${maxRetries}) after error`
16
+ );
17
+ }
18
+ }
19
+ const startTime = Date.now();
20
+ try {
21
+ return await fn();
22
+ } catch (error) {
23
+ if (maxRetries > 0) {
24
+ const errorDuration = Date.now() - startTime;
25
+ const shouldRetry = retryCondition ? retryCondition(error) : true;
26
+ let maxErrorDurationMsToUse = maxErrorDurationMs;
27
+ if (typeof shouldRetry === "boolean") {
28
+ if (!shouldRetry) throw error;
29
+ } else {
30
+ maxErrorDurationMsToUse = shouldRetry.maxErrorDurationMs;
31
+ }
32
+ if (errorDuration > maxErrorDurationMsToUse) {
33
+ throw error;
34
+ }
35
+ if (delayBetweenRetriesMs) {
36
+ await sleep(
37
+ typeof delayBetweenRetriesMs === "function" ? delayBetweenRetriesMs(retry) : delayBetweenRetriesMs
38
+ );
39
+ }
40
+ return retryOnError(fn, maxRetries - 1, options, retry + 1);
41
+ } else {
42
+ throw error;
43
+ }
44
+ }
45
+ }
46
+ export {
47
+ retryOnError
48
+ };
package/dist/testUtils.js CHANGED
@@ -7,7 +7,7 @@ import {
7
7
  import {
8
8
  omit,
9
9
  pick
10
- } from "./chunk-RVVH3MDA.js";
10
+ } from "./chunk-ZE3DLPMN.js";
11
11
  import {
12
12
  arrayWithPrevAndIndex,
13
13
  filterAndMap
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@ls-stack/utils",
3
3
  "description": "Typescript utils",
4
- "version": "2.4.0",
4
+ "version": "2.6.0",
5
5
  "license": "MIT",
6
6
  "files": [
7
7
  "dist"
@@ -74,6 +74,11 @@
74
74
  "types": "./dist/exhaustiveMatch.d.ts",
75
75
  "require": "./dist/exhaustiveMatch.cjs"
76
76
  },
77
+ "./internalUtils": {
78
+ "import": "./dist/internalUtils.js",
79
+ "types": "./dist/internalUtils.d.ts",
80
+ "require": "./dist/internalUtils.cjs"
81
+ },
77
82
  "./interpolate": {
78
83
  "import": "./dist/interpolate.js",
79
84
  "types": "./dist/interpolate.d.ts",
@@ -109,6 +114,11 @@
109
114
  "types": "./dist/promiseUtils.d.ts",
110
115
  "require": "./dist/promiseUtils.cjs"
111
116
  },
117
+ "./retryOnError": {
118
+ "import": "./dist/retryOnError.js",
119
+ "types": "./dist/retryOnError.d.ts",
120
+ "require": "./dist/retryOnError.cjs"
121
+ },
112
122
  "./rsResult": {
113
123
  "import": "./dist/rsResult.js",
114
124
  "types": "./dist/rsResult.d.ts",
@@ -225,6 +235,9 @@
225
235
  "exhaustiveMatch": [
226
236
  "./dist/exhaustiveMatch.d.ts"
227
237
  ],
238
+ "internalUtils": [
239
+ "./dist/internalUtils.d.ts"
240
+ ],
228
241
  "interpolate": [
229
242
  "./dist/interpolate.d.ts"
230
243
  ],
@@ -246,6 +259,9 @@
246
259
  "promiseUtils": [
247
260
  "./dist/promiseUtils.d.ts"
248
261
  ],
262
+ "retryOnError": [
263
+ "./dist/retryOnError.d.ts"
264
+ ],
249
265
  "rsResult": [
250
266
  "./dist/rsResult.d.ts"
251
267
  ],