@ztimson/utils 0.27.15 → 0.27.16

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
@@ -2293,6 +2293,68 @@ ${opts.message || this.desc}`;
2293
2293
  }).length == and.length;
2294
2294
  });
2295
2295
  }
2296
+ class TemplateError extends BadRequestError {
2297
+ }
2298
+ async function renderTemplate(template, data, fetch2) {
2299
+ let content = template, found;
2300
+ const now = /* @__PURE__ */ new Date(), d = {
2301
+ date: {
2302
+ day: now.getDate(),
2303
+ month: now.toLocaleString("default", { month: "long" }),
2304
+ year: now.getFullYear(),
2305
+ time: now.toLocaleTimeString(),
2306
+ format: formatDate
2307
+ },
2308
+ ...data || {}
2309
+ };
2310
+ if (!fetch2) fetch2 = (file) => {
2311
+ throw new TemplateError(`Unable to fetch template: ${file}`);
2312
+ };
2313
+ const evaluate = (code, data2, fatal = true) => {
2314
+ try {
2315
+ return Function("data", `Object.assign(this, data); return ${code};`)(data2);
2316
+ } catch {
2317
+ if (fatal) throw new TemplateError(`Failed to evaluate: ${code}`);
2318
+ else return false;
2319
+ }
2320
+ };
2321
+ while (!!(found = /\{\{\s*?\?\s*?(.+?)\s*?}}([\s\S]*?)(?:\{\{\s*?!\?\s*?}}([\s\S]*?))?\{\{\s*?\/\?\s*?}}/g.exec(content))) {
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.replaceAll(found[0], (evaluate(found[1], d, false) ? found[2] : found[3]) || "");
2326
+ }
2327
+ while (!!(found = /\{\{\s*?<\s*?(.+?)\s*?}}/g.exec(content))) {
2328
+ content = content.replaceAll(found[0], await renderTemplate(await fetch2(found[1].trim()), data, fetch2));
2329
+ }
2330
+ while (!!(found = /\{\{\s*?\*\s*?(.+?)\s+in\s+(.+?)\s*?}}([\s\S]*?)\{\{\s*?\/\*\s*?}}/g.exec(content))) {
2331
+ const split = found[1].replaceAll(/[()\s]/g, "").split(",");
2332
+ const element = split[0];
2333
+ 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]}`);
2337
+ 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
+ }
2345
+ content = content.replaceAll(found[0], compiled.join("\n"));
2346
+ }
2347
+ while (!!(found = /\{\{\s*([^<>\*\?!/}\s][^}]*?)\s*}}/g.exec(content))) {
2348
+ content = content.replaceAll(found[0], evaluate(found[1].trim(), d) || "");
2349
+ }
2350
+ 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,
2353
+ [found[2].trim()]: found[3]
2354
+ }, fetch2));
2355
+ }
2356
+ return content;
2357
+ }
2296
2358
  var dist = {};
2297
2359
  var persist = {};
2298
2360
  var hasRequiredPersist;
@@ -2516,6 +2578,7 @@ ${opts.message || this.desc}`;
2516
2578
  exports2.SYMBOL_LIST = SYMBOL_LIST;
2517
2579
  exports2.ServiceUnavailableError = ServiceUnavailableError;
2518
2580
  exports2.Table = Table;
2581
+ exports2.TemplateError = TemplateError;
2519
2582
  exports2.TypedEmitter = TypedEmitter;
2520
2583
  exports2.UnauthorizedError = UnauthorizedError;
2521
2584
  exports2.addUnique = addUnique;
@@ -2577,6 +2640,7 @@ ${opts.message || this.desc}`;
2577
2640
  exports2.randomHex = randomHex;
2578
2641
  exports2.randomString = randomString;
2579
2642
  exports2.randomStringBuilder = randomStringBuilder;
2643
+ exports2.renderTemplate = renderTemplate;
2580
2644
  exports2.reservedIp = reservedIp;
2581
2645
  exports2.search = search;
2582
2646
  exports2.sleep = sleep;