@ztimson/utils 0.28.0 → 0.28.2

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
@@ -2293,12 +2293,44 @@ class TemplateError extends BadRequestError {
2293
2293
  }
2294
2294
  function findTemplateVars(html) {
2295
2295
  const variables = /* @__PURE__ */ new Set();
2296
+ const arrays = /* @__PURE__ */ new Set();
2297
+ const excluded = /* @__PURE__ */ new Set(["true", "false", "null", "undefined"]);
2298
+ for (const loop of matchAll(html, /\{\{\s*?\*\s*?(.+?)\s+in\s+(.+?)\s*?}}/g)) {
2299
+ const [element, index = "index"] = loop[1].replaceAll(/[()\s]/g, "").split(",");
2300
+ excluded.add(element);
2301
+ excluded.add(index);
2302
+ const arrayVar = loop[2].trim();
2303
+ const root = arrayVar.split(".")[0];
2304
+ if (!excluded.has(root)) {
2305
+ variables.add(arrayVar);
2306
+ arrays.add(arrayVar);
2307
+ }
2308
+ }
2309
+ for (const ifStmt of matchAll(html, /\{\{\s*?[!]?\?\s*?([^}]+?)\s*?}}/g)) {
2310
+ const code = ifStmt[1].replace(/["'`][^"'`]*["'`]/g, "");
2311
+ const cleaned = code.replace(/([a-zA-Z_$][a-zA-Z0-9_$.]*)\s*\(/g, (_, v) => {
2312
+ const parts = v.split(".");
2313
+ return parts.length > 1 ? parts.slice(0, -1).join(".") + " " : "";
2314
+ });
2315
+ const vars = cleaned.match(/[a-zA-Z_$][a-zA-Z0-9_$.]+/g) || [];
2316
+ for (const v of vars) {
2317
+ const root = v.split(".")[0];
2318
+ if (!excluded.has(root)) variables.add(v);
2319
+ }
2320
+ }
2296
2321
  const regex = /\{\{\s*([^<>\*\?!/}\s][^}]*?)\s*}}/g;
2297
2322
  let match;
2298
2323
  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]);
2324
+ const code = match[1].trim().replace(/["'`][^"'`]*["'`]/g, "");
2325
+ const cleaned = code.replace(/([a-zA-Z_$][a-zA-Z0-9_$.]*)\s*\(/g, (_, v) => {
2326
+ const parts = v.split(".");
2327
+ return parts.length > 1 ? parts.slice(0, -1).join(".") + " " : "";
2328
+ });
2329
+ const vars = cleaned.match(/[a-zA-Z_$][a-zA-Z0-9_$.]+/g) || [];
2330
+ for (const v of vars) {
2331
+ const root = v.split(".")[0];
2332
+ if (!excluded.has(root)) variables.add(v);
2333
+ }
2302
2334
  }
2303
2335
  const result = {};
2304
2336
  for (const path of variables) {
@@ -2307,7 +2339,8 @@ function findTemplateVars(html) {
2307
2339
  for (let i = 0; i < parts.length; i++) {
2308
2340
  const part = parts[i];
2309
2341
  if (i === parts.length - 1) {
2310
- current[part] = "";
2342
+ const fullPath = parts.slice(0, i + 1).join(".");
2343
+ current[part] = arrays.has(fullPath) ? [] : "";
2311
2344
  } else {
2312
2345
  current[part] = current[part] || {};
2313
2346
  current = current[part];