vivth 1.4.1 → 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 +5 -71
- package/index.mjs +0 -2
- package/package.json +1 -1
- package/src/function/TryAsync.mjs +3 -2
- package/src/function/TryNew.mjs +12 -13
- 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/TryNew.d.mts +5 -5
- package/types/src/function/TrySync.d.mts +1 -1
- package/types/src/function/resolveErrorArray.d.mts +1 -0
- package/src/function/TryAsyncCall.mjs +0 -31
- package/src/function/TryCall.mjs +0 -29
- package/types/src/function/TryAsyncCall.d.mts +0 -20
- 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,70 +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
|
-
* @returns {(...param:Parameters<UNSAFEASYNCCALLBACK>)=> Promise<
|
|
3414
|
-
* [Awaited<ReturnType<UNSAFEASYNCCALLBACK>>,undefined]|
|
|
3415
|
-
* [undefined,Error]>}
|
|
3416
|
-
*/
|
|
3417
|
-
```
|
|
3418
|
-
|
|
3419
|
-
- <i>example</i>:
|
|
3420
|
-
|
|
3421
|
-
```js
|
|
3422
|
-
import { TryAsyncCall } from "vivth";
|
|
3423
|
-
|
|
3424
|
-
(async () => {
|
|
3425
|
-
const [result, error] = await TryAsyncCall(unsafeAsyncCallback)(
|
|
3426
|
-
...unsafeAsyncCallbackParameters,
|
|
3427
|
-
);
|
|
3428
|
-
if (!error) {
|
|
3429
|
-
// do something with result
|
|
3430
|
-
}
|
|
3431
|
-
})();
|
|
3432
|
-
```
|
|
3433
|
-
|
|
3434
|
-
\*) <sub>[go to list of exported API and typehelpers](#list-of-exported-api-and-typehelpers)</sub>
|
|
3435
|
-
|
|
3436
|
-
<h2 id="trycall">TryCall</h2>
|
|
3437
|
-
|
|
3438
|
-
#### reference:`TryCall`
|
|
3439
|
-
|
|
3440
|
-
- function helper to turn unsafe callback into safe one without tryCatch block;
|
|
3441
|
-
- usefull to flatten your source code;
|
|
3442
|
-
|
|
3443
|
-
```js
|
|
3444
|
-
/**
|
|
3445
|
-
* @template {(...param:any[])=>any} UNSAFECALLBACK
|
|
3446
|
-
* @param {UNSAFECALLBACK} unsafeCallback
|
|
3447
|
-
* @returns {(...param:Parameters<UNSAFECALLBACK>)=>
|
|
3448
|
-
* [ReturnType<UNSAFECALLBACK>,undefined]|
|
|
3449
|
-
* [undefined,Error]}
|
|
3450
|
-
*/
|
|
3451
|
-
```
|
|
3452
|
-
|
|
3453
|
-
- <i>example</i>:
|
|
3454
|
-
|
|
3455
|
-
```js
|
|
3456
|
-
import { TryCall } from "vivth";
|
|
3457
|
-
|
|
3458
|
-
const [result, error] = TryCall(unsafeCallback)(...unsafeCallbackParameters);
|
|
3459
|
-
if (!error) {
|
|
3460
|
-
// do something with result
|
|
3461
|
-
}
|
|
3462
|
-
```
|
|
3463
|
-
|
|
3464
|
-
\*) <sub>[go to list of exported API and typehelpers](#list-of-exported-api-and-typehelpers)</sub>
|
|
3465
|
-
|
|
3466
3400
|
<h2 id="trynew">TryNew</h2>
|
|
3467
3401
|
|
|
3468
3402
|
#### reference:`TryNew`
|
|
@@ -3474,8 +3408,8 @@ if (!error) {
|
|
|
3474
3408
|
/**
|
|
3475
3409
|
* @template {new (...args: any[]) => any} CLASSREF
|
|
3476
3410
|
* @param {CLASSREF} classReference
|
|
3477
|
-
* @
|
|
3478
|
-
* [InstanceType<CLASSREF>, undefined]|
|
|
3411
|
+
* @param {ConstructorParameters<CLASSREF>} params
|
|
3412
|
+
* @returns {[InstanceType<CLASSREF>, undefined]|
|
|
3479
3413
|
* [undefined, Error]}
|
|
3480
3414
|
*/
|
|
3481
3415
|
```
|
|
@@ -3485,9 +3419,9 @@ if (!error) {
|
|
|
3485
3419
|
```js
|
|
3486
3420
|
import { TryNew } from "vivth";
|
|
3487
3421
|
|
|
3488
|
-
const [instance, error] = TryNew(ClassReference
|
|
3422
|
+
const [instance, error] = TryNew(ClassReference, ...classConstructorParameters);
|
|
3489
3423
|
if (!error) {
|
|
3490
|
-
// do something with instance
|
|
3424
|
+
// do something with instance safely;
|
|
3491
3425
|
}
|
|
3492
3426
|
```
|
|
3493
3427
|
|
|
@@ -3512,7 +3446,7 @@ if (!error) {
|
|
|
3512
3446
|
- <i>example</i>:
|
|
3513
3447
|
|
|
3514
3448
|
```js
|
|
3515
|
-
import { readFileSync } from "fs";
|
|
3449
|
+
import { readFileSync } from "node:fs";
|
|
3516
3450
|
import { TrySync } from "./yourModule.js";
|
|
3517
3451
|
|
|
3518
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,29 +1,28 @@
|
|
|
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;
|
|
6
8
|
* - usefull to flatten your source code;
|
|
7
9
|
* @template {new (...args: any[]) => any} CLASSREF
|
|
8
10
|
* @param {CLASSREF} classReference
|
|
9
|
-
* @
|
|
10
|
-
* [InstanceType<CLASSREF>, undefined]|
|
|
11
|
+
* @param {ConstructorParameters<CLASSREF>} params
|
|
12
|
+
* @returns {[InstanceType<CLASSREF>, undefined]|
|
|
11
13
|
* [undefined, Error]}
|
|
12
14
|
* @example
|
|
13
15
|
* import { TryNew } from 'vivth';
|
|
14
16
|
*
|
|
15
|
-
* const [instance, error] = TryNew(ClassReference
|
|
17
|
+
* const [instance, error] = TryNew(ClassReference, ...classConstructorParameters)
|
|
16
18
|
* if(!error) {
|
|
17
|
-
*
|
|
19
|
+
* // do something with instance safely;
|
|
18
20
|
* }
|
|
19
21
|
*/
|
|
20
|
-
export function TryNew(classReference) {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
return [undefined, err];
|
|
27
|
-
}
|
|
28
|
-
};
|
|
22
|
+
export function TryNew(classReference, ...params) {
|
|
23
|
+
try {
|
|
24
|
+
return [new classReference(...params), undefined];
|
|
25
|
+
} catch (error) {
|
|
26
|
+
return resolveErrorArray(error);
|
|
27
|
+
}
|
|
29
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";
|
|
@@ -4,15 +4,15 @@
|
|
|
4
4
|
* - usefull to flatten your source code;
|
|
5
5
|
* @template {new (...args: any[]) => any} CLASSREF
|
|
6
6
|
* @param {CLASSREF} classReference
|
|
7
|
-
* @
|
|
8
|
-
* [InstanceType<CLASSREF>, undefined]|
|
|
7
|
+
* @param {ConstructorParameters<CLASSREF>} params
|
|
8
|
+
* @returns {[InstanceType<CLASSREF>, undefined]|
|
|
9
9
|
* [undefined, Error]}
|
|
10
10
|
* @example
|
|
11
11
|
* import { TryNew } from 'vivth';
|
|
12
12
|
*
|
|
13
|
-
* const [instance, error] = TryNew(ClassReference
|
|
13
|
+
* const [instance, error] = TryNew(ClassReference, ...classConstructorParameters)
|
|
14
14
|
* if(!error) {
|
|
15
|
-
*
|
|
15
|
+
* // do something with instance safely;
|
|
16
16
|
* }
|
|
17
17
|
*/
|
|
18
|
-
export function TryNew<CLASSREF extends new (...args: any[]) => any>(classReference: CLASSREF
|
|
18
|
+
export function TryNew<CLASSREF extends new (...args: any[]) => any>(classReference: CLASSREF, ...params: ConstructorParameters<CLASSREF>): [InstanceType<CLASSREF>, undefined] | [undefined, Error];
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export function resolveErrorArray(unknown: unknown): [undefined, Error];
|
|
@@ -1,31 +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
|
-
* @returns {(...param:Parameters<UNSAFEASYNCCALLBACK>)=> Promise<
|
|
10
|
-
* [Awaited<ReturnType<UNSAFEASYNCCALLBACK>>,undefined]|
|
|
11
|
-
* [undefined,Error]>}
|
|
12
|
-
* @example
|
|
13
|
-
* import { TryAsyncCall } from 'vivth';
|
|
14
|
-
*
|
|
15
|
-
* (async() => {
|
|
16
|
-
* const [result, error] = await TryAsyncCall(unsafeAsyncCallback)(...unsafeAsyncCallbackParameters);
|
|
17
|
-
* if (!error) {
|
|
18
|
-
* // do something with result
|
|
19
|
-
* }
|
|
20
|
-
* })()
|
|
21
|
-
*/
|
|
22
|
-
export function TryAsyncCall(unsafeAsyncCallback) {
|
|
23
|
-
// @ts-expect-error
|
|
24
|
-
return async (...params) => {
|
|
25
|
-
try {
|
|
26
|
-
return [await unsafeAsyncCallback(...params), undefined];
|
|
27
|
-
} catch (err) {
|
|
28
|
-
return [undefined, err];
|
|
29
|
-
}
|
|
30
|
-
};
|
|
31
|
-
}
|
package/src/function/TryCall.mjs
DELETED
|
@@ -1,29 +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
|
-
* @returns {(...param:Parameters<UNSAFECALLBACK>)=>
|
|
10
|
-
* [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
|
|
18
|
-
* }
|
|
19
|
-
*/
|
|
20
|
-
export function TryCall(unsafeCallback) {
|
|
21
|
-
// @ts-expect-error
|
|
22
|
-
return (...params) => {
|
|
23
|
-
try {
|
|
24
|
-
return [unsafeCallback(...params), undefined];
|
|
25
|
-
} catch (err) {
|
|
26
|
-
return [undefined, err];
|
|
27
|
-
}
|
|
28
|
-
};
|
|
29
|
-
}
|
|
@@ -1,20 +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
|
-
* @returns {(...param:Parameters<UNSAFEASYNCCALLBACK>)=> Promise<
|
|
8
|
-
* [Awaited<ReturnType<UNSAFEASYNCCALLBACK>>,undefined]|
|
|
9
|
-
* [undefined,Error]>}
|
|
10
|
-
* @example
|
|
11
|
-
* import { TryAsyncCall } from 'vivth';
|
|
12
|
-
*
|
|
13
|
-
* (async() => {
|
|
14
|
-
* const [result, error] = await TryAsyncCall(unsafeAsyncCallback)(...unsafeAsyncCallbackParameters);
|
|
15
|
-
* if (!error) {
|
|
16
|
-
* // do something with result
|
|
17
|
-
* }
|
|
18
|
-
* })()
|
|
19
|
-
*/
|
|
20
|
-
export function TryAsyncCall<UNSAFEASYNCCALLBACK extends (...param: any[]) => Promise<any>>(unsafeAsyncCallback: UNSAFEASYNCCALLBACK): (...param: 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
|
-
* @returns {(...param:Parameters<UNSAFECALLBACK>)=>
|
|
8
|
-
* [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
|
|
16
|
-
* }
|
|
17
|
-
*/
|
|
18
|
-
export function TryCall<UNSAFECALLBACK extends (...param: any[]) => any>(unsafeCallback: UNSAFECALLBACK): (...param: Parameters<UNSAFECALLBACK>) => [ReturnType<UNSAFECALLBACK>, undefined] | [undefined, Error];
|