@ztimson/utils 0.27.19 → 0.27.20

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
@@ -2314,38 +2314,50 @@ async function renderTemplate(template, data, fetch2) {
2314
2314
  else return false;
2315
2315
  }
2316
2316
  };
2317
- while (!!(found = /\{\{\s*?\?\s*?(.+?)\s*?}}([\s\S]*?)(?:\{\{\s*?!\?\s*?}}([\s\S]*?))?\{\{\s*?\/\?\s*?}}/g.exec(content))) {
2317
+ while (!!(found = /\{\{\s*?\?\s*?(.+?)\s*?}}([\s\S]*?)\{\{\s*?\/\?\s*?}}/g.exec(content))) {
2318
2318
  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]) || "");
2319
+ if (nested != 0) found = /\{\{\s*?\?\s*?(.+?)\s*?}}([\s\S]*?)\{\{\s*?\/\?\s*?}}/g.exec(content.slice(found.index + nested));
2320
+ const parts = found[2].split(/\{\{\s*?!\?\s*?/);
2321
+ let result = evaluate(found[1], d, false) ? parts[0] : "";
2322
+ if (!result) {
2323
+ for (let i = 1; i < parts.length; i++) {
2324
+ const [cond, body] = parts[i].split(/}}/);
2325
+ if (!cond.trim()) {
2326
+ result = body || "";
2327
+ break;
2328
+ }
2329
+ if (evaluate(cond, d, false)) {
2330
+ result = body || "";
2331
+ break;
2332
+ }
2333
+ }
2334
+ }
2335
+ content = content.replace(found[0], result);
2322
2336
  }
2323
2337
  while (!!(found = /\{\{\s*?<\s*?(.+?)\s*?}}/g.exec(content))) {
2324
- content = content.replace(found[0], await renderTemplate(await fetch2(found[1].trim()), data, fetch2));
2338
+ const t = await fetch2(found[1].trim());
2339
+ if (!t) throw new TemplateError(`Unknown imported template: ${found[1].trim()}`);
2340
+ content = content.replace(found[0], await renderTemplate(t, d, fetch2));
2325
2341
  }
2326
2342
  while (!!(found = /\{\{\s*?\*\s*?(.+?)\s+in\s+(.+?)\s*?}}([\s\S]*?)\{\{\s*?\/\*\s*?}}/g.exec(content))) {
2327
2343
  const split = found[1].replaceAll(/[()\s]/g, "").split(",");
2328
2344
  const element = split[0];
2329
2345
  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]}`);
2346
+ const array = dotNotation(d, found[2]);
2347
+ if (!array || typeof array != "object") throw new TemplateError(`Cannot iterate: ${found[2]}`);
2333
2348
  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
- }
2349
+ for (let i = 0; i < array.length; i++)
2350
+ compiled.push(await renderTemplate(found[3], { ...d, [element]: array[i], [index]: i }, fetch2));
2341
2351
  content = content.replace(found[0], compiled.join("\n"));
2342
2352
  }
2343
2353
  while (!!(found = /\{\{\s*([^<>\*\?!/}\s][^}]*?)\s*}}/g.exec(content))) {
2344
- content = content.replace(found[0], evaluate(found[1].trim(), d) || "");
2354
+ content = content.replace(found[0], evaluate(found[1].trim(), d) ?? "");
2345
2355
  }
2346
2356
  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,
2357
+ const t = await fetch2(found[1].trim());
2358
+ if (!t) throw new TemplateError(`Unknown extended templated: ${found[1].trim()}`);
2359
+ content = content.replace(found[0], await renderTemplate(t, {
2360
+ ...d,
2349
2361
  [found[2].trim()]: found[3]
2350
2362
  }, fetch2));
2351
2363
  }