mikel 0.8.0 → 0.9.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 +20 -0
- package/index.js +15 -5
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -199,6 +199,26 @@ const data = {
|
|
|
199
199
|
console.log(m("{{#unless isAdmin}}Hello guest{{/unless}}", data)); // --> 'Hello guest'
|
|
200
200
|
```
|
|
201
201
|
|
|
202
|
+
#### eq
|
|
203
|
+
|
|
204
|
+
> Added in `v0.9.0`.
|
|
205
|
+
|
|
206
|
+
The `eq` helper renders the blocks only if the two values provided as argument are equal. Example:
|
|
207
|
+
|
|
208
|
+
```javascript
|
|
209
|
+
console.log(m(`{{#eq name "bob"}}Hello bob{{/eq}}`, {name: "bob"})); // --> 'Hello bob'
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
#### ne
|
|
213
|
+
|
|
214
|
+
> Added in `v0.9.0`.
|
|
215
|
+
|
|
216
|
+
The `ne` helper renders the block only if the two values provided as argument are not equal. Example:
|
|
217
|
+
|
|
218
|
+
```javascript
|
|
219
|
+
console.log(m(`{{#ne name "bob"}}Not bob{{/ne}}`, {name: "John"})); // --> 'Not bob'
|
|
220
|
+
```
|
|
221
|
+
|
|
202
222
|
### Custom Helpers
|
|
203
223
|
|
|
204
224
|
> Added in `v0.5.0`.
|
package/index.js
CHANGED
|
@@ -11,6 +11,13 @@ 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 parse = (v, context = {}, vars = {}) => {
|
|
15
|
+
if ((v.startsWith(`"`) && v.endsWith(`"`)) || /^-?\d*\.?\d*$/.test(v) || v === "true" || v === "false" || v === "null") {
|
|
16
|
+
return JSON.parse(v);
|
|
17
|
+
}
|
|
18
|
+
return (v || "").startsWith("@") ? get(vars, v.slice(1)) : get(context, v || ".");
|
|
19
|
+
};
|
|
20
|
+
|
|
14
21
|
const defaultHelpers = {
|
|
15
22
|
"each": (value, opt) => {
|
|
16
23
|
return (typeof value === "object" ? Object.entries(value || {}) : [])
|
|
@@ -19,6 +26,8 @@ const defaultHelpers = {
|
|
|
19
26
|
},
|
|
20
27
|
"if": (value, opt) => !!value ? opt.fn(opt.context) : "",
|
|
21
28
|
"unless": (value, opt) => !!!value ? opt.fn(opt.context) : "",
|
|
29
|
+
"eq": (a, b, opt) => a === b ? opt.fn(opt.context) : "",
|
|
30
|
+
"ne": (a, b, opt) => a !== b ? opt.fn(opt.context) : "",
|
|
22
31
|
};
|
|
23
32
|
|
|
24
33
|
const compile = (tokens, output, context, partials, helpers, vars, fn = {}, index = 0, section = "") => {
|
|
@@ -34,9 +43,9 @@ const compile = (tokens, output, context, partials, helpers, vars, fn = {}, inde
|
|
|
34
43
|
output.push(get(context, tokens[i].slice(1).trim()));
|
|
35
44
|
}
|
|
36
45
|
else if (tokens[i].startsWith("#") && typeof helpers[tokens[i].slice(1).trim().split(" ")[0]] === "function") {
|
|
37
|
-
const [t, ...args] = tokens[i].slice(1).trim().
|
|
46
|
+
const [t, ...args] = tokens[i].slice(1).trim().match(/(?:[^\s"]+|"[^"]*")+/g);
|
|
38
47
|
const j = i + 1;
|
|
39
|
-
output.push(helpers[t](...args.map(v => (v
|
|
48
|
+
output.push(helpers[t](...args.map(v => parse(v, context, vars)), {
|
|
40
49
|
context: context,
|
|
41
50
|
fn: (blockContext = {}, blockVars = {}, blockOutput = []) => {
|
|
42
51
|
i = compile(tokens, blockOutput, blockContext, partials, helpers, {...vars, ...blockVars, root: vars.root}, fn, j, t);
|
|
@@ -45,7 +54,7 @@ const compile = (tokens, output, context, partials, helpers, vars, fn = {}, inde
|
|
|
45
54
|
}));
|
|
46
55
|
// Make sure that this block is executed at least once
|
|
47
56
|
if (i + 1 === j) {
|
|
48
|
-
i = compile(tokens, [], {}, {},
|
|
57
|
+
i = compile(tokens, [], {}, {}, helpers, {}, {}, j, t);
|
|
49
58
|
}
|
|
50
59
|
}
|
|
51
60
|
else if (tokens[i].startsWith("#") || tokens[i].startsWith("^")) {
|
|
@@ -70,9 +79,9 @@ const compile = (tokens, output, context, partials, helpers, vars, fn = {}, inde
|
|
|
70
79
|
}
|
|
71
80
|
}
|
|
72
81
|
else if (tokens[i].startsWith("=")) {
|
|
73
|
-
const [t, ...args] = tokens[i].slice(1).trim().
|
|
82
|
+
const [t, ...args] = tokens[i].slice(1).trim().match(/(?:[^\s"]+|"[^"]*")+/g);
|
|
74
83
|
if (typeof fn[t] === "function") {
|
|
75
|
-
output.push(fn[t](...args.map(v => (v
|
|
84
|
+
output.push(fn[t](...args.map(v => parse(v, context, vars))) || "");
|
|
76
85
|
}
|
|
77
86
|
}
|
|
78
87
|
else if (tokens[i].startsWith("/")) {
|
|
@@ -101,5 +110,6 @@ const mikel = (str, context = {}, opt = {}, output = []) => {
|
|
|
101
110
|
// @description assign utilities
|
|
102
111
|
mikel.escape = escape;
|
|
103
112
|
mikel.get = get;
|
|
113
|
+
mikel.parse = parse;
|
|
104
114
|
|
|
105
115
|
export default mikel;
|