mikel 0.6.0 → 0.7.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.
Files changed (3) hide show
  1. package/README.md +23 -3
  2. package/index.js +4 -3
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -234,11 +234,11 @@ Custom helper functions receive a single object parameter containing the followi
234
234
 
235
235
  The helper function must return a string, which will be injected into the result string.
236
236
 
237
- ### Variables
237
+ ### Runtime Variables
238
238
 
239
239
  > Added in `v0.4.0`.
240
240
 
241
- Data Variables in Mikel provide convenient access to special values within your templates. These variables, denoted by the `@` symbol, allow users to interact with specific data contexts or values.
241
+ Runtime Variables in Mikel provide convenient access to special values within your templates. These variables, denoted by the `@` symbol, allow users to interact with specific data contexts or values at runtime. Runtime variables are usually generated by helpers like `#each`.
242
242
 
243
243
  #### @root
244
244
 
@@ -266,7 +266,27 @@ The `@key` variable allows users to retrieve the current key of the object entry
266
266
 
267
267
  The `@value` variable allows users to retrieve the current value of the object entry when iterating over an object using the `#each` helper. It simplifies access to object values for dynamic rendering and data manipulation.
268
268
 
269
- ### Custom variables
269
+ #### @first
270
+
271
+ > Added in `v0.7.0`.
272
+
273
+ The `@first` variable allows to check if the current iteration using the `#each` helper is the first item in the array or object.
274
+
275
+ ```
276
+ {{#each items}} {{.}}: {{#if @first}}first item!{{/if}}{{#unless @first}}not first{{/if}} {{/each}}
277
+ ```
278
+
279
+ #### @last
280
+
281
+ > Added in `v0.7.0`.
282
+
283
+ The `@last` variable allows to check if the current iteration using the `#each` helper is the last item in the array or object.
284
+
285
+ ```
286
+ {{#each items}}{{@index}}:{{.}} {{#unless @last}},{{/unless}}{{/each}}
287
+ ```
288
+
289
+ ### Custom runtime variables
270
290
 
271
291
  > Added in `v0.5.0`
272
292
 
package/index.js CHANGED
@@ -13,8 +13,9 @@ const get = (c, p) => (p === "." ? c : p.split(".").reduce((x, k) => x?.[k], c))
13
13
 
14
14
  const defaultHelpers = {
15
15
  "each": ({value, fn}) => {
16
- return (typeof value === "object" ? Object.entries(value || {}) : [])
17
- .map((item, index) => fn(item[1], {index: index, key: item[0], value: item[1]}))
16
+ const items = (typeof value === "object" ? Object.entries(value || {}) : []);
17
+ return items
18
+ .map((item, index) => fn(item[1], {index: index, key: item[0], value: item[1], first: index === 0, last: index === items.length - 1}))
18
19
  .join("");
19
20
  },
20
21
  "if": ({value, fn, context}) => !!value ? fn(context) : "",
@@ -39,7 +40,7 @@ const compile = (tokens, output, context, partials, helpers, vars, index = 0, se
39
40
  output.push(helpers[t]({
40
41
  context: context,
41
42
  key: v || ".",
42
- value: get(context, v || "."),
43
+ value: (v || "").startsWith("@") ? get(vars, v.slice(1)) : get(context, v || "."),
43
44
  fn: (blockContext = {}, blockVars = {}, blockOutput = []) => {
44
45
  i = compile(tokens, blockOutput, blockContext, partials, helpers, {...vars, ...blockVars, root: vars.root}, j, t);
45
46
  return blockOutput.join("");
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.6.0",
4
+ "version": "0.7.0",
5
5
  "type": "module",
6
6
  "author": {
7
7
  "name": "Josemi Juanes",