mikel 0.28.0 → 0.29.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 CHANGED
@@ -433,10 +433,10 @@ const result = m(template, data, options);
433
433
  console.log(result); // Output: "Hello, World!"
434
434
  ```
435
435
 
436
- Custom helper functions receive a single object as argument, containing the following keys:
436
+ Custom helper functions receive a single `params` object as argument, containing the following fields:
437
437
 
438
438
  - `args`: an array containing the variables with the helper is called in the template.
439
- - `opt`: an object containing the keyword arguments provided to the helper.
439
+ - `options`: an object containing the keyword arguments provided to the helper.
440
440
  - `data`: the current data where the helper has been executed.
441
441
  - `variables`: an object containing the runtime variables available in the current context (e.g., `@root`, `@index`, etc.).
442
442
  - `fn`: a function that executes the template provided in the helper block and returns a string with the evaluated template in the provided context.
@@ -562,10 +562,10 @@ Mikel allows users to define custom functions that can be used within templates
562
562
 
563
563
  Functions should be provided in the `options.functions` field of the options object when rendering a template. Each function is defined by a name and a corresponding function that performs the desired operation.
564
564
 
565
- Functions will receive a single object as argument, containing the following keys:
565
+ Functions will receive a single `params` object as argument, containing the following keys:
566
566
 
567
567
  - `args`: an array containing the variables with the function is called in the template.
568
- - `opt`: an object containing the keyword arguments provided to the function.
568
+ - `options`: an object containing the keyword arguments provided to the function.
569
569
  - `data`: the current data object where the function has been executed.
570
570
  - `variables`: an object containing the runtime variables available in the current context (e.g., `@root`, `@index`, etc.).
571
571
 
@@ -632,7 +632,7 @@ const data = {
632
632
  const options = {
633
633
  functions: {
634
634
  fullName: params => {
635
- return `${params.opt.firstName} ${params.opt.lastName}`;
635
+ return `${params.options.firstName} ${params.options.lastName}`;
636
636
  }
637
637
  },
638
638
  };
package/index.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  export type MikelHelper = (params: {
2
2
  args: any[];
3
- opt: Record<string, any>;
3
+ opt?: Record<string, any>;
4
+ options: Record<string, any>;
4
5
  tokens: string[];
5
6
  data: Record<string, any>;
6
7
  variables: Record<string, any>;
@@ -14,7 +15,8 @@ export type MikelPartial = {
14
15
 
15
16
  export type MikelFunction = (params: {
16
17
  args: any[];
17
- opt: Record<string, any>;
18
+ opt?: Record<string, any>;
19
+ options: Record<string,any>;
18
20
  data: Record<string, any>;
19
21
  variables: Record<string, any>;
20
22
  }) => string | void;
package/index.js CHANGED
@@ -75,9 +75,17 @@ const findClosingToken = (tokens, i, token) => {
75
75
  const defaultHelpers = {
76
76
  "each": p => {
77
77
  const items = typeof p.args[0] === "object" ? Object.entries(p.args[0] || {}) : [];
78
- const limit = Math.min(items.length - (p.opt.skip || 0), p.opt.limit || items.length);
79
- return items.slice(p.opt.skip || 0, (p.opt.skip || 0) + limit)
80
- .map((item, index) => p.fn(item[1], {index: index, key: item[0], value: item[1], first: index === 0, last: index === items.length - 1}))
78
+ const limit = Math.min(items.length - (p.options?.skip || 0), p.options?.limit || items.length);
79
+ return items.slice(p.options?.skip || 0, (p.options?.skip || 0) + limit)
80
+ .map((item, index) => {
81
+ return p.fn(item[1], {
82
+ index: index,
83
+ key: item[0],
84
+ value: item[1],
85
+ first: index === 0,
86
+ last: index === items.length - 1,
87
+ });
88
+ })
81
89
  .join("");
82
90
  },
83
91
  "if": p => !!p.args[0] ? p.fn(p.data) : "",
@@ -183,7 +191,7 @@ const create = (options = {}) => {
183
191
  else if (tokens[i].startsWith("=")) {
184
192
  const [t, args, opt] = parseArgs(tokens[i].slice(1), data, vars);
185
193
  if (typeof ctx.functions[t] === "function") {
186
- output.push(ctx.functions[t]({args, opt, data, variables: vars}) || "");
194
+ output.push(ctx.functions[t]({args, opt, options: opt, data, variables: vars}) || "");
187
195
  }
188
196
  }
189
197
  else if (tokens[i].startsWith("/")) {
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.28.0",
4
+ "version": "0.29.0",
5
5
  "type": "module",
6
6
  "author": {
7
7
  "name": "Josemi Juanes",