vivth 1.3.8 → 1.4.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.
package/README.md CHANGED
@@ -93,8 +93,11 @@ npm i vivth
93
93
  - [IsAsync](#isasync)
94
94
  - [LazyFactory](#lazyfactory)
95
95
  - [Timeout](#timeout)
96
- - [Try](#try)
96
+ - [Tries](#tries)
97
97
  - [TryAsync](#tryasync)
98
+ - [TryAsyncCall](#tryasynccall)
99
+ - [TryCall](#trycall)
100
+ - [TryNew](#trynew)
98
101
  - [TrySync](#trysync)
99
102
  - [TsToMjs](#tstomjs)
100
103
  - [AnyButUndefined](#anybutundefined)
@@ -3300,9 +3303,9 @@ test();
3300
3303
 
3301
3304
  \*) <sub>[go to list of exported API and typehelpers](#list-of-exported-api-and-typehelpers)</sub>
3302
3305
 
3303
- <h2 id="try">Try</h2>
3306
+ <h2 id="tries">Tries</h2>
3304
3307
 
3305
- #### reference:`Try`
3308
+ #### reference:`Tries`
3306
3309
 
3307
3310
  - function for error as value for chained operations;
3308
3311
  - utility function to brute force which key is able to run;
@@ -3329,9 +3332,9 @@ test();
3329
3332
  - <i>example</i>:
3330
3333
 
3331
3334
  ```js
3332
- import { Try } from "vivth";
3335
+ import { Tries } from "vivth";
3333
3336
 
3334
- const [[key, result], error] = await Try({
3337
+ const [[key, result], error] = await Tries({
3335
3338
  someRuntime: async ({ prevError }) => {
3336
3339
  // asuming on this one doesn't naturally throw error,
3337
3340
  // yet you need to continue to next key,
@@ -3396,6 +3399,100 @@ let [res, error] = await TryAsync(async () => {
3396
3399
 
3397
3400
  \*) <sub>[go to list of exported API and typehelpers](#list-of-exported-api-and-typehelpers)</sub>
3398
3401
 
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
+ <h2 id="trynew">TryNew</h2>
3467
+
3468
+ #### reference:`TryNew`
3469
+
3470
+ - function helper to turn unsafe constructor call of classReference into safe one without tryCatch block;
3471
+ - usefull to flatten your source code;
3472
+
3473
+ ```js
3474
+ /**
3475
+ * @template {new (...args: any[]) => any} CLASSREF
3476
+ * @param {CLASSREF} classReference
3477
+ * @returns {(...args: ConstructorParameters<CLASSREF>) =>
3478
+ * [InstanceType<CLASSREF>, undefined]|
3479
+ * [undefined, Error]}
3480
+ */
3481
+ ```
3482
+
3483
+ - <i>example</i>:
3484
+
3485
+ ```js
3486
+ import { TryNew } from "vivth";
3487
+
3488
+ const [instance, error] = TryNew(ClassReference)(...classConstructorParameters);
3489
+ if (!error) {
3490
+ // do something with instance
3491
+ }
3492
+ ```
3493
+
3494
+ \*) <sub>[go to list of exported API and typehelpers](#list-of-exported-api-and-typehelpers)</sub>
3495
+
3399
3496
  <h2 id="trysync">TrySync</h2>
3400
3497
 
3401
3498
  #### reference:`TrySync`
package/index.mjs CHANGED
@@ -46,8 +46,11 @@ export { GetRuntime } from './src/function/GetRuntime.mjs';
46
46
  export { IsAsync } from './src/function/IsAsync.mjs';
47
47
  export { LazyFactory } from './src/function/LazyFactory.mjs';
48
48
  export { Timeout } from './src/function/Timeout.mjs';
49
- export { Try } from './src/function/Try.mjs';
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
+ export { TryNew } from './src/function/TryNew.mjs';
51
54
  export { TrySync } from './src/function/TrySync.mjs';
52
55
  export { TsToMjs } from './src/function/TsToMjs.mjs';
53
56
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vivth",
3
- "version": "1.3.8",
3
+ "version": "1.4.0",
4
4
  "description": "library primitives",
5
5
  "main": "index.mjs",
6
6
  "types": "./types/index.d.mts",
@@ -2,7 +2,7 @@
2
2
 
3
3
  import { closeWorkerThreadEventObject } from '../common/eventObjects.mjs';
4
4
  import { GetRuntime } from '../function/GetRuntime.mjs';
5
- import { Try } from '../function/Try.mjs';
5
+ import { Tries } from '../function/Tries.mjs';
6
6
  import { TryAsync } from '../function/TryAsync.mjs';
7
7
  import { Console } from './Console.mjs';
8
8
  import { Derived } from './Derived.mjs';
@@ -179,7 +179,7 @@ export class WorkerMainThread {
179
179
  Console.error('invalid `Worker` inputed to `WorkerMainThread`;');
180
180
  return;
181
181
  }
182
- const [, errorCreatingWorker] = await Try({
182
+ const [, errorCreatingWorker] = await Tries({
183
183
  browser: async () => {
184
184
  if (runtime !== 'browser') {
185
185
  throw new Error('not a browser');
@@ -4,7 +4,7 @@ import { FSInline } from '../bundler/FSInline.mjs';
4
4
  import { Base64URL } from '../common/Base64URL.mjs';
5
5
  import { closeWorkerThreadEventObject } from '../common/eventObjects.mjs';
6
6
  import { GetRuntime } from '../function/GetRuntime.mjs';
7
- import { Try } from '../function/Try.mjs';
7
+ import { Tries } from '../function/Tries.mjs';
8
8
  import { Console } from './Console.mjs';
9
9
  import { Derived } from './Derived.mjs';
10
10
  import { Effect } from './Effect.mjs';
@@ -111,7 +111,7 @@ export class WorkerMainThread {
111
111
  Console.error('invalid `Worker` inputed to `WorkerMainThread`;');
112
112
  return;
113
113
  }
114
- const [, errorCreatingWorker] = await Try({
114
+ const [, errorCreatingWorker] = await Tries({
115
115
  browser: async () => {
116
116
  if (runtime !== 'browser') {
117
117
  throw new Error('not a browser');
@@ -2,7 +2,7 @@
2
2
 
3
3
  import { closeWorkerThreadEventObject } from '../common/eventObjects.mjs';
4
4
  import { EventCheck } from '../function/EventCheck.mjs';
5
- import { Try } from '../function/Try.mjs';
5
+ import { Tries } from '../function/Tries.mjs';
6
6
  import { TryAsync } from '../function/TryAsync.mjs';
7
7
  import { Console } from './Console.mjs';
8
8
  import { QChannel } from './QChannel.mjs';
@@ -75,7 +75,7 @@ export class WorkerThread {
75
75
  constructor(handler) {
76
76
  this.handler = handler;
77
77
  const this_ = this;
78
- Try({
78
+ Tries({
79
79
  post: async () => {
80
80
  /**
81
81
  * @param {MessageEvent<RECEIVE>|RECEIVE} ev
@@ -21,9 +21,9 @@ import { TryAsync } from './TryAsync.mjs';
21
21
  * | [[undefined, undefined], Error|undefined]
22
22
  * >}
23
23
  * @example
24
- * import { Try } from 'vivth';
24
+ * import { Tries } from 'vivth';
25
25
  *
26
- * const [[key, result], error] = await Try({
26
+ * const [[key, result], error] = await Tries({
27
27
  * someRuntime: async ({ prevError }) => {
28
28
  * // asuming on this one doesn't naturally throw error,
29
29
  * // yet you need to continue to next key,
@@ -51,7 +51,7 @@ import { TryAsync } from './TryAsync.mjs';
51
51
  * },
52
52
  * });
53
53
  */
54
- export async function Try(tryRecord) {
54
+ export async function Tries(tryRecord) {
55
55
  /**
56
56
  * @type {Error|undefined}
57
57
  */
@@ -0,0 +1,31 @@
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
+ }
@@ -0,0 +1,29 @@
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
+ }
@@ -0,0 +1,29 @@
1
+ // @ts-check
2
+
3
+ /**
4
+ * @description
5
+ * - function helper to turn unsafe constructor call of classReference into safe one without tryCatch block;
6
+ * - usefull to flatten your source code;
7
+ * @template {new (...args: any[]) => any} CLASSREF
8
+ * @param {CLASSREF} classReference
9
+ * @returns {(...args: ConstructorParameters<CLASSREF>) =>
10
+ * [InstanceType<CLASSREF>, undefined]|
11
+ * [undefined, Error]}
12
+ * @example
13
+ * import { TryNew } from 'vivth';
14
+ *
15
+ * const [instance, error] = TryNew(ClassReference)(...classConstructorParameters)
16
+ * if(!error) {
17
+ * // do something with instance
18
+ * }
19
+ */
20
+ export function TryNew(classReference) {
21
+ // @ts-expect-error
22
+ return (...params) => {
23
+ try {
24
+ return [new classReference(...params), undefined];
25
+ } catch (err) {
26
+ return [undefined, err];
27
+ }
28
+ };
29
+ }
package/types/index.d.mts CHANGED
@@ -38,8 +38,11 @@ export { GetRuntime } from "./src/function/GetRuntime.mjs";
38
38
  export { IsAsync } from "./src/function/IsAsync.mjs";
39
39
  export { LazyFactory } from "./src/function/LazyFactory.mjs";
40
40
  export { Timeout } from "./src/function/Timeout.mjs";
41
- export { Try } from "./src/function/Try.mjs";
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
+ export { TryNew } from "./src/function/TryNew.mjs";
43
46
  export { TrySync } from "./src/function/TrySync.mjs";
44
47
  export { TsToMjs } from "./src/function/TsToMjs.mjs";
45
48
  export type AnyButUndefined = import("./src/types/AnyButUndefined.mjs").AnyButUndefined;
@@ -17,9 +17,9 @@
17
17
  * | [[undefined, undefined], Error|undefined]
18
18
  * >}
19
19
  * @example
20
- * import { Try } from 'vivth';
20
+ * import { Tries } from 'vivth';
21
21
  *
22
- * const [[key, result], error] = await Try({
22
+ * const [[key, result], error] = await Tries({
23
23
  * someRuntime: async ({ prevError }) => {
24
24
  * // asuming on this one doesn't naturally throw error,
25
25
  * // yet you need to continue to next key,
@@ -47,6 +47,6 @@
47
47
  * },
48
48
  * });
49
49
  */
50
- export function Try<KEY extends string, RETURNTYPE, RecordTryType extends Record<KEY, (err: {
50
+ export function Tries<KEY extends string, RETURNTYPE, RecordTryType extends Record<KEY, (err: {
51
51
  prevError: undefined | Error;
52
52
  }) => Promise<RETURNTYPE>>>(tryRecord: RecordTryType): Promise<[[keyof RecordTryType, RETURNTYPE], undefined] | [[undefined, undefined], Error | undefined]>;
@@ -0,0 +1,20 @@
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]>;
@@ -0,0 +1,18 @@
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];
@@ -0,0 +1,18 @@
1
+ /**
2
+ * @description
3
+ * - function helper to turn unsafe constructor call of classReference into safe one without tryCatch block;
4
+ * - usefull to flatten your source code;
5
+ * @template {new (...args: any[]) => any} CLASSREF
6
+ * @param {CLASSREF} classReference
7
+ * @returns {(...args: ConstructorParameters<CLASSREF>) =>
8
+ * [InstanceType<CLASSREF>, undefined]|
9
+ * [undefined, Error]}
10
+ * @example
11
+ * import { TryNew } from 'vivth';
12
+ *
13
+ * const [instance, error] = TryNew(ClassReference)(...classConstructorParameters)
14
+ * if(!error) {
15
+ * // do something with instance
16
+ * }
17
+ */
18
+ export function TryNew<CLASSREF extends new (...args: any[]) => any>(classReference: CLASSREF): (...args: ConstructorParameters<CLASSREF>) => [InstanceType<CLASSREF>, undefined] | [undefined, Error];