mikel 0.37.0 → 0.38.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.
- package/index.d.ts +7 -3
- package/index.js +11 -7
- package/package.json +1 -1
package/index.d.ts
CHANGED
|
@@ -12,7 +12,7 @@ export type MikelHelper = (params: {
|
|
|
12
12
|
fn: MikelHelperCallback;
|
|
13
13
|
}) => string;
|
|
14
14
|
|
|
15
|
-
export type MikelPartial = {
|
|
15
|
+
export type MikelPartial = string | {
|
|
16
16
|
body: string;
|
|
17
17
|
data: Record<string, any>;
|
|
18
18
|
};
|
|
@@ -26,9 +26,11 @@ export type MikelFunction = (params: {
|
|
|
26
26
|
|
|
27
27
|
export type MikelState = Record<string, string>;
|
|
28
28
|
|
|
29
|
+
export type MikelTransform = (content: string) => string;
|
|
30
|
+
|
|
29
31
|
export type MikelOptions = {
|
|
30
32
|
helpers?: Record<string, MikelHelper>;
|
|
31
|
-
partials?: Record<string,
|
|
33
|
+
partials?: Record<string, MikelPartial>;
|
|
32
34
|
functions?: Record<string, MikelFunction>;
|
|
33
35
|
};
|
|
34
36
|
|
|
@@ -41,6 +43,8 @@ export type MikelContext = {
|
|
|
41
43
|
functions: Record<string, MikelFunction>;
|
|
42
44
|
partials: Record<string, MikelPartial>;
|
|
43
45
|
initialState: MikelState;
|
|
46
|
+
preTransforms: MikelTransform[];
|
|
47
|
+
postTransforms: MikelTransform[];
|
|
44
48
|
};
|
|
45
49
|
|
|
46
50
|
export type MikelPlugin = (ctx: MikelContext) => void;
|
|
@@ -52,7 +56,7 @@ export type Mikel = {
|
|
|
52
56
|
removeHelper(name: string): void;
|
|
53
57
|
addFunction(name: string, fn: MikelFunction): void;
|
|
54
58
|
removeFunction(name: string): void;
|
|
55
|
-
addPartial(name: string, partial:
|
|
59
|
+
addPartial(name: string, partial: MikelPartial): void;
|
|
56
60
|
removePartial(name: string): void;
|
|
57
61
|
};
|
|
58
62
|
|
package/index.js
CHANGED
|
@@ -29,8 +29,8 @@ const untokenize = (ts = [], s = "{{", e = "}}") => {
|
|
|
29
29
|
// @description tokenize args
|
|
30
30
|
const tokenizeArgs = (str = "", tokens = [], strings = []) => {
|
|
31
31
|
let current = "", depth = 0;
|
|
32
|
-
// 1. replace strings
|
|
33
|
-
str = str.replace(/"([^"\\]|\\.)*"/g, (match) => {
|
|
32
|
+
// 1. replace strings in single/double quotes
|
|
33
|
+
str = str.replace(/("([^"\\]|\\.)*"|'([^'\\]|\\.)*')/g, (match) => {
|
|
34
34
|
const id = `__STR${strings.length}__`;
|
|
35
35
|
strings.push(match);
|
|
36
36
|
return id;
|
|
@@ -100,8 +100,9 @@ const parse = (v, data = {}, state = {}, fns = {}) => {
|
|
|
100
100
|
if (v.startsWith("(") && v.endsWith(")")) {
|
|
101
101
|
return evaluateExpression(v.slice(1, -1).trim(), data, state, fns);
|
|
102
102
|
}
|
|
103
|
-
if ((v.startsWith(`"`) && v.endsWith(`"`)) || /^-?\d+\.?\d*$/.test(v) || v === "true" || v === "false" || v === "null") {
|
|
104
|
-
|
|
103
|
+
if ((v.startsWith(`"`) && v.endsWith(`"`)) || (v.startsWith(`'`) && v.endsWith(`'`)) || /^-?\d+\.?\d*$/.test(v) || v === "true" || v === "false" || v === "null") {
|
|
104
|
+
const normalized = (v.startsWith(`'`) && v.endsWith(`'`)) ? `"${v.slice(1, -1).replace(/"/g, '\\"')}"` : v;
|
|
105
|
+
return JSON.parse(normalized);
|
|
105
106
|
}
|
|
106
107
|
return (v || "").startsWith("@") ? get(state, v.slice(1)) : get(data, v || ".");
|
|
107
108
|
};
|
|
@@ -134,6 +135,7 @@ const compile = (ctx, tokens, output, data, state, index = 0, section = "") => {
|
|
|
134
135
|
const j = i + 1;
|
|
135
136
|
i = findClosingToken(tokens, j, t);
|
|
136
137
|
output.push(ctx.helpers[t]({
|
|
138
|
+
context: ctx,
|
|
137
139
|
args: args,
|
|
138
140
|
options: opt,
|
|
139
141
|
tokens: tokens.slice(j, i),
|
|
@@ -279,12 +281,14 @@ const create = (options = {}) => {
|
|
|
279
281
|
partials: Object.assign({}, options?.partials || {}),
|
|
280
282
|
functions: Object.assign({}, options?.functions || {}),
|
|
281
283
|
initialState: {}, // Object.assign({}, options?.initialState || {}),
|
|
284
|
+
preTransforms: [], // list of pre-transform to apply to the template
|
|
285
|
+
postTransforms: [], // list of post-transforms to apply to the result
|
|
282
286
|
});
|
|
283
287
|
// entry method to compile the template with the provided data object
|
|
284
288
|
const compileTemplate = (template, data = {}) => {
|
|
285
|
-
const output = [];
|
|
286
|
-
compile(ctx, tokenize(
|
|
287
|
-
return output.join("");
|
|
289
|
+
const output = [], input = ctx.preTransforms.reduce((content, fn) => fn(content), template);
|
|
290
|
+
compile(ctx, tokenize(input), output, data, { ...ctx.initialState, root: data }, 0, "");
|
|
291
|
+
return ctx.postTransforms.reduce((result, fn) => fn(result), output.join(""));
|
|
288
292
|
};
|
|
289
293
|
// assign api methods and return method to compile the template
|
|
290
294
|
return Object.assign(compileTemplate, {
|