mikel 0.5.0 → 0.6.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 +11 -3
- package/index.js +24 -18
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -289,7 +289,7 @@ In this example, the custom data variable `customVariable` is defined with the v
|
|
|
289
289
|
|
|
290
290
|
## API
|
|
291
291
|
|
|
292
|
-
### `
|
|
292
|
+
### `mikel(template, data[, options])`
|
|
293
293
|
|
|
294
294
|
Render the given template string with the provided data object.
|
|
295
295
|
|
|
@@ -303,16 +303,24 @@ Render the given template string with the provided data object.
|
|
|
303
303
|
Returns: A string with the rendered output.
|
|
304
304
|
|
|
305
305
|
```javascript
|
|
306
|
-
import
|
|
306
|
+
import mikel from "mikel";
|
|
307
307
|
|
|
308
308
|
const data = {
|
|
309
309
|
name: "World",
|
|
310
310
|
};
|
|
311
311
|
|
|
312
|
-
const result =
|
|
312
|
+
const result = mikel("Hello, {{name}}!", data);
|
|
313
313
|
console.log(result); // Output: "Hello, World!"
|
|
314
314
|
```
|
|
315
315
|
|
|
316
|
+
### `mikel.escape(str)`
|
|
317
|
+
|
|
318
|
+
This function converts special HTML characters `&`, `<`, `>`, `"`, and `'` to their corresponding HTML entities.
|
|
319
|
+
|
|
320
|
+
### `mikel.get(object, path)`
|
|
321
|
+
|
|
322
|
+
This function returns the value in `object` following the provided `path` string.
|
|
323
|
+
|
|
316
324
|
## License
|
|
317
325
|
|
|
318
326
|
This project is licensed under the [MIT License](LICENSE).
|
package/index.js
CHANGED
|
@@ -11,7 +11,7 @@ const escape = s => s.toString().replace(/[&<>\"']/g, m => escapedChars[m]);
|
|
|
11
11
|
|
|
12
12
|
const get = (c, p) => (p === "." ? c : p.split(".").reduce((x, k) => x?.[k], c)) ?? "";
|
|
13
13
|
|
|
14
|
-
const
|
|
14
|
+
const defaultHelpers = {
|
|
15
15
|
"each": ({value, fn}) => {
|
|
16
16
|
return (typeof value === "object" ? Object.entries(value || {}) : [])
|
|
17
17
|
.map((item, index) => fn(item[1], {index: index, key: item[0], value: item[1]}))
|
|
@@ -19,39 +19,35 @@ const helpers = new Map(Object.entries({
|
|
|
19
19
|
},
|
|
20
20
|
"if": ({value, fn, context}) => !!value ? fn(context) : "",
|
|
21
21
|
"unless": ({value, fn, context}) => !!!value ? fn(context) : "",
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
const hasHelper = (n, o) => helpers.has(n) || typeof o?.helpers?.[n] === "function";
|
|
25
|
-
const getHelper = (n, o) => helpers.get(n) || o?.helpers?.[n];
|
|
22
|
+
};
|
|
26
23
|
|
|
27
|
-
const compile = (tokens, output, context,
|
|
24
|
+
const compile = (tokens, output, context, partials, helpers, vars, index = 0, section = "") => {
|
|
28
25
|
let i = index;
|
|
29
26
|
while (i < tokens.length) {
|
|
30
27
|
if (i % 2 === 0) {
|
|
31
28
|
output.push(tokens[i]);
|
|
32
29
|
}
|
|
33
30
|
else if (tokens[i].startsWith("@")) {
|
|
34
|
-
output.push(get(
|
|
31
|
+
output.push(get(vars, tokens[i].slice(1).trim() ?? "_") ?? "");
|
|
35
32
|
}
|
|
36
33
|
else if (tokens[i].startsWith("!")) {
|
|
37
34
|
output.push(get(context, tokens[i].slice(1).trim()));
|
|
38
35
|
}
|
|
39
|
-
else if (tokens[i].startsWith("#") &&
|
|
36
|
+
else if (tokens[i].startsWith("#") && typeof helpers[tokens[i].slice(1).trim().split(" ")[0]] === "function") {
|
|
40
37
|
const [t, v] = tokens[i].slice(1).trim().split(" ");
|
|
41
38
|
const j = i + 1;
|
|
42
|
-
output.push(
|
|
39
|
+
output.push(helpers[t]({
|
|
43
40
|
context: context,
|
|
44
41
|
key: v || ".",
|
|
45
42
|
value: get(context, v || "."),
|
|
46
|
-
options: opt,
|
|
47
43
|
fn: (blockContext = {}, blockVars = {}, blockOutput = []) => {
|
|
48
|
-
i = compile(tokens, blockOutput, blockContext,
|
|
44
|
+
i = compile(tokens, blockOutput, blockContext, partials, helpers, {...vars, ...blockVars, root: vars.root}, j, t);
|
|
49
45
|
return blockOutput.join("");
|
|
50
46
|
},
|
|
51
47
|
}));
|
|
52
48
|
// Make sure that this block has been executed
|
|
53
49
|
if (i + 1 === j) {
|
|
54
|
-
i = compile(tokens, [], {},
|
|
50
|
+
i = compile(tokens, [], {}, partials, helpers, vars, j, t);
|
|
55
51
|
}
|
|
56
52
|
}
|
|
57
53
|
else if (tokens[i].startsWith("#") || tokens[i].startsWith("^")) {
|
|
@@ -61,18 +57,18 @@ const compile = (tokens, output, context, opt, index = 0, section = "", vars = {
|
|
|
61
57
|
if (!negate && value && Array.isArray(value)) {
|
|
62
58
|
const j = i + 1;
|
|
63
59
|
(value.length > 0 ? value : [""]).forEach(item => {
|
|
64
|
-
i = compile(tokens, value.length > 0 ? output : [], item,
|
|
60
|
+
i = compile(tokens, value.length > 0 ? output : [], item, partials, helpers, vars, j, t);
|
|
65
61
|
});
|
|
66
62
|
}
|
|
67
63
|
else {
|
|
68
64
|
const includeOutput = (!negate && !!value) || (negate && !!!value);
|
|
69
|
-
i = compile(tokens, includeOutput ? output : [], context,
|
|
65
|
+
i = compile(tokens, includeOutput ? output : [], context, partials, helpers, vars, i + 1, t);
|
|
70
66
|
}
|
|
71
67
|
}
|
|
72
68
|
else if (tokens[i].startsWith(">")) {
|
|
73
69
|
const [t, v] = tokens[i].slice(1).trim().split(" ");
|
|
74
|
-
if (typeof
|
|
75
|
-
compile(
|
|
70
|
+
if (typeof partials[t] === "string") {
|
|
71
|
+
compile(partials[t].split(tags), output, v ? get(context, v) : context, partials, helpers, vars, 0, "");
|
|
76
72
|
}
|
|
77
73
|
}
|
|
78
74
|
else if (tokens[i].startsWith("/")) {
|
|
@@ -89,7 +85,17 @@ const compile = (tokens, output, context, opt, index = 0, section = "", vars = {
|
|
|
89
85
|
return i;
|
|
90
86
|
};
|
|
91
87
|
|
|
92
|
-
|
|
93
|
-
|
|
88
|
+
// @description main compiler function
|
|
89
|
+
const mikel = (str, context = {}, opt = {}, output = []) => {
|
|
90
|
+
const partials = Object.assign({}, opt.partials || {});
|
|
91
|
+
const helpers = Object.assign({}, defaultHelpers, opt.helpers || {});
|
|
92
|
+
const variables = Object.assign({}, opt.variables || {}, {root: context});
|
|
93
|
+
compile(str.split(tags), output, context, partials, helpers, variables, 0, "");
|
|
94
94
|
return output.join("");
|
|
95
95
|
};
|
|
96
|
+
|
|
97
|
+
// @description assign utilities
|
|
98
|
+
mikel.escape = escape;
|
|
99
|
+
mikel.get = get;
|
|
100
|
+
|
|
101
|
+
export default mikel;
|