@wxn0brp/falcon-frame 0.9.11 → 0.9.12
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/dist/render.d.ts +1 -1
- package/dist/render.js +21 -10
- package/package.json +1 -1
package/dist/render.d.ts
CHANGED
|
@@ -11,4 +11,4 @@ export interface RenderHTMLOptions {
|
|
|
11
11
|
}
|
|
12
12
|
export declare function renderHTML(options: RenderHTMLOptions): string;
|
|
13
13
|
export declare function interpolate(template: string, data: Record<string, any>): string;
|
|
14
|
-
export declare function expandLoops(template: string, data: Record<string, any>):
|
|
14
|
+
export declare function expandLoops(template: string, data: Record<string, any>): any;
|
package/dist/render.js
CHANGED
|
@@ -113,24 +113,35 @@ function resolvePath(data, key) {
|
|
|
113
113
|
export function interpolate(template, data) {
|
|
114
114
|
return template.replace(/{{(.*?)}}/g, (_, key) => resolvePath(data, key.trim()) ?? "");
|
|
115
115
|
}
|
|
116
|
+
function isTruthy(value) {
|
|
117
|
+
return Array.isArray(value) ? value.length > 0 : Boolean(value);
|
|
118
|
+
}
|
|
116
119
|
export function expandLoops(template, data) {
|
|
117
120
|
return template
|
|
118
121
|
.replace(/<!--\s*each\s+(\w+)\s+as\s+(\w+)\s*-->([\s\S]*?)<!--\s*\/each\s*-->/g, (_, key, itemVar, body) => (resolvePath(data, key) ?? [])
|
|
119
|
-
.map((value, index) =>
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
122
|
+
.map((value, index) => {
|
|
123
|
+
const itemData = {
|
|
124
|
+
...data,
|
|
125
|
+
[itemVar]: value,
|
|
126
|
+
i: index,
|
|
127
|
+
};
|
|
128
|
+
return interpolate(expandLoops(body, itemData), itemData);
|
|
129
|
+
})
|
|
124
130
|
.join(""))
|
|
125
131
|
.replace(/<!--\s*range\s+(\w+)\s*-->([\s\S]*?)<!--\s*\/range\s*-->/g, (_, key, body) => {
|
|
126
132
|
const range = Array.from({
|
|
127
133
|
length: +key,
|
|
128
134
|
}, (_, i) => i);
|
|
129
135
|
return range
|
|
130
|
-
.map((i) =>
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
136
|
+
.map((i) => {
|
|
137
|
+
const itemData = {
|
|
138
|
+
...data,
|
|
139
|
+
i,
|
|
140
|
+
};
|
|
141
|
+
return interpolate(expandLoops(body, itemData), itemData);
|
|
142
|
+
})
|
|
134
143
|
.join("");
|
|
135
|
-
})
|
|
144
|
+
})
|
|
145
|
+
.replace(/<!--\s*if\s+([^\s]+)\s*-->([\s\S]*?)<!--\s*\/if\s*-->/g, (_, key, body) => (isTruthy(resolvePath(data, key)) ? body : ""))
|
|
146
|
+
.replace(/<!--\s*unless\s+([^\s]+)\s*-->([\s\S]*?)<!--\s*\/unless\s*-->/g, (_, key, body) => (isTruthy(resolvePath(data, key)) ? "" : body));
|
|
136
147
|
}
|