mikel 0.25.1 → 0.26.1

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.
Files changed (3) hide show
  1. package/README.md +5 -5
  2. package/index.js +7 -1
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -50,7 +50,7 @@ const result = m("{{#isAdmin}}You are Admin{{/isAdmin}}", data);
50
50
  // Output: 'You are Admin'
51
51
  ```
52
52
 
53
- You can also use sections for looping over arrays. When looping over array of strings, you can use a dot `.` to reference the current item in the loop.
53
+ You can also use sections for looping over arrays. When looping over array of strings, you can use a dot `.` or the `this` word to reference the current item in the loop.
54
54
 
55
55
  Example:
56
56
 
@@ -232,7 +232,7 @@ const data = {
232
232
  users: ["John", "Alice", "Bob"],
233
233
  };
234
234
 
235
- console.log(m("{{#each users}}{{.}}, {{/each}}", data)); // --> 'John, Alice, Bob, '
235
+ console.log(m("{{#each users}}{{this}}, {{/each}}", data)); // --> 'John, Alice, Bob, '
236
236
  ```
237
237
 
238
238
  When looping throug arrays, you can use the variable `@index` to access to the current index of the item in the array:
@@ -242,7 +242,7 @@ const data = {
242
242
  users: ["John", "Alice", "Bob"],
243
243
  };
244
244
 
245
- console.log(m("{{#each users}}{{@index}}: {{.}}, {{/each}}", data)); // --> '0: John, 1: Alice, 2: Bob, '
245
+ console.log(m("{{#each users}}{{@index}}: {{this}}, {{/each}}", data)); // --> '0: John, 1: Alice, 2: Bob, '
246
246
  ```
247
247
 
248
248
  The `each` helper can also iterate over objects:
@@ -254,7 +254,7 @@ const data = {
254
254
  },
255
255
  };
256
256
 
257
- console.log(m("{{#each values}}{{.}}{{/each}}", data)); // --> 'bar'
257
+ console.log(m("{{#each values}}{{this}}{{/each}}", data)); // --> 'bar'
258
258
  ```
259
259
 
260
260
  When looping throug objects, you can use the variable `@key` to access to the current key in the object, and the variable `@value` to access to the corresponding value:
@@ -277,7 +277,7 @@ The `each` helper also supports the following options, provided as keyword argum
277
277
  Example:
278
278
 
279
279
  ```javascript
280
- console.log(m("{{each values limit=2}}{{.}}{{/each}}", {values: [0, 1, 2, 3]})); // --> '01'
280
+ console.log(m("{{each values limit=2}}{{this}}{{/each}}", {values: [0, 1, 2, 3]})); // --> '01'
281
281
  ```
282
282
 
283
283
 
package/index.js CHANGED
@@ -8,7 +8,13 @@ const escapedChars = {
8
8
 
9
9
  const escape = s => s.toString().replace(/[&<>\"']/g, m => escapedChars[m]);
10
10
 
11
- const get = (c, p) => (p === "." ? c : p.split(".").reduce((x, k) => x?.[k], c)) ?? "";
11
+ // @description get the value in the provided object and the given path string
12
+ const get = (data, path = ".") => {
13
+ if (path === "." || path === "this") {
14
+ return data ?? "";
15
+ }
16
+ return path.replace(/^this\./, "").split(".").reduce((x, k) => x?.[k], data) ?? "";
17
+ };
12
18
 
13
19
  // @description tokenize and untokenize methods
14
20
  const tokenize = (str = "") => str.split(/\{\{|\}\}/);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "mikel",
3
3
  "description": "Micro templating library with zero dependencies",
4
- "version": "0.25.1",
4
+ "version": "0.26.1",
5
5
  "type": "module",
6
6
  "author": {
7
7
  "name": "Josemi Juanes",