@ztimson/utils 0.27.18 → 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.cjs CHANGED
@@ -2312,44 +2312,56 @@ ${opts.message || this.desc}`;
2312
2312
  };
2313
2313
  const evaluate = (code, data2, fatal = true) => {
2314
2314
  try {
2315
- return Function("data", `Object.assign(this, data); return ${code};`)(data2);
2315
+ return Function("data", `with(data) { return ${code}; }`)(data2);
2316
2316
  } catch {
2317
2317
  if (fatal) throw new TemplateError(`Failed to evaluate: ${code}`);
2318
2318
  else return false;
2319
2319
  }
2320
2320
  };
2321
- while (!!(found = /\{\{\s*?\?\s*?(.+?)\s*?}}([\s\S]*?)(?:\{\{\s*?!\?\s*?}}([\s\S]*?))?\{\{\s*?\/\?\s*?}}/g.exec(content))) {
2321
+ while (!!(found = /\{\{\s*?\?\s*?(.+?)\s*?}}([\s\S]*?)\{\{\s*?\/\?\s*?}}/g.exec(content))) {
2322
2322
  const nested = matchAll(found[0], /\{\{\s*?\?.+?}}/g).slice(-1)?.[0]?.index;
2323
- if (nested != 0)
2324
- found = /\{\{\s*?\?\s*?(.+?)\s*?}}([\s\S]*?)(?:\{\{\s*?!\?\s*?}}([\s\S]*?))?\{\{\s*?\/\?\s*?}}/g.exec(content.slice(found.index + nested));
2325
- content = content.replace(found[0], (evaluate(found[1], d, false) ? found[2] : found[3]) || "");
2323
+ if (nested != 0) found = /\{\{\s*?\?\s*?(.+?)\s*?}}([\s\S]*?)\{\{\s*?\/\?\s*?}}/g.exec(content.slice(found.index + nested));
2324
+ const parts = found[2].split(/\{\{\s*?!\?\s*?/);
2325
+ let result = evaluate(found[1], d, false) ? parts[0] : "";
2326
+ if (!result) {
2327
+ for (let i = 1; i < parts.length; i++) {
2328
+ const [cond, body] = parts[i].split(/}}/);
2329
+ if (!cond.trim()) {
2330
+ result = body || "";
2331
+ break;
2332
+ }
2333
+ if (evaluate(cond, d, false)) {
2334
+ result = body || "";
2335
+ break;
2336
+ }
2337
+ }
2338
+ }
2339
+ content = content.replace(found[0], result);
2326
2340
  }
2327
2341
  while (!!(found = /\{\{\s*?<\s*?(.+?)\s*?}}/g.exec(content))) {
2328
- content = content.replace(found[0], await renderTemplate(await fetch2(found[1].trim()), data, fetch2));
2342
+ const t = await fetch2(found[1].trim());
2343
+ if (!t) throw new TemplateError(`Unknown imported template: ${found[1].trim()}`);
2344
+ content = content.replace(found[0], await renderTemplate(t, d, fetch2));
2329
2345
  }
2330
2346
  while (!!(found = /\{\{\s*?\*\s*?(.+?)\s+in\s+(.+?)\s*?}}([\s\S]*?)\{\{\s*?\/\*\s*?}}/g.exec(content))) {
2331
2347
  const split = found[1].replaceAll(/[()\s]/g, "").split(",");
2332
2348
  const element = split[0];
2333
2349
  const index = split[1] || "index";
2334
- const array = dotNotation(data, found[2]);
2335
- if (!array || typeof array != "object")
2336
- throw new TemplateError(`Cannot iterate: ${found[2]}`);
2350
+ const array = dotNotation(d, found[2]);
2351
+ if (!array || typeof array != "object") throw new TemplateError(`Cannot iterate: ${found[2]}`);
2337
2352
  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
- }
2353
+ for (let i = 0; i < array.length; i++)
2354
+ compiled.push(await renderTemplate(found[3], { ...d, [element]: array[i], [index]: i }, fetch2));
2345
2355
  content = content.replace(found[0], compiled.join("\n"));
2346
2356
  }
2347
2357
  while (!!(found = /\{\{\s*([^<>\*\?!/}\s][^}]*?)\s*}}/g.exec(content))) {
2348
- content = content.replace(found[0], evaluate(found[1].trim(), d) || "");
2358
+ content = content.replace(found[0], evaluate(found[1].trim(), d) ?? "");
2349
2359
  }
2350
2360
  while (!!(found = /\{\{\s*?>\s*?(.+?):(.+?)\s*?}}([\s\S]*?)\{\{\s*?\/>\s*?}}/g.exec(content))) {
2351
- content = content.replace(found[0], await renderTemplate(await fetch2(found[1].trim()), {
2352
- ...data,
2361
+ const t = await fetch2(found[1].trim());
2362
+ if (!t) throw new TemplateError(`Unknown extended templated: ${found[1].trim()}`);
2363
+ content = content.replace(found[0], await renderTemplate(t, {
2364
+ ...d,
2353
2365
  [found[2].trim()]: found[3]
2354
2366
  }, fetch2));
2355
2367
  }