@ztimson/utils 0.27.20 → 0.28.1

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,64 @@ 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 excluded = /* @__PURE__ */ new Set(["true", "false", "null", "undefined"]);
2297
+ for (const loop of matchAll(html, /\{\{\s*?\*\s*?(.+?)\s+in\s+(.+?)\s*?}}/g)) {
2298
+ const [element, index = "index"] = loop[1].replaceAll(/[()\s]/g, "").split(",");
2299
+ excluded.add(element);
2300
+ excluded.add(index);
2301
+ const arrayRef = loop[2].trim();
2302
+ const arrayVar = arrayRef.split(".")[0].match(/^[a-zA-Z_$][a-zA-Z0-9_$]*/)?.[0];
2303
+ if (arrayVar && !excluded.has(arrayVar)) variables.add(arrayVar);
2304
+ }
2305
+ for (const ifStmt of matchAll(html, /\{\{\s*?[!]?\?\s*?([^}]+?)\s*?}}/g)) {
2306
+ const code = ifStmt[1].replace(/["'`][^"'`]*["'`]/g, "");
2307
+ const vars = code.match(/[a-zA-Z_$][a-zA-Z0-9_$.]+/g) || [];
2308
+ for (const v of vars) {
2309
+ const root = v.split(".")[0];
2310
+ if (!excluded.has(root)) variables.add(root);
2311
+ }
2312
+ }
2313
+ for (const ifBlock of matchAll(html, /\{\{\s*?\?\s*?.+?\s*?}}([\s\S]*?)\{\{\s*?\/\?\s*?}}/g)) {
2314
+ const content = ifBlock[1];
2315
+ const regex2 = /\{\{\s*([^<>\*\?!/}\s][^}]*?)\s*}}/g;
2316
+ let match2;
2317
+ while ((match2 = regex2.exec(content)) !== null) {
2318
+ const code = match2[1].trim().replace(/["'`][^"'`]*["'`]/g, "");
2319
+ const vars = code.match(/[a-zA-Z_$][a-zA-Z0-9_$.]+/g) || [];
2320
+ for (const v of vars) {
2321
+ const root = v.split(".")[0];
2322
+ if (!excluded.has(root)) variables.add(v);
2323
+ }
2324
+ }
2325
+ }
2326
+ const regex = /\{\{\s*([^<>\*\?!/}\s][^}]*?)\s*}}/g;
2327
+ let match;
2328
+ while ((match = regex.exec(html)) !== null) {
2329
+ const code = match[1].trim().replace(/["'`][^"'`]*["'`]/g, "");
2330
+ const vars = code.match(/[a-zA-Z_$][a-zA-Z0-9_$.]+/g) || [];
2331
+ for (const v of vars) {
2332
+ const root = v.split(".")[0];
2333
+ if (!excluded.has(root)) variables.add(v);
2334
+ }
2335
+ }
2336
+ const result = {};
2337
+ for (const path of variables) {
2338
+ const parts = path.split(".");
2339
+ let current = result;
2340
+ for (let i = 0; i < parts.length; i++) {
2341
+ const part = parts[i];
2342
+ if (i === parts.length - 1) {
2343
+ current[part] = "";
2344
+ } else {
2345
+ current[part] = current[part] || {};
2346
+ current = current[part];
2347
+ }
2348
+ }
2349
+ }
2350
+ return result;
2351
+ }
2294
2352
  async function renderTemplate(template, data, fetch2) {
2295
2353
  let content = template, found;
2296
2354
  const now = /* @__PURE__ */ new Date(), d = {
@@ -2617,6 +2675,7 @@ export {
2617
2675
  fileBrowser,
2618
2676
  fileText,
2619
2677
  findByProp,
2678
+ findTemplateVars,
2620
2679
  flattenArr,
2621
2680
  flattenObj,
2622
2681
  fn,