mikel 0.23.0 → 0.24.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 +26 -20
- package/index.d.ts +3 -3
- package/index.js +5 -6
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -620,83 +620,89 @@ const result = mikel("Hello, {{name}}!", data);
|
|
|
620
620
|
console.log(result); // Output: "Hello, World!"
|
|
621
621
|
```
|
|
622
622
|
|
|
623
|
-
### `mikel.create(
|
|
623
|
+
### `mikel.create(options)`
|
|
624
624
|
|
|
625
|
-
|
|
625
|
+
> Removed `template` argument in `v0.24.0`.
|
|
626
626
|
|
|
627
|
-
|
|
627
|
+
Allows to create an isolated instance of mikel, useful when you want to use the same options for multiple templates without passing them every time. You can pass an `options` object with the same structure as the one used in the `mikel` function, which will be used for all templates compiled with this instance.
|
|
628
|
+
|
|
629
|
+
It returns a function that you can call with the template and data to compile the template.
|
|
628
630
|
|
|
629
631
|
```javascript
|
|
630
632
|
import mikel from "mikel";
|
|
631
633
|
|
|
632
|
-
const
|
|
634
|
+
const mk = mikel.create({
|
|
635
|
+
partials: {
|
|
636
|
+
hello: "Hello, {{name}}!",
|
|
637
|
+
},
|
|
638
|
+
});
|
|
633
639
|
|
|
634
|
-
console.log(
|
|
635
|
-
console.log(
|
|
640
|
+
console.log(mk("{{>hello}}", {name: "Bob"})); // --> "Hello, Bob!"
|
|
641
|
+
console.log(mk("{{>hello}}", {name: "Susan"})); // --> "Hello, Susan!"
|
|
636
642
|
```
|
|
637
643
|
|
|
638
644
|
It also exposes the following additional methods:
|
|
639
645
|
|
|
640
|
-
#### `
|
|
646
|
+
#### `mk.use(options)`
|
|
641
647
|
|
|
642
648
|
> Added in `v0.19.0`.
|
|
643
649
|
|
|
644
650
|
Allows to extend the templating with custom **helpers**, **functions**, and **partials**.
|
|
645
651
|
|
|
646
652
|
```javascript
|
|
647
|
-
|
|
653
|
+
mk.use({
|
|
648
654
|
partials: {
|
|
649
655
|
foo: "bar",
|
|
650
656
|
},
|
|
651
657
|
});
|
|
652
658
|
```
|
|
653
659
|
|
|
654
|
-
#### `
|
|
660
|
+
#### `mk.addHelper(helperName, helperFn)`
|
|
655
661
|
|
|
656
662
|
Allows to register a new helper instead of using the `options` object.
|
|
657
663
|
|
|
658
664
|
```javascript
|
|
659
|
-
|
|
665
|
+
mk.addHelper("foo", () => { ... });
|
|
660
666
|
```
|
|
661
667
|
|
|
662
|
-
#### `
|
|
668
|
+
#### `mk.removeHelper(helperName)`
|
|
663
669
|
|
|
664
670
|
Removes a previously added helper.
|
|
665
671
|
|
|
666
672
|
```javascript
|
|
667
|
-
|
|
673
|
+
mk.removeHelper("foo");
|
|
668
674
|
```
|
|
669
675
|
|
|
670
|
-
#### `
|
|
676
|
+
#### `mk.addPartial(partialName, partialCode)`
|
|
671
677
|
|
|
672
678
|
Registers a new partial instead of using the `options` object.
|
|
673
679
|
|
|
674
680
|
```javascript
|
|
675
|
-
|
|
681
|
+
mk.addPartial("bar", " ... ");
|
|
676
682
|
```
|
|
677
683
|
|
|
678
|
-
#### `
|
|
684
|
+
#### `mk.removePartial(partialName)`
|
|
679
685
|
|
|
680
686
|
Removes a previously added partial.
|
|
681
687
|
|
|
682
688
|
```javascript
|
|
683
|
-
|
|
689
|
+
mk.removePartial("bar");
|
|
684
690
|
```
|
|
685
691
|
|
|
686
|
-
#### `
|
|
692
|
+
#### `mk.addFunction(fnName, fn)`
|
|
687
693
|
|
|
688
694
|
Registers a new function instead of using the `options` object.
|
|
689
695
|
|
|
690
696
|
```javascript
|
|
691
|
-
|
|
697
|
+
mk.addFunction("foo", () => "...");
|
|
692
698
|
```
|
|
693
699
|
|
|
694
|
-
#### `
|
|
700
|
+
#### `mk.removeFunction(fnName)`
|
|
695
701
|
|
|
696
702
|
Removes a previously added function.
|
|
697
703
|
|
|
698
704
|
```javascript
|
|
699
|
-
|
|
705
|
+
mk.removeFunction("foo");
|
|
700
706
|
```
|
|
701
707
|
|
|
702
708
|
### `mikel.escape(str)`
|
package/index.d.ts
CHANGED
|
@@ -33,8 +33,8 @@ declare interface MikelOptions {
|
|
|
33
33
|
variables?: Variables;
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
-
declare interface
|
|
37
|
-
(data?: any): string;
|
|
36
|
+
declare interface MikelInstance {
|
|
37
|
+
(template: string, data?: any): string;
|
|
38
38
|
use(options: Partial<MikelOptions>): MikelTemplate;
|
|
39
39
|
addHelper(name: string, fn: HelperFunction): void;
|
|
40
40
|
removeHelper(name: string): void;
|
|
@@ -46,7 +46,7 @@ declare interface MikelTemplate {
|
|
|
46
46
|
|
|
47
47
|
declare const mikel: {
|
|
48
48
|
(template: string, data?: any, options?: Partial<MikelOptions>): string;
|
|
49
|
-
create(
|
|
49
|
+
create(options?: Partial<MikelOptions>): MikelInstance;
|
|
50
50
|
escape(str: string): string;
|
|
51
51
|
get(context: any, path: string): any;
|
|
52
52
|
parse(value: string, context?: any, vars?: any): any;
|
package/index.js
CHANGED
|
@@ -13,7 +13,7 @@ const get = (c, p) => (p === "." ? c : p.split(".").reduce((x, k) => x?.[k], c))
|
|
|
13
13
|
// @description tokenize and untokenize methods
|
|
14
14
|
const tokenize = (str = "") => str.split(/\{\{|\}\}/);
|
|
15
15
|
const untokenize = (ts = [], s = "{{", e = "}}") => {
|
|
16
|
-
return ts.reduce((p, t, i) => p + (i % 2 === 0 ? e : s) + t);
|
|
16
|
+
return ts.length > 0 ? ts.reduce((p, t, i) => p + (i % 2 === 0 ? e : s) + t) : "";
|
|
17
17
|
};
|
|
18
18
|
|
|
19
19
|
// @description parse string arguments
|
|
@@ -80,9 +80,8 @@ const defaultHelpers = {
|
|
|
80
80
|
};
|
|
81
81
|
|
|
82
82
|
// @description create a new instance of mikel
|
|
83
|
-
const create = (
|
|
83
|
+
const create = (options = {}) => {
|
|
84
84
|
const ctx = {
|
|
85
|
-
tokens: tokenize(template),
|
|
86
85
|
helpers: Object.assign({}, defaultHelpers, options?.helpers || {}),
|
|
87
86
|
partials: Object.assign({}, options?.partials || {}),
|
|
88
87
|
functions: options?.functions || {},
|
|
@@ -170,8 +169,8 @@ const create = (template = "", options = {}) => {
|
|
|
170
169
|
return i;
|
|
171
170
|
};
|
|
172
171
|
// entry method to compile the template with the provided data object
|
|
173
|
-
const compileTemplate = (data = {}, output = []) => {
|
|
174
|
-
compile(
|
|
172
|
+
const compileTemplate = (template, data = {}, output = []) => {
|
|
173
|
+
compile(tokenize(template), output, data, {root: data, ...ctx.variables}, 0, "");
|
|
175
174
|
return output.join("");
|
|
176
175
|
};
|
|
177
176
|
// assign api methods and return method to compile the template
|
|
@@ -198,7 +197,7 @@ const create = (template = "", options = {}) => {
|
|
|
198
197
|
|
|
199
198
|
// @description main compiler function
|
|
200
199
|
const mikel = (template = "", data = {}, options = {}) => {
|
|
201
|
-
return create(template,
|
|
200
|
+
return create(options)(template, data);
|
|
202
201
|
};
|
|
203
202
|
|
|
204
203
|
// @description assign utilities
|