@ztimson/utils 0.27.19 → 0.28.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/dist/index.cjs +56 -18
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +56 -18
- package/dist/index.mjs.map +1 -1
- package/dist/template.d.ts +1 -0
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -2295,6 +2295,31 @@ ${opts.message || this.desc}`;
|
|
|
2295
2295
|
}
|
|
2296
2296
|
class TemplateError extends BadRequestError {
|
|
2297
2297
|
}
|
|
2298
|
+
function findTemplateVars(html) {
|
|
2299
|
+
const variables = /* @__PURE__ */ new Set();
|
|
2300
|
+
const regex = /\{\{\s*([^<>\*\?!/}\s][^}]*?)\s*}}/g;
|
|
2301
|
+
let match;
|
|
2302
|
+
while ((match = regex.exec(html)) !== null) {
|
|
2303
|
+
const code = match[1].trim();
|
|
2304
|
+
const varMatch = code.match(/^([a-zA-Z_$][a-zA-Z0-9_$.]*)/);
|
|
2305
|
+
if (varMatch) variables.add(varMatch[1]);
|
|
2306
|
+
}
|
|
2307
|
+
const result = {};
|
|
2308
|
+
for (const path of variables) {
|
|
2309
|
+
const parts = path.split(".");
|
|
2310
|
+
let current = result;
|
|
2311
|
+
for (let i = 0; i < parts.length; i++) {
|
|
2312
|
+
const part = parts[i];
|
|
2313
|
+
if (i === parts.length - 1) {
|
|
2314
|
+
current[part] = "";
|
|
2315
|
+
} else {
|
|
2316
|
+
current[part] = current[part] || {};
|
|
2317
|
+
current = current[part];
|
|
2318
|
+
}
|
|
2319
|
+
}
|
|
2320
|
+
}
|
|
2321
|
+
return result;
|
|
2322
|
+
}
|
|
2298
2323
|
async function renderTemplate(template, data, fetch2) {
|
|
2299
2324
|
let content = template, found;
|
|
2300
2325
|
const now = /* @__PURE__ */ new Date(), d = {
|
|
@@ -2318,38 +2343,50 @@ ${opts.message || this.desc}`;
|
|
|
2318
2343
|
else return false;
|
|
2319
2344
|
}
|
|
2320
2345
|
};
|
|
2321
|
-
while (!!(found = /\{\{\s*?\?\s*?(.+?)\s*?}}([\s\S]*?)
|
|
2346
|
+
while (!!(found = /\{\{\s*?\?\s*?(.+?)\s*?}}([\s\S]*?)\{\{\s*?\/\?\s*?}}/g.exec(content))) {
|
|
2322
2347
|
const nested = matchAll(found[0], /\{\{\s*?\?.+?}}/g).slice(-1)?.[0]?.index;
|
|
2323
|
-
if (nested != 0)
|
|
2324
|
-
|
|
2325
|
-
|
|
2348
|
+
if (nested != 0) found = /\{\{\s*?\?\s*?(.+?)\s*?}}([\s\S]*?)\{\{\s*?\/\?\s*?}}/g.exec(content.slice(found.index + nested));
|
|
2349
|
+
const parts = found[2].split(/\{\{\s*?!\?\s*?/);
|
|
2350
|
+
let result = evaluate(found[1], d, false) ? parts[0] : "";
|
|
2351
|
+
if (!result) {
|
|
2352
|
+
for (let i = 1; i < parts.length; i++) {
|
|
2353
|
+
const [cond, body] = parts[i].split(/}}/);
|
|
2354
|
+
if (!cond.trim()) {
|
|
2355
|
+
result = body || "";
|
|
2356
|
+
break;
|
|
2357
|
+
}
|
|
2358
|
+
if (evaluate(cond, d, false)) {
|
|
2359
|
+
result = body || "";
|
|
2360
|
+
break;
|
|
2361
|
+
}
|
|
2362
|
+
}
|
|
2363
|
+
}
|
|
2364
|
+
content = content.replace(found[0], result);
|
|
2326
2365
|
}
|
|
2327
2366
|
while (!!(found = /\{\{\s*?<\s*?(.+?)\s*?}}/g.exec(content))) {
|
|
2328
|
-
|
|
2367
|
+
const t = await fetch2(found[1].trim());
|
|
2368
|
+
if (!t) throw new TemplateError(`Unknown imported template: ${found[1].trim()}`);
|
|
2369
|
+
content = content.replace(found[0], await renderTemplate(t, d, fetch2));
|
|
2329
2370
|
}
|
|
2330
2371
|
while (!!(found = /\{\{\s*?\*\s*?(.+?)\s+in\s+(.+?)\s*?}}([\s\S]*?)\{\{\s*?\/\*\s*?}}/g.exec(content))) {
|
|
2331
2372
|
const split = found[1].replaceAll(/[()\s]/g, "").split(",");
|
|
2332
2373
|
const element = split[0];
|
|
2333
2374
|
const index = split[1] || "index";
|
|
2334
|
-
const array = dotNotation(
|
|
2335
|
-
if (!array || typeof array != "object")
|
|
2336
|
-
throw new TemplateError(`Cannot iterate: ${found[2]}`);
|
|
2375
|
+
const array = dotNotation(d, found[2]);
|
|
2376
|
+
if (!array || typeof array != "object") throw new TemplateError(`Cannot iterate: ${found[2]}`);
|
|
2337
2377
|
let compiled = [];
|
|
2338
|
-
for (let i = 0; i < array.length; i++)
|
|
2339
|
-
compiled.push(renderTemplate(found[3], {
|
|
2340
|
-
...d,
|
|
2341
|
-
[element]: array[i],
|
|
2342
|
-
[index]: i
|
|
2343
|
-
}, fetch2));
|
|
2344
|
-
}
|
|
2378
|
+
for (let i = 0; i < array.length; i++)
|
|
2379
|
+
compiled.push(await renderTemplate(found[3], { ...d, [element]: array[i], [index]: i }, fetch2));
|
|
2345
2380
|
content = content.replace(found[0], compiled.join("\n"));
|
|
2346
2381
|
}
|
|
2347
2382
|
while (!!(found = /\{\{\s*([^<>\*\?!/}\s][^}]*?)\s*}}/g.exec(content))) {
|
|
2348
|
-
content = content.replace(found[0], evaluate(found[1].trim(), d)
|
|
2383
|
+
content = content.replace(found[0], evaluate(found[1].trim(), d) ?? "");
|
|
2349
2384
|
}
|
|
2350
2385
|
while (!!(found = /\{\{\s*?>\s*?(.+?):(.+?)\s*?}}([\s\S]*?)\{\{\s*?\/>\s*?}}/g.exec(content))) {
|
|
2351
|
-
|
|
2352
|
-
|
|
2386
|
+
const t = await fetch2(found[1].trim());
|
|
2387
|
+
if (!t) throw new TemplateError(`Unknown extended templated: ${found[1].trim()}`);
|
|
2388
|
+
content = content.replace(found[0], await renderTemplate(t, {
|
|
2389
|
+
...d,
|
|
2353
2390
|
[found[2].trim()]: found[3]
|
|
2354
2391
|
}, fetch2));
|
|
2355
2392
|
}
|
|
@@ -2608,6 +2645,7 @@ ${opts.message || this.desc}`;
|
|
|
2608
2645
|
exports2.fileBrowser = fileBrowser;
|
|
2609
2646
|
exports2.fileText = fileText;
|
|
2610
2647
|
exports2.findByProp = findByProp;
|
|
2648
|
+
exports2.findTemplateVars = findTemplateVars;
|
|
2611
2649
|
exports2.flattenArr = flattenArr;
|
|
2612
2650
|
exports2.flattenObj = flattenObj;
|
|
2613
2651
|
exports2.fn = fn;
|