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