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