vivth 1.4.3 → 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,6 +92,7 @@ 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)
@@ -3272,6 +3273,41 @@ myInstance["vivth:unwrapLazy;"](); // forcefully call factory generator;
3272
3273
 
3273
3274
  \*) <sub>[go to list of exported API and typehelpers](#list-of-exported-api-and-typehelpers)</sub>
3274
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
+
3275
3311
  <h2 id="timeout">Timeout</h2>
3276
3312
 
3277
3313
  #### reference:`Timeout`
package/index.mjs CHANGED
@@ -45,6 +45,7 @@ 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';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vivth",
3
- "version": "1.4.3",
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
+ }
package/types/index.d.mts CHANGED
@@ -37,6 +37,7 @@ 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";
@@ -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>;