autoql-fe-utils 1.11.5 → 1.11.6

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.
@@ -16037,6 +16037,41 @@
16037
16037
  return array3.slice(0, index).concat(array3.slice(index + 1));
16038
16038
  };
16039
16039
 
16040
+ // src/safeRegex.ts
16041
+ var MAX_PATTERN_LENGTH = Number(process.env.REGEXP_GUARD_MAX_LEN) || 2e3;
16042
+ function isSimpleLiteral(pattern) {
16043
+ return typeof pattern === "string" && /^[A-Za-z0-9 _\-\.,:\/]+$/.test(pattern);
16044
+ }
16045
+ function escapeRegExp(str) {
16046
+ return String(str).replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
16047
+ }
16048
+ function compileSafeRegex(pattern, flags = "") {
16049
+ if (typeof pattern !== "string") return null;
16050
+ if (pattern.length > MAX_PATTERN_LENGTH) return null;
16051
+ try {
16052
+ return new RegExp(pattern, flags);
16053
+ } catch (e) {
16054
+ return null;
16055
+ }
16056
+ }
16057
+ function safeTest(pattern, input, flags = "") {
16058
+ if (pattern == null || input == null) return false;
16059
+ const strInput = String(input);
16060
+ const re2 = compileSafeRegex(String(pattern), flags);
16061
+ if (re2) {
16062
+ try {
16063
+ return re2.test(strInput);
16064
+ } catch (e) {
16065
+ return false;
16066
+ }
16067
+ }
16068
+ if (isSimpleLiteral(pattern)) {
16069
+ const needle = String(pattern);
16070
+ return flags.includes("i") ? strInput.toLowerCase().includes(needle.toLowerCase()) : strInput.includes(needle);
16071
+ }
16072
+ return false;
16073
+ }
16074
+
16040
16075
  // src/HelperFns/filterEngine.ts
16041
16076
  var FilterOperatorEnum = /* @__PURE__ */ ((FilterOperatorEnum2) => {
16042
16077
  FilterOperatorEnum2["LIKE"] = "like";
@@ -16085,8 +16120,7 @@
16085
16120
  var greaterThanMatcher = (cell, cond) => Number(cell) > Number(cond.value);
16086
16121
  var greaterThanEqMatcher = (cell, cond) => Number(cell) >= Number(cond.value);
16087
16122
  var regexMatcher = (cell, cond) => {
16088
- const reg = new RegExp(String(cond.value));
16089
- return reg.test(String(cell));
16123
+ return safeTest(cond.value, cell);
16090
16124
  };
16091
16125
  var betweenMatcher = (cell, cond) => {
16092
16126
  if (cell == null) return false;
@@ -16142,9 +16176,6 @@
16142
16176
  function isValidFilterType(t) {
16143
16177
  return FILTER_TYPES.includes(t);
16144
16178
  }
16145
- function escapeRegExp(str) {
16146
- return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
16147
- }
16148
16179
  function parseLocaleNumber(value, locale3) {
16149
16180
  var _a, _b;
16150
16181
  if (typeof value === "number") return value;
@@ -16156,10 +16187,20 @@
16156
16187
  const group = (_a = parts.find((p) => p.type === "group")) == null ? void 0 : _a.value;
16157
16188
  const decimal = (_b = parts.find((p) => p.type === "decimal")) == null ? void 0 : _b.value;
16158
16189
  if (group) {
16159
- str = str.replace(new RegExp(escapeRegExp(group), "g"), "");
16190
+ const grpRe = compileSafeRegex(escapeRegExp(group), "g");
16191
+ if (grpRe) {
16192
+ str = str.replace(grpRe, "");
16193
+ } else {
16194
+ str = str.split(group).join("");
16195
+ }
16160
16196
  }
16161
16197
  if (decimal && decimal !== ".") {
16162
- str = str.replace(new RegExp(escapeRegExp(decimal), "g"), ".");
16198
+ const decRe = compileSafeRegex(escapeRegExp(decimal), "g");
16199
+ if (decRe) {
16200
+ str = str.replace(decRe, ".");
16201
+ } else {
16202
+ str = str.split(decimal).join(".");
16203
+ }
16163
16204
  }
16164
16205
  } catch (e) {
16165
16206
  str = str.replace(/[,']/g, "");
@@ -16180,7 +16221,9 @@
16180
16221
  const trimmed = raw.trim();
16181
16222
  if (trimmed.length === 0) return { operator: defaultOperator, value: "", type };
16182
16223
  const opToken = `${"between" /* BETWEEN */}|[<>!=]=?|not_like`;
16183
- const opMatch = new RegExp(`^(${opToken})\\s*(.*)$`, "i").exec(trimmed);
16224
+ const opPattern = `^(${opToken})\\s*(.*)$`;
16225
+ const opRe = compileSafeRegex(opPattern, "i");
16226
+ const opMatch = opRe ? opRe.exec(trimmed) : null;
16184
16227
  if (opMatch) {
16185
16228
  let op = String((_a = opMatch[1]) != null ? _a : "").toLowerCase();
16186
16229
  const rest = (_b = opMatch[2]) != null ? _b : "";
@@ -30680,8 +30723,12 @@
30680
30723
  };
30681
30724
  var getSampleQueryRegex = (suggestionValues) => {
30682
30725
  const valueArray = Object.keys(suggestionValues);
30683
- const valueRegexArray = valueArray.map((value) => `\\b${value}\\b`);
30684
- const valueRegex = new RegExp(valueRegexArray.join("|"), "gm");
30726
+ const valueRegexArray = valueArray.map((value) => {
30727
+ const esc = String(value).replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
30728
+ return `\\b${esc}\\b`;
30729
+ });
30730
+ const pattern = valueRegexArray.join("|");
30731
+ const valueRegex = compileSafeRegex(pattern, "gm");
30685
30732
  return valueRegex;
30686
30733
  };
30687
30734
  var getSampleQueryText = (query, values = {}) => {
@@ -30695,9 +30742,14 @@
30695
30742
  var _a;
30696
30743
  const chunk = values[valueKey];
30697
30744
  const replacementText = (_a = chunk == null ? void 0 : chunk.replacement) == null ? void 0 : _a.format_txt;
30698
- const valueKeyRegex = new RegExp(`\\b${valueKey}\\b`);
30745
+ const pattern = `\\b${valueKey}\\b`;
30746
+ const valueKeyRegex = compileSafeRegex(pattern);
30699
30747
  if (replacementText) {
30700
- queryText = queryText.replace(valueKeyRegex, replacementText);
30748
+ if (valueKeyRegex) {
30749
+ queryText = queryText.replace(valueKeyRegex, replacementText);
30750
+ } else if (isSimpleLiteral(valueKey)) {
30751
+ queryText = String(queryText).split(valueKey).join(replacementText);
30752
+ }
30701
30753
  }
30702
30754
  });
30703
30755
  return queryText;
@@ -30789,6 +30841,7 @@
30789
30841
  }
30790
30842
  };
30791
30843
  var getSampleQueryChunks = (query, values = {}) => {
30844
+ var _a, _b;
30792
30845
  try {
30793
30846
  const valueArray = Object.keys(values);
30794
30847
  if (!(valueArray == null ? void 0 : valueArray.length)) {
@@ -30800,22 +30853,57 @@
30800
30853
  }
30801
30854
  ];
30802
30855
  }
30803
- const valueRegexArray = valueArray.map((value) => `(\\b${value}\\b)`);
30804
- const valueRegex = new RegExp(valueRegexArray.join("|"), "gm");
30805
30856
  let splitStrArray = [query];
30806
- if (valueArray == null ? void 0 : valueArray.length) {
30807
- splitStrArray = query.split(valueRegex);
30857
+ if (valueArray.length < 500) {
30858
+ const valueRegexArray = valueArray.map((value) => {
30859
+ const esc = String(value).replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
30860
+ return `(\\b${esc}\\b)`;
30861
+ });
30862
+ const pattern = valueRegexArray.join("|");
30863
+ const valueRegex = compileSafeRegex(pattern, "gm");
30864
+ if (valueRegex) {
30865
+ splitStrArray = query.split(valueRegex);
30866
+ }
30867
+ }
30868
+ if (splitStrArray.length === 1) {
30869
+ const result = [];
30870
+ let remaining = query;
30871
+ const sortedValues = [...valueArray].sort((a, b) => b.length - a.length);
30872
+ for (const value of sortedValues) {
30873
+ const escaped = String(value).replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
30874
+ const re2 = compileSafeRegex(`\\b${escaped}\\b`, "i");
30875
+ let match = null;
30876
+ let matchedText = "";
30877
+ if (re2) {
30878
+ match = re2.exec(remaining);
30879
+ matchedText = (_a = match == null ? void 0 : match[0]) != null ? _a : "";
30880
+ } else if (isSimpleLiteral(value)) {
30881
+ const idx = remaining.toLowerCase().indexOf(String(value).toLowerCase());
30882
+ if (idx >= 0) {
30883
+ match = [String(value)];
30884
+ matchedText = remaining.substr(idx, String(value).length);
30885
+ match.index = idx;
30886
+ }
30887
+ }
30888
+ if (match) {
30889
+ const matchIndex = (_b = match.index) != null ? _b : 0;
30890
+ result.push(remaining.substring(0, matchIndex), matchedText || match[0]);
30891
+ remaining = remaining.substring(matchIndex + (matchedText || match[0]).length);
30892
+ }
30893
+ }
30894
+ if (remaining) result.push(remaining);
30895
+ splitStrArray = result;
30808
30896
  }
30809
30897
  const chunkedSuggestion = [];
30810
30898
  splitStrArray.forEach((key) => {
30811
- var _a, _b;
30812
- const replacement = (_a = values[key]) == null ? void 0 : _a.replacement;
30899
+ var _a2, _b2;
30900
+ const replacement = (_a2 = values[key]) == null ? void 0 : _a2.replacement;
30813
30901
  const type = getSampleQueryReplacementType(key, replacement);
30814
30902
  const name = key == null ? void 0 : key.trim();
30815
30903
  if (!type || !name) {
30816
30904
  return;
30817
30905
  }
30818
- let value = (_b = replacement == null ? void 0 : replacement.format_txt) == null ? void 0 : _b.trim();
30906
+ let value = (_b2 = replacement == null ? void 0 : replacement.format_txt) == null ? void 0 : _b2.trim();
30819
30907
  if (type == "TEXT" /* SAMPLE_QUERY_TEXT_TYPE */) {
30820
30908
  value = name;
30821
30909
  }