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