@rg-dev/stdlib 1.0.0 → 1.0.1

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.
@@ -1,167 +1,167 @@
1
- var __defProp = Object.defineProperty;
2
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
- var __getOwnPropNames = Object.getOwnPropertyNames;
4
- var __hasOwnProp = Object.prototype.hasOwnProperty;
5
- var __export = (target, all) => {
6
- for (var name in all)
7
- __defProp(target, name, { get: all[name], enumerable: true });
8
- };
9
- var __copyProps = (to, from, except, desc) => {
10
- if (from && typeof from === "object" || typeof from === "function") {
11
- for (let key of __getOwnPropNames(from))
12
- if (!__hasOwnProp.call(to, key) && key !== except)
13
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
- }
15
- return to;
16
- };
17
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
-
19
- // src/common-env.ts
20
- var common_env_exports = {};
21
- __export(common_env_exports, {
22
- Optional: () => Optional,
23
- catchPromise: () => catchPromise,
24
- doSafe: () => doSafe,
25
- isNumber: () => isNumber,
26
- isRunningOnServer: () => isRunningOnServer,
27
- promiseRetry: () => promiseRetry,
28
- promiseWithTimeout: () => promiseWithTimeout,
29
- sleep: () => sleep,
30
- useServer: () => useServer
31
- });
32
- module.exports = __toCommonJS(common_env_exports);
33
-
34
- // src/promise-retry.ts
35
- var defaultOptions = {
36
- maxAttempts: 10,
37
- retryDelay: 0,
38
- onError: (err, attempt) => {
39
- }
40
- };
41
- function promiseRetry(func, options = defaultOptions, attempt = 1) {
42
- const config = { ...defaultOptions, ...options };
43
- return func(attempt).catch(
44
- (err) => {
45
- config.onError(err, attempt);
46
- if (attempt < config.maxAttempts) {
47
- return new Promise(
48
- (resolve) => {
49
- setTimeout(
50
- () => resolve(promiseRetry(func, options, attempt + 1)),
51
- config.retryDelay
52
- );
53
- }
54
- );
55
- } else {
56
- throw err;
57
- }
58
- }
59
- );
60
- }
61
-
62
- // src/common-env.ts
63
- var Optional = class _Optional {
64
- constructor(value) {
65
- this.value = value;
66
- }
67
- value = void 0;
68
- orElseThrow(msg) {
69
- if (this.value == void 0)
70
- throw new Error(msg || "Value is empty");
71
- return this.value;
72
- }
73
- static of(val) {
74
- return new _Optional(val);
75
- }
76
- setValue(newValue) {
77
- this.value = newValue;
78
- return this;
79
- }
80
- getValue() {
81
- return this.value;
82
- }
83
- ifPresent(action) {
84
- if (this.value == void 0)
85
- return;
86
- try {
87
- action(this.value);
88
- } catch (e) {
89
- console.error(e);
90
- }
91
- }
92
- };
93
- function sleep(ms) {
94
- return new Promise((resolve) => setTimeout(() => resolve(""), ms));
95
- }
96
- function isNumber(num) {
97
- if (typeof num === "number") {
98
- return num - num === 0;
99
- }
100
- if (typeof num === "string" && num.trim() !== "") {
101
- return Number.isFinite ? Number.isFinite(+num) : isFinite(+num);
102
- }
103
- return false;
104
- }
105
- function promiseWithTimeout(promise, timeout, errMsg = "The promise timed out.") {
106
- let timeoutID;
107
- const timeOutPromise = new Promise(
108
- (_resolve, reject) => {
109
- timeoutID = setTimeout(() => reject(new Error(errMsg)), timeout);
110
- }
111
- );
112
- return Promise.race([promise, timeOutPromise]).finally(
113
- () => clearTimeout(timeoutID)
114
- );
115
- }
116
- async function catchPromise(promise) {
117
- try {
118
- return { result: await promise, err: null };
119
- } catch (e) {
120
- return { result: null, err: e };
121
- }
122
- }
123
- function doSafe(safe, onError) {
124
- var _a;
125
- if (!safe)
126
- return;
127
- try {
128
- const result = safe();
129
- if (isPromise(result)) {
130
- (_a = result["catch"]) == null ? void 0 : _a.call(
131
- result,
132
- (error) => {
133
- if (error instanceof Error) {
134
- onError == null ? void 0 : onError(error);
135
- } else {
136
- onError == null ? void 0 : onError(new Error(String(error)));
137
- }
138
- }
139
- );
140
- }
141
- } catch (error) {
142
- if (error instanceof Error) {
143
- onError == null ? void 0 : onError(error);
144
- } else {
145
- onError == null ? void 0 : onError(new Error(String(error)));
146
- }
147
- }
148
- }
149
- function isRunningOnServer() {
150
- return (globalThis == null ? void 0 : globalThis.process) !== void 0;
151
- }
152
- async function useServer(fn, onError) {
153
- if (isRunningOnServer()) {
154
- try {
155
- return await fn();
156
- } catch (e) {
157
- if (onError) {
158
- onError(e);
159
- } else {
160
- console.error("Error from useServer", e);
161
- }
162
- }
163
- }
164
- }
165
- function isPromise(value) {
166
- return Boolean(value && typeof value.then === "function");
167
- }
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+
19
+ // src/common-env.ts
20
+ var common_env_exports = {};
21
+ __export(common_env_exports, {
22
+ Optional: () => Optional,
23
+ catchPromise: () => catchPromise,
24
+ doSafe: () => doSafe,
25
+ isNumber: () => isNumber,
26
+ isRunningOnServer: () => isRunningOnServer,
27
+ promiseRetry: () => promiseRetry,
28
+ promiseWithTimeout: () => promiseWithTimeout,
29
+ sleep: () => sleep,
30
+ useServer: () => useServer
31
+ });
32
+ module.exports = __toCommonJS(common_env_exports);
33
+
34
+ // src/promise-retry.ts
35
+ var defaultOptions = {
36
+ maxAttempts: 10,
37
+ retryDelay: 0,
38
+ onError: (err, attempt) => {
39
+ }
40
+ };
41
+ function promiseRetry(func, options = defaultOptions, attempt = 1) {
42
+ const config = { ...defaultOptions, ...options };
43
+ return func(attempt).catch(
44
+ (err) => {
45
+ config.onError(err, attempt);
46
+ if (attempt < config.maxAttempts) {
47
+ return new Promise(
48
+ (resolve) => {
49
+ setTimeout(
50
+ () => resolve(promiseRetry(func, options, attempt + 1)),
51
+ config.retryDelay
52
+ );
53
+ }
54
+ );
55
+ } else {
56
+ throw err;
57
+ }
58
+ }
59
+ );
60
+ }
61
+
62
+ // src/common-env.ts
63
+ var Optional = class _Optional {
64
+ constructor(value) {
65
+ this.value = value;
66
+ }
67
+ value = void 0;
68
+ orElseThrow(msg) {
69
+ if (this.value == void 0)
70
+ throw new Error(msg || "Value is empty");
71
+ return this.value;
72
+ }
73
+ static of(val) {
74
+ return new _Optional(val);
75
+ }
76
+ setValue(newValue) {
77
+ this.value = newValue;
78
+ return this;
79
+ }
80
+ getValue() {
81
+ return this.value;
82
+ }
83
+ ifPresent(action) {
84
+ if (this.value == void 0)
85
+ return;
86
+ try {
87
+ action(this.value);
88
+ } catch (e) {
89
+ console.error(e);
90
+ }
91
+ }
92
+ };
93
+ function sleep(ms) {
94
+ return new Promise((resolve) => setTimeout(() => resolve(""), ms));
95
+ }
96
+ function isNumber(num) {
97
+ if (typeof num === "number") {
98
+ return num - num === 0;
99
+ }
100
+ if (typeof num === "string" && num.trim() !== "") {
101
+ return Number.isFinite ? Number.isFinite(+num) : isFinite(+num);
102
+ }
103
+ return false;
104
+ }
105
+ function promiseWithTimeout(promise, timeout, errMsg = "The promise timed out.") {
106
+ let timeoutID;
107
+ const timeOutPromise = new Promise(
108
+ (_resolve, reject) => {
109
+ timeoutID = setTimeout(() => reject(new Error(errMsg)), timeout);
110
+ }
111
+ );
112
+ return Promise.race([promise, timeOutPromise]).finally(
113
+ () => clearTimeout(timeoutID)
114
+ );
115
+ }
116
+ async function catchPromise(promise) {
117
+ try {
118
+ return { result: await promise, err: null };
119
+ } catch (e) {
120
+ return { result: null, err: e };
121
+ }
122
+ }
123
+ function doSafe(safe, onError) {
124
+ var _a;
125
+ if (!safe)
126
+ return;
127
+ try {
128
+ const result = safe();
129
+ if (isPromise(result)) {
130
+ (_a = result["catch"]) == null ? void 0 : _a.call(
131
+ result,
132
+ (error) => {
133
+ if (error instanceof Error) {
134
+ onError == null ? void 0 : onError(error);
135
+ } else {
136
+ onError == null ? void 0 : onError(new Error(String(error)));
137
+ }
138
+ }
139
+ );
140
+ }
141
+ } catch (error) {
142
+ if (error instanceof Error) {
143
+ onError == null ? void 0 : onError(error);
144
+ } else {
145
+ onError == null ? void 0 : onError(new Error(String(error)));
146
+ }
147
+ }
148
+ }
149
+ function isRunningOnServer() {
150
+ return (globalThis == null ? void 0 : globalThis.process) !== void 0;
151
+ }
152
+ async function useServer(fn, onError) {
153
+ if (isRunningOnServer()) {
154
+ try {
155
+ return await fn();
156
+ } catch (e) {
157
+ if (onError) {
158
+ onError(e);
159
+ } else {
160
+ console.error("Error from useServer", e);
161
+ }
162
+ }
163
+ }
164
+ }
165
+ function isPromise(value) {
166
+ return Boolean(value && typeof value.then === "function");
167
+ }
@@ -3,8 +3,8 @@ type Options = {
3
3
  retryDelay: number;
4
4
  onError: (err: any, attempt: number) => void;
5
5
  };
6
- declare function promiseRetry<T>(func: (attempt: number) => Promise<T>, options?: Partial<Options>, attempt?: number): Promise<T>;
7
-
6
+ declare function promiseRetry<T>(func: (attempt: number) => Promise<T>, options?: Partial<Options>, attempt?: number): Promise<T>;
7
+
8
8
  declare class Optional<T> {
9
9
  private constructor();
10
10
  private value;
@@ -23,6 +23,6 @@ declare function catchPromise<T>(promise: Promise<T>): Promise<{
23
23
  }>;
24
24
  declare function doSafe(safe: () => Promise<void> | void, onError?: (error: Error) => void): void;
25
25
  declare function isRunningOnServer(): boolean;
26
- declare function useServer(fn: () => (Promise<any> | void), onError?: (err: Error) => void): Promise<any>;
27
-
28
- export { Optional, catchPromise, doSafe, isNumber, isRunningOnServer, promiseRetry, promiseWithTimeout, sleep, useServer };
26
+ declare function useServer(fn: () => (Promise<any> | void), onError?: (err: Error) => void): Promise<any>;
27
+
28
+ export { Optional, catchPromise, doSafe, isNumber, isRunningOnServer, promiseRetry, promiseWithTimeout, sleep, useServer };
@@ -3,8 +3,8 @@ type Options = {
3
3
  retryDelay: number;
4
4
  onError: (err: any, attempt: number) => void;
5
5
  };
6
- declare function promiseRetry<T>(func: (attempt: number) => Promise<T>, options?: Partial<Options>, attempt?: number): Promise<T>;
7
-
6
+ declare function promiseRetry<T>(func: (attempt: number) => Promise<T>, options?: Partial<Options>, attempt?: number): Promise<T>;
7
+
8
8
  declare class Optional<T> {
9
9
  private constructor();
10
10
  private value;
@@ -23,6 +23,6 @@ declare function catchPromise<T>(promise: Promise<T>): Promise<{
23
23
  }>;
24
24
  declare function doSafe(safe: () => Promise<void> | void, onError?: (error: Error) => void): void;
25
25
  declare function isRunningOnServer(): boolean;
26
- declare function useServer(fn: () => (Promise<any> | void), onError?: (err: Error) => void): Promise<any>;
27
-
28
- export { Optional, catchPromise, doSafe, isNumber, isRunningOnServer, promiseRetry, promiseWithTimeout, sleep, useServer };
26
+ declare function useServer(fn: () => (Promise<any> | void), onError?: (err: Error) => void): Promise<any>;
27
+
28
+ export { Optional, catchPromise, doSafe, isNumber, isRunningOnServer, promiseRetry, promiseWithTimeout, sleep, useServer };
package/lib/common-env.js CHANGED
@@ -1,145 +1,145 @@
1
- // src/promise-retry.ts
2
- var defaultOptions = {
3
- maxAttempts: 10,
4
- retryDelay: 0,
5
- onError: (err, attempt) => {
6
- }
7
- };
8
- function promiseRetry(func, options = defaultOptions, attempt = 1) {
9
- const config = { ...defaultOptions, ...options };
10
- return func(attempt).catch(
11
- (err) => {
12
- config.onError(err, attempt);
13
- if (attempt < config.maxAttempts) {
14
- return new Promise(
15
- (resolve) => {
16
- setTimeout(
17
- () => resolve(promiseRetry(func, options, attempt + 1)),
18
- config.retryDelay
19
- );
20
- }
21
- );
22
- } else {
23
- throw err;
24
- }
25
- }
26
- );
27
- }
28
-
29
- // src/common-env.ts
30
- var Optional = class _Optional {
31
- constructor(value) {
32
- this.value = value;
33
- }
34
- value = void 0;
35
- orElseThrow(msg) {
36
- if (this.value == void 0)
37
- throw new Error(msg || "Value is empty");
38
- return this.value;
39
- }
40
- static of(val) {
41
- return new _Optional(val);
42
- }
43
- setValue(newValue) {
44
- this.value = newValue;
45
- return this;
46
- }
47
- getValue() {
48
- return this.value;
49
- }
50
- ifPresent(action) {
51
- if (this.value == void 0)
52
- return;
53
- try {
54
- action(this.value);
55
- } catch (e) {
56
- console.error(e);
57
- }
58
- }
59
- };
60
- function sleep(ms) {
61
- return new Promise((resolve) => setTimeout(() => resolve(""), ms));
62
- }
63
- function isNumber(num) {
64
- if (typeof num === "number") {
65
- return num - num === 0;
66
- }
67
- if (typeof num === "string" && num.trim() !== "") {
68
- return Number.isFinite ? Number.isFinite(+num) : isFinite(+num);
69
- }
70
- return false;
71
- }
72
- function promiseWithTimeout(promise, timeout, errMsg = "The promise timed out.") {
73
- let timeoutID;
74
- const timeOutPromise = new Promise(
75
- (_resolve, reject) => {
76
- timeoutID = setTimeout(() => reject(new Error(errMsg)), timeout);
77
- }
78
- );
79
- return Promise.race([promise, timeOutPromise]).finally(
80
- () => clearTimeout(timeoutID)
81
- );
82
- }
83
- async function catchPromise(promise) {
84
- try {
85
- return { result: await promise, err: null };
86
- } catch (e) {
87
- return { result: null, err: e };
88
- }
89
- }
90
- function doSafe(safe, onError) {
91
- var _a;
92
- if (!safe)
93
- return;
94
- try {
95
- const result = safe();
96
- if (isPromise(result)) {
97
- (_a = result["catch"]) == null ? void 0 : _a.call(
98
- result,
99
- (error) => {
100
- if (error instanceof Error) {
101
- onError == null ? void 0 : onError(error);
102
- } else {
103
- onError == null ? void 0 : onError(new Error(String(error)));
104
- }
105
- }
106
- );
107
- }
108
- } catch (error) {
109
- if (error instanceof Error) {
110
- onError == null ? void 0 : onError(error);
111
- } else {
112
- onError == null ? void 0 : onError(new Error(String(error)));
113
- }
114
- }
115
- }
116
- function isRunningOnServer() {
117
- return (globalThis == null ? void 0 : globalThis.process) !== void 0;
118
- }
119
- async function useServer(fn, onError) {
120
- if (isRunningOnServer()) {
121
- try {
122
- return await fn();
123
- } catch (e) {
124
- if (onError) {
125
- onError(e);
126
- } else {
127
- console.error("Error from useServer", e);
128
- }
129
- }
130
- }
131
- }
132
- function isPromise(value) {
133
- return Boolean(value && typeof value.then === "function");
134
- }
135
- export {
136
- Optional,
137
- catchPromise,
138
- doSafe,
139
- isNumber,
140
- isRunningOnServer,
141
- promiseRetry,
142
- promiseWithTimeout,
143
- sleep,
144
- useServer
145
- };
1
+ // src/promise-retry.ts
2
+ var defaultOptions = {
3
+ maxAttempts: 10,
4
+ retryDelay: 0,
5
+ onError: (err, attempt) => {
6
+ }
7
+ };
8
+ function promiseRetry(func, options = defaultOptions, attempt = 1) {
9
+ const config = { ...defaultOptions, ...options };
10
+ return func(attempt).catch(
11
+ (err) => {
12
+ config.onError(err, attempt);
13
+ if (attempt < config.maxAttempts) {
14
+ return new Promise(
15
+ (resolve) => {
16
+ setTimeout(
17
+ () => resolve(promiseRetry(func, options, attempt + 1)),
18
+ config.retryDelay
19
+ );
20
+ }
21
+ );
22
+ } else {
23
+ throw err;
24
+ }
25
+ }
26
+ );
27
+ }
28
+
29
+ // src/common-env.ts
30
+ var Optional = class _Optional {
31
+ constructor(value) {
32
+ this.value = value;
33
+ }
34
+ value = void 0;
35
+ orElseThrow(msg) {
36
+ if (this.value == void 0)
37
+ throw new Error(msg || "Value is empty");
38
+ return this.value;
39
+ }
40
+ static of(val) {
41
+ return new _Optional(val);
42
+ }
43
+ setValue(newValue) {
44
+ this.value = newValue;
45
+ return this;
46
+ }
47
+ getValue() {
48
+ return this.value;
49
+ }
50
+ ifPresent(action) {
51
+ if (this.value == void 0)
52
+ return;
53
+ try {
54
+ action(this.value);
55
+ } catch (e) {
56
+ console.error(e);
57
+ }
58
+ }
59
+ };
60
+ function sleep(ms) {
61
+ return new Promise((resolve) => setTimeout(() => resolve(""), ms));
62
+ }
63
+ function isNumber(num) {
64
+ if (typeof num === "number") {
65
+ return num - num === 0;
66
+ }
67
+ if (typeof num === "string" && num.trim() !== "") {
68
+ return Number.isFinite ? Number.isFinite(+num) : isFinite(+num);
69
+ }
70
+ return false;
71
+ }
72
+ function promiseWithTimeout(promise, timeout, errMsg = "The promise timed out.") {
73
+ let timeoutID;
74
+ const timeOutPromise = new Promise(
75
+ (_resolve, reject) => {
76
+ timeoutID = setTimeout(() => reject(new Error(errMsg)), timeout);
77
+ }
78
+ );
79
+ return Promise.race([promise, timeOutPromise]).finally(
80
+ () => clearTimeout(timeoutID)
81
+ );
82
+ }
83
+ async function catchPromise(promise) {
84
+ try {
85
+ return { result: await promise, err: null };
86
+ } catch (e) {
87
+ return { result: null, err: e };
88
+ }
89
+ }
90
+ function doSafe(safe, onError) {
91
+ var _a;
92
+ if (!safe)
93
+ return;
94
+ try {
95
+ const result = safe();
96
+ if (isPromise(result)) {
97
+ (_a = result["catch"]) == null ? void 0 : _a.call(
98
+ result,
99
+ (error) => {
100
+ if (error instanceof Error) {
101
+ onError == null ? void 0 : onError(error);
102
+ } else {
103
+ onError == null ? void 0 : onError(new Error(String(error)));
104
+ }
105
+ }
106
+ );
107
+ }
108
+ } catch (error) {
109
+ if (error instanceof Error) {
110
+ onError == null ? void 0 : onError(error);
111
+ } else {
112
+ onError == null ? void 0 : onError(new Error(String(error)));
113
+ }
114
+ }
115
+ }
116
+ function isRunningOnServer() {
117
+ return (globalThis == null ? void 0 : globalThis.process) !== void 0;
118
+ }
119
+ async function useServer(fn, onError) {
120
+ if (isRunningOnServer()) {
121
+ try {
122
+ return await fn();
123
+ } catch (e) {
124
+ if (onError) {
125
+ onError(e);
126
+ } else {
127
+ console.error("Error from useServer", e);
128
+ }
129
+ }
130
+ }
131
+ }
132
+ function isPromise(value) {
133
+ return Boolean(value && typeof value.then === "function");
134
+ }
135
+ export {
136
+ Optional,
137
+ catchPromise,
138
+ doSafe,
139
+ isNumber,
140
+ isRunningOnServer,
141
+ promiseRetry,
142
+ promiseWithTimeout,
143
+ sleep,
144
+ useServer
145
+ };