vivth 1.4.2 → 1.4.3
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/README.md +1 -69
- package/index.mjs +0 -2
- package/package.json +1 -1
- package/src/function/TryAsync.mjs +3 -2
- package/src/function/TryNew.mjs +4 -3
- package/src/function/TrySync.mjs +4 -3
- package/src/function/resolveErrorArray.mjs +9 -0
- package/types/index.d.mts +0 -2
- package/types/src/function/TrySync.d.mts +1 -1
- package/types/src/function/resolveErrorArray.d.mts +1 -0
- package/src/function/TryAsyncCall.mjs +0 -30
- package/src/function/TryCall.mjs +0 -27
- package/types/src/function/TryAsyncCall.d.mts +0 -21
- package/types/src/function/TryCall.d.mts +0 -18
package/README.md
CHANGED
|
@@ -95,8 +95,6 @@ npm i vivth
|
|
|
95
95
|
- [Timeout](#timeout)
|
|
96
96
|
- [Tries](#tries)
|
|
97
97
|
- [TryAsync](#tryasync)
|
|
98
|
-
- [TryAsyncCall](#tryasynccall)
|
|
99
|
-
- [TryCall](#trycall)
|
|
100
98
|
- [TryNew](#trynew)
|
|
101
99
|
- [TrySync](#trysync)
|
|
102
100
|
- [TsToMjs](#tstomjs)
|
|
@@ -3399,72 +3397,6 @@ let [res, error] = await TryAsync(async () => {
|
|
|
3399
3397
|
|
|
3400
3398
|
\*) <sub>[go to list of exported API and typehelpers](#list-of-exported-api-and-typehelpers)</sub>
|
|
3401
3399
|
|
|
3402
|
-
<h2 id="tryasynccall">TryAsyncCall</h2>
|
|
3403
|
-
|
|
3404
|
-
#### reference:`TryAsyncCall`
|
|
3405
|
-
|
|
3406
|
-
- function helper to turn unsafe callback into safe one without tryCatch block;
|
|
3407
|
-
- usefull to flatten your source code;
|
|
3408
|
-
|
|
3409
|
-
```js
|
|
3410
|
-
/**
|
|
3411
|
-
* @template {(...param:any[])=>Promise<any>} UNSAFEASYNCCALLBACK
|
|
3412
|
-
* @param {UNSAFEASYNCCALLBACK} unsafeAsyncCallback
|
|
3413
|
-
* @param {Parameters<UNSAFEASYNCCALLBACK>} params
|
|
3414
|
-
* @returns {Promise<
|
|
3415
|
-
* [Awaited<ReturnType<UNSAFEASYNCCALLBACK>>,undefined]|
|
|
3416
|
-
* [undefined,Error]>}
|
|
3417
|
-
*/
|
|
3418
|
-
```
|
|
3419
|
-
|
|
3420
|
-
- <i>example</i>:
|
|
3421
|
-
|
|
3422
|
-
```js
|
|
3423
|
-
import { TryAsyncCall } from "vivth";
|
|
3424
|
-
|
|
3425
|
-
(async () => {
|
|
3426
|
-
const [result, error] = await TryAsyncCall(
|
|
3427
|
-
unsafeAsyncCallback,
|
|
3428
|
-
...unsafeAsyncCallbackParameters,
|
|
3429
|
-
);
|
|
3430
|
-
if (!error) {
|
|
3431
|
-
// do something with result safely;
|
|
3432
|
-
}
|
|
3433
|
-
})();
|
|
3434
|
-
```
|
|
3435
|
-
|
|
3436
|
-
\*) <sub>[go to list of exported API and typehelpers](#list-of-exported-api-and-typehelpers)</sub>
|
|
3437
|
-
|
|
3438
|
-
<h2 id="trycall">TryCall</h2>
|
|
3439
|
-
|
|
3440
|
-
#### reference:`TryCall`
|
|
3441
|
-
|
|
3442
|
-
- function helper to turn unsafe callback into safe one without tryCatch block;
|
|
3443
|
-
- usefull to flatten your source code;
|
|
3444
|
-
|
|
3445
|
-
```js
|
|
3446
|
-
/**
|
|
3447
|
-
* @template {(...param:any[])=>any} UNSAFECALLBACK
|
|
3448
|
-
* @param {UNSAFECALLBACK} unsafeCallback
|
|
3449
|
-
* @param {Parameters<UNSAFECALLBACK>} params
|
|
3450
|
-
* @returns {[ReturnType<UNSAFECALLBACK>,undefined]|
|
|
3451
|
-
* [undefined, Error]}
|
|
3452
|
-
*/
|
|
3453
|
-
```
|
|
3454
|
-
|
|
3455
|
-
- <i>example</i>:
|
|
3456
|
-
|
|
3457
|
-
```js
|
|
3458
|
-
import { TryCall } from "vivth";
|
|
3459
|
-
|
|
3460
|
-
const [result, error] = TryCall(unsafeCallback, ...unsafeCallbackParameters);
|
|
3461
|
-
if (!error) {
|
|
3462
|
-
// do something with result safely;
|
|
3463
|
-
}
|
|
3464
|
-
```
|
|
3465
|
-
|
|
3466
|
-
\*) <sub>[go to list of exported API and typehelpers](#list-of-exported-api-and-typehelpers)</sub>
|
|
3467
|
-
|
|
3468
3400
|
<h2 id="trynew">TryNew</h2>
|
|
3469
3401
|
|
|
3470
3402
|
#### reference:`TryNew`
|
|
@@ -3514,7 +3446,7 @@ if (!error) {
|
|
|
3514
3446
|
- <i>example</i>:
|
|
3515
3447
|
|
|
3516
3448
|
```js
|
|
3517
|
-
import { readFileSync } from "fs";
|
|
3449
|
+
import { readFileSync } from "node:fs";
|
|
3518
3450
|
import { TrySync } from "./yourModule.js";
|
|
3519
3451
|
|
|
3520
3452
|
const [data, error] = TrySync(() => {
|
package/index.mjs
CHANGED
|
@@ -48,8 +48,6 @@ export { LazyFactory } from './src/function/LazyFactory.mjs';
|
|
|
48
48
|
export { Timeout } from './src/function/Timeout.mjs';
|
|
49
49
|
export { Tries } from './src/function/Tries.mjs';
|
|
50
50
|
export { TryAsync } from './src/function/TryAsync.mjs';
|
|
51
|
-
export { TryAsyncCall } from './src/function/TryAsyncCall.mjs';
|
|
52
|
-
export { TryCall } from './src/function/TryCall.mjs';
|
|
53
51
|
export { TryNew } from './src/function/TryNew.mjs';
|
|
54
52
|
export { TrySync } from './src/function/TrySync.mjs';
|
|
55
53
|
export { TsToMjs } from './src/function/TsToMjs.mjs';
|
package/package.json
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
// @ts-check
|
|
2
2
|
|
|
3
|
+
import { resolveErrorArray } from './resolveErrorArray.mjs';
|
|
4
|
+
|
|
3
5
|
/**
|
|
4
6
|
* @description
|
|
5
7
|
* - function for error as value for asynchronous operation;
|
|
@@ -26,7 +28,6 @@ export async function TryAsync(asyncFunction_) {
|
|
|
26
28
|
const result = await asyncFunction_();
|
|
27
29
|
return [result, undefined];
|
|
28
30
|
} catch (error) {
|
|
29
|
-
|
|
30
|
-
return [undefined, error];
|
|
31
|
+
return resolveErrorArray(error);
|
|
31
32
|
}
|
|
32
33
|
}
|
package/src/function/TryNew.mjs
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
// @ts-check
|
|
2
2
|
|
|
3
|
+
import { resolveErrorArray } from './resolveErrorArray.mjs';
|
|
4
|
+
|
|
3
5
|
/**
|
|
4
6
|
* @description
|
|
5
7
|
* - function helper to turn unsafe constructor call of classReference into safe one without tryCatch block;
|
|
@@ -20,8 +22,7 @@
|
|
|
20
22
|
export function TryNew(classReference, ...params) {
|
|
21
23
|
try {
|
|
22
24
|
return [new classReference(...params), undefined];
|
|
23
|
-
} catch (
|
|
24
|
-
|
|
25
|
-
return [undefined, err];
|
|
25
|
+
} catch (error) {
|
|
26
|
+
return resolveErrorArray(error);
|
|
26
27
|
}
|
|
27
28
|
}
|
package/src/function/TrySync.mjs
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
// @ts-check
|
|
2
2
|
|
|
3
|
+
import { resolveErrorArray } from './resolveErrorArray.mjs';
|
|
4
|
+
|
|
3
5
|
/**
|
|
4
6
|
* @description
|
|
5
7
|
* - function for error as value for synchronous operation;
|
|
@@ -9,7 +11,7 @@
|
|
|
9
11
|
* @returns {[RESULT,undefined]|
|
|
10
12
|
* [undefined,Error]}
|
|
11
13
|
* @example
|
|
12
|
-
* import { readFileSync } from 'fs';
|
|
14
|
+
* import { readFileSync } from 'node:fs';
|
|
13
15
|
* import { TrySync } from './yourModule.js';
|
|
14
16
|
*
|
|
15
17
|
* const [data, error] = TrySync(() => {
|
|
@@ -21,7 +23,6 @@ export function TrySync(function_) {
|
|
|
21
23
|
const result = function_();
|
|
22
24
|
return [result, undefined];
|
|
23
25
|
} catch (error) {
|
|
24
|
-
|
|
25
|
-
return [undefined, error];
|
|
26
|
+
return resolveErrorArray(error);
|
|
26
27
|
}
|
|
27
28
|
}
|
package/types/index.d.mts
CHANGED
|
@@ -40,8 +40,6 @@ export { LazyFactory } from "./src/function/LazyFactory.mjs";
|
|
|
40
40
|
export { Timeout } from "./src/function/Timeout.mjs";
|
|
41
41
|
export { Tries } from "./src/function/Tries.mjs";
|
|
42
42
|
export { TryAsync } from "./src/function/TryAsync.mjs";
|
|
43
|
-
export { TryAsyncCall } from "./src/function/TryAsyncCall.mjs";
|
|
44
|
-
export { TryCall } from "./src/function/TryCall.mjs";
|
|
45
43
|
export { TryNew } from "./src/function/TryNew.mjs";
|
|
46
44
|
export { TrySync } from "./src/function/TrySync.mjs";
|
|
47
45
|
export { TsToMjs } from "./src/function/TsToMjs.mjs";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export function resolveErrorArray(unknown: unknown): [undefined, Error];
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
// @ts-check
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* @description
|
|
5
|
-
* - function helper to turn unsafe callback into safe one without tryCatch block;
|
|
6
|
-
* - usefull to flatten your source code;
|
|
7
|
-
* @template {(...param:any[])=>Promise<any>} UNSAFEASYNCCALLBACK
|
|
8
|
-
* @param {UNSAFEASYNCCALLBACK} unsafeAsyncCallback
|
|
9
|
-
* @param {Parameters<UNSAFEASYNCCALLBACK>} params
|
|
10
|
-
* @returns {Promise<
|
|
11
|
-
* [Awaited<ReturnType<UNSAFEASYNCCALLBACK>>,undefined]|
|
|
12
|
-
* [undefined,Error]>}
|
|
13
|
-
* @example
|
|
14
|
-
* import { TryAsyncCall } from 'vivth';
|
|
15
|
-
*
|
|
16
|
-
* (async() => {
|
|
17
|
-
* const [result, error] = await TryAsyncCall(unsafeAsyncCallback, ...unsafeAsyncCallbackParameters);
|
|
18
|
-
* if (!error) {
|
|
19
|
-
* // do something with result safely;
|
|
20
|
-
* }
|
|
21
|
-
* })()
|
|
22
|
-
*/
|
|
23
|
-
export async function TryAsyncCall(unsafeAsyncCallback, ...params) {
|
|
24
|
-
try {
|
|
25
|
-
return [await unsafeAsyncCallback(...params), undefined];
|
|
26
|
-
} catch (err) {
|
|
27
|
-
// @ts-expect-error
|
|
28
|
-
return [undefined, err];
|
|
29
|
-
}
|
|
30
|
-
}
|
package/src/function/TryCall.mjs
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
// @ts-check
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* @description
|
|
5
|
-
* - function helper to turn unsafe callback into safe one without tryCatch block;
|
|
6
|
-
* - usefull to flatten your source code;
|
|
7
|
-
* @template {(...param:any[])=>any} UNSAFECALLBACK
|
|
8
|
-
* @param {UNSAFECALLBACK} unsafeCallback
|
|
9
|
-
* @param {Parameters<UNSAFECALLBACK>} params
|
|
10
|
-
* @returns {[ReturnType<UNSAFECALLBACK>,undefined]|
|
|
11
|
-
* [undefined, Error]}
|
|
12
|
-
* @example
|
|
13
|
-
* import { TryCall } from 'vivth';
|
|
14
|
-
*
|
|
15
|
-
* const [result, error] = TryCall(unsafeCallback, ...unsafeCallbackParameters);
|
|
16
|
-
* if (!error) {
|
|
17
|
-
* // do something with result safely;
|
|
18
|
-
* }
|
|
19
|
-
*/
|
|
20
|
-
export function TryCall(unsafeCallback, ...params) {
|
|
21
|
-
try {
|
|
22
|
-
return [unsafeCallback(...params), undefined];
|
|
23
|
-
} catch (err) {
|
|
24
|
-
// @ts-expect-error
|
|
25
|
-
return [undefined, err];
|
|
26
|
-
}
|
|
27
|
-
}
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @description
|
|
3
|
-
* - function helper to turn unsafe callback into safe one without tryCatch block;
|
|
4
|
-
* - usefull to flatten your source code;
|
|
5
|
-
* @template {(...param:any[])=>Promise<any>} UNSAFEASYNCCALLBACK
|
|
6
|
-
* @param {UNSAFEASYNCCALLBACK} unsafeAsyncCallback
|
|
7
|
-
* @param {Parameters<UNSAFEASYNCCALLBACK>} params
|
|
8
|
-
* @returns {Promise<
|
|
9
|
-
* [Awaited<ReturnType<UNSAFEASYNCCALLBACK>>,undefined]|
|
|
10
|
-
* [undefined,Error]>}
|
|
11
|
-
* @example
|
|
12
|
-
* import { TryAsyncCall } from 'vivth';
|
|
13
|
-
*
|
|
14
|
-
* (async() => {
|
|
15
|
-
* const [result, error] = await TryAsyncCall(unsafeAsyncCallback, ...unsafeAsyncCallbackParameters);
|
|
16
|
-
* if (!error) {
|
|
17
|
-
* // do something with result safely;
|
|
18
|
-
* }
|
|
19
|
-
* })()
|
|
20
|
-
*/
|
|
21
|
-
export function TryAsyncCall<UNSAFEASYNCCALLBACK extends (...param: any[]) => Promise<any>>(unsafeAsyncCallback: UNSAFEASYNCCALLBACK, ...params: Parameters<UNSAFEASYNCCALLBACK>): Promise<[Awaited<ReturnType<UNSAFEASYNCCALLBACK>>, undefined] | [undefined, Error]>;
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @description
|
|
3
|
-
* - function helper to turn unsafe callback into safe one without tryCatch block;
|
|
4
|
-
* - usefull to flatten your source code;
|
|
5
|
-
* @template {(...param:any[])=>any} UNSAFECALLBACK
|
|
6
|
-
* @param {UNSAFECALLBACK} unsafeCallback
|
|
7
|
-
* @param {Parameters<UNSAFECALLBACK>} params
|
|
8
|
-
* @returns {[ReturnType<UNSAFECALLBACK>,undefined]|
|
|
9
|
-
* [undefined, Error]}
|
|
10
|
-
* @example
|
|
11
|
-
* import { TryCall } from 'vivth';
|
|
12
|
-
*
|
|
13
|
-
* const [result, error] = TryCall(unsafeCallback, ...unsafeCallbackParameters);
|
|
14
|
-
* if (!error) {
|
|
15
|
-
* // do something with result safely;
|
|
16
|
-
* }
|
|
17
|
-
*/
|
|
18
|
-
export function TryCall<UNSAFECALLBACK extends (...param: any[]) => any>(unsafeCallback: UNSAFECALLBACK, ...params: Parameters<UNSAFECALLBACK>): [ReturnType<UNSAFECALLBACK>, undefined] | [undefined, Error];
|