vivth 1.4.2 → 1.4.4

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
@@ -92,11 +92,10 @@ npm i vivth
92
92
  - [GetRuntime](#getruntime)
93
93
  - [IsAsync](#isasync)
94
94
  - [LazyFactory](#lazyfactory)
95
+ - [TemplateLiteral](#templateliteral)
95
96
  - [Timeout](#timeout)
96
97
  - [Tries](#tries)
97
98
  - [TryAsync](#tryasync)
98
- - [TryAsyncCall](#tryasynccall)
99
- - [TryCall](#trycall)
100
99
  - [TryNew](#trynew)
101
100
  - [TrySync](#trysync)
102
101
  - [TsToMjs](#tstomjs)
@@ -3274,6 +3273,41 @@ myInstance["vivth:unwrapLazy;"](); // forcefully call factory generator;
3274
3273
 
3275
3274
  \*) <sub>[go to list of exported API and typehelpers](#list-of-exported-api-and-typehelpers)</sub>
3276
3275
 
3276
+ <h2 id="templateliteral">TemplateLiteral</h2>
3277
+
3278
+ #### reference:`TemplateLiteral`
3279
+
3280
+ - function helper to create template literal with VALUEhandler to handle each values;
3281
+
3282
+ ```js
3283
+ /**
3284
+ * @template {(input:any)=>string|Promise<string>} VALUEHANDLER
3285
+ * @param {VALUEHANDLER} valueHandler
3286
+ * @param {(result:string)=>string} [postProcess]
3287
+ * @returns {(strings:TemplateStringsArray,
3288
+ * ...values:(Parameters<VALUEHANDLER>[0])[])=>
3289
+ * ReturnType<VALUEHANDLER>}
3290
+ */
3291
+ ```
3292
+
3293
+ - <i>example</i>:
3294
+
3295
+ ```js
3296
+ import { TemplateLiteral } form 'vivth';
3297
+
3298
+ export const html = TemplateLiteral(
3299
+ (val) => val,
3300
+ // optional
3301
+ (res) => return window.body.innerHTML = res
3302
+ );
3303
+
3304
+ html`<div>${`<button>innerButton</button>`}</div>`;
3305
+ // this will set innerHTML of body to '<div><button>innerButton</button></div>'
3306
+
3307
+ ```
3308
+
3309
+ \*) <sub>[go to list of exported API and typehelpers](#list-of-exported-api-and-typehelpers)</sub>
3310
+
3277
3311
  <h2 id="timeout">Timeout</h2>
3278
3312
 
3279
3313
  #### reference:`Timeout`
@@ -3399,72 +3433,6 @@ let [res, error] = await TryAsync(async () => {
3399
3433
 
3400
3434
  \*) <sub>[go to list of exported API and typehelpers](#list-of-exported-api-and-typehelpers)</sub>
3401
3435
 
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
3436
  <h2 id="trynew">TryNew</h2>
3469
3437
 
3470
3438
  #### reference:`TryNew`
@@ -3514,7 +3482,7 @@ if (!error) {
3514
3482
  - <i>example</i>:
3515
3483
 
3516
3484
  ```js
3517
- import { readFileSync } from "fs";
3485
+ import { readFileSync } from "node:fs";
3518
3486
  import { TrySync } from "./yourModule.js";
3519
3487
 
3520
3488
  const [data, error] = TrySync(() => {
package/index.mjs CHANGED
@@ -45,11 +45,10 @@ export { GetNamedImportAlias } from './src/function/GetNamedImportAlias.mjs';
45
45
  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
+ export { TemplateLiteral } from './src/function/TemplateLiteral.mjs';
48
49
  export { Timeout } from './src/function/Timeout.mjs';
49
50
  export { Tries } from './src/function/Tries.mjs';
50
51
  export { TryAsync } from './src/function/TryAsync.mjs';
51
- export { TryAsyncCall } from './src/function/TryAsyncCall.mjs';
52
- export { TryCall } from './src/function/TryCall.mjs';
53
52
  export { TryNew } from './src/function/TryNew.mjs';
54
53
  export { TrySync } from './src/function/TrySync.mjs';
55
54
  export { TsToMjs } from './src/function/TsToMjs.mjs';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vivth",
3
- "version": "1.4.2",
3
+ "version": "1.4.4",
4
4
  "description": "library primitives",
5
5
  "main": "index.mjs",
6
6
  "types": "./types/index.d.mts",
@@ -132,7 +132,7 @@ export class LitExp {
132
132
  throw new Error('string undefined');
133
133
  }
134
134
  if (i + 1 == stringsLength && string === '') {
135
- result.push('(?:\\s+?|;|,|$|)');
135
+ result.push('(?:\\s+?|;|,|$|\/\/)');
136
136
  } else {
137
137
  result.push(LitExp.escape(string));
138
138
  }
@@ -0,0 +1,67 @@
1
+ // @ts-check
2
+
3
+ import { IsAsync } from './IsAsync.mjs';
4
+
5
+ /**
6
+ * @description
7
+ * - function helper to create template literal with VALUEhandler to handle each values;
8
+ * @template {(input:any)=>string|Promise<string>} VALUEHANDLER
9
+ * @param {VALUEHANDLER} valueHandler
10
+ * @param {(result:string)=>string} [postProcess]
11
+ * @returns {(strings:TemplateStringsArray,
12
+ * ...values:(Parameters<VALUEHANDLER>[0])[])=>
13
+ * ReturnType<VALUEHANDLER>}
14
+ * @example
15
+ * import { TemplateLiteral } form 'vivth';
16
+ *
17
+ * export const html = TemplateLiteral(
18
+ * (val) => val,
19
+ * // optional
20
+ * (res) => return window.body.innerHTML = res
21
+ * );
22
+ *
23
+ * html`<div>${`<button>innerButton</button>`}</div>`;
24
+ * // this will set innerHTML of body to '<div><button>innerButton</button></div>'
25
+ */
26
+ export function TemplateLiteral(valueHandler, postProcess = undefined) {
27
+ if (IsAsync(valueHandler)) {
28
+ // @ts-expect-error
29
+ return async (strings, ...values) => {
30
+ const result = [];
31
+ for (let i = 0; i < strings.length; i++) {
32
+ result.push(strings[i]);
33
+ if (i < values.length) {
34
+ const value = values[i];
35
+ if (value === undefined) {
36
+ continue;
37
+ }
38
+ result.push(await valueHandler(value));
39
+ }
40
+ }
41
+ const resTrue = result.join('');
42
+ if (!postProcess) {
43
+ return resTrue;
44
+ }
45
+ return postProcess(resTrue);
46
+ };
47
+ }
48
+ // @ts-expect-error
49
+ return (strings, ...values) => {
50
+ const result = [];
51
+ for (let i = 0; i < strings.length; i++) {
52
+ result.push(strings[i]);
53
+ if (i < values.length) {
54
+ const value = values[i];
55
+ if (value === undefined) {
56
+ continue;
57
+ }
58
+ result.push(valueHandler(value));
59
+ }
60
+ }
61
+ const resTrue = result.join('');
62
+ if (!postProcess) {
63
+ return resTrue;
64
+ }
65
+ return postProcess(resTrue);
66
+ };
67
+ }
@@ -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
- // @ts-expect-error
30
- return [undefined, error];
31
+ return resolveErrorArray(error);
31
32
  }
32
33
  }
@@ -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 (err) {
24
- // @ts-expect-error
25
- return [undefined, err];
25
+ } catch (error) {
26
+ return resolveErrorArray(error);
26
27
  }
27
28
  }
@@ -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
- // @ts-expect-error
25
- return [undefined, error];
26
+ return resolveErrorArray(error);
26
27
  }
27
28
  }
@@ -0,0 +1,9 @@
1
+ // @ts-check
2
+
3
+ /**
4
+ * @param {unknown} unknown
5
+ * @returns {[undefined, Error]}
6
+ */
7
+ export const resolveErrorArray = (unknown) => {
8
+ return [undefined, unknown instanceof Error ? unknown : new Error(String(unknown))];
9
+ };
package/types/index.d.mts CHANGED
@@ -37,11 +37,10 @@ export { GetNamedImportAlias } from "./src/function/GetNamedImportAlias.mjs";
37
37
  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
+ export { TemplateLiteral } from "./src/function/TemplateLiteral.mjs";
40
41
  export { Timeout } from "./src/function/Timeout.mjs";
41
42
  export { Tries } from "./src/function/Tries.mjs";
42
43
  export { TryAsync } from "./src/function/TryAsync.mjs";
43
- export { TryAsyncCall } from "./src/function/TryAsyncCall.mjs";
44
- export { TryCall } from "./src/function/TryCall.mjs";
45
44
  export { TryNew } from "./src/function/TryNew.mjs";
46
45
  export { TrySync } from "./src/function/TrySync.mjs";
47
46
  export { TsToMjs } from "./src/function/TsToMjs.mjs";
@@ -0,0 +1,22 @@
1
+ /**
2
+ * @description
3
+ * - function helper to create template literal with VALUEhandler to handle each values;
4
+ * @template {(input:any)=>string|Promise<string>} VALUEHANDLER
5
+ * @param {VALUEHANDLER} valueHandler
6
+ * @param {(result:string)=>string} [postProcess]
7
+ * @returns {(strings:TemplateStringsArray,
8
+ * ...values:(Parameters<VALUEHANDLER>[0])[])=>
9
+ * ReturnType<VALUEHANDLER>}
10
+ * @example
11
+ * import { TemplateLiteral } form 'vivth';
12
+ *
13
+ * export const html = TemplateLiteral(
14
+ * (val) => val,
15
+ * // optional
16
+ * (res) => return window.body.innerHTML = res
17
+ * );
18
+ *
19
+ * html`<div>${`<button>innerButton</button>`}</div>`;
20
+ * // this will set innerHTML of body to '<div><button>innerButton</button></div>'
21
+ */
22
+ export function TemplateLiteral<VALUEHANDLER extends (input: any) => string | Promise<string>>(valueHandler: VALUEHANDLER, postProcess?: (result: string) => string): (strings: TemplateStringsArray, ...values: (Parameters<VALUEHANDLER>[0])[]) => ReturnType<VALUEHANDLER>;
@@ -7,7 +7,7 @@
7
7
  * @returns {[RESULT,undefined]|
8
8
  * [undefined,Error]}
9
9
  * @example
10
- * import { readFileSync } from 'fs';
10
+ * import { readFileSync } from 'node:fs';
11
11
  * import { TrySync } from './yourModule.js';
12
12
  *
13
13
  * const [data, error] = TrySync(() => {
@@ -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
- }
@@ -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];