@usertour/helpers 0.0.18 → 0.0.20

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.
@@ -24,68 +24,87 @@ __export(url_exports, {
24
24
  isMatchUrlPattern: () => isMatchUrlPattern
25
25
  });
26
26
  module.exports = __toCommonJS(url_exports);
27
- var parseUrl = (url) => {
28
- const urlPatterns = url.match(/^(([a-z\d]+):\/\/)?([^/?#]+)?(\/[^?#]*)?(\?([^#]*))?(#.*)?$/i);
29
- if (!urlPatterns) {
27
+
28
+ // src/conditions/url-v2.ts
29
+ function parseUrl(url) {
30
+ const match = url.match(/^(([a-z\d]+):\/\/)?([^/?#]+)?(\/[^?#]*)?(\?([^#]*))?(#.*)?$/i);
31
+ if (!match)
30
32
  return null;
31
- }
32
- const [, , scheme = "", domain = "", path = "", , query = "", fragment = ""] = urlPatterns;
33
- return { scheme, domain, path, query, fragment };
34
- };
35
- var replaceWildcard = (input, s1, s2) => {
36
- const withSpecialWords = replaceSpecialWords(input);
37
- const withWildcard = withSpecialWords.replace(/\\\*/g, `${s1}*`);
38
- if (!s2) {
39
- return withWildcard;
40
- }
41
- return withWildcard.replace(/:[a-z0-9_]+/g, `[^${s2}]+`);
42
- };
43
- var replaceSpecialWords = (str) => {
33
+ const [, , scheme, domain, path, , query, fragment] = match;
34
+ return {
35
+ scheme: scheme || "",
36
+ domain: domain || "",
37
+ path: path || "",
38
+ query: query || "",
39
+ fragment: fragment || ""
40
+ };
41
+ }
42
+ function escapeRegex(str) {
44
43
  return str.replace(/[-/\\^$*+?.()|[\]{}]/g, "\\$&");
45
- };
46
- var parsePattern = (pattern) => {
47
- if (!pattern || !pattern.trim()) {
48
- return null;
44
+ }
45
+ function processUrlPattern(pattern, wildcardReplacement, paramReplacement) {
46
+ let processed = escapeRegex(pattern);
47
+ processed = processed.replace(/\\\*/g, `${wildcardReplacement}*`);
48
+ if (paramReplacement) {
49
+ processed = processed.replace(/:[a-zA-Z0-9_]+/g, `[^${paramReplacement}]+`);
49
50
  }
50
- const _pattern = parseUrl(pattern);
51
- if (!_pattern) {
52
- console.error("Invalid URL pattern:", pattern);
51
+ return processed;
52
+ }
53
+ function urlPatternToRegex(pattern) {
54
+ const parsed = parseUrl(pattern);
55
+ if (!parsed) {
53
56
  return null;
54
57
  }
55
- const { scheme, domain, path, query, fragment } = _pattern;
56
- const _scheme = scheme ? replaceSpecialWords(scheme) : "[a-z\\d]+";
57
- const _domain = domain ? replaceWildcard(domain, "[^/]", ".") : "[^/]*";
58
- const _fragment = fragment ? replaceWildcard(fragment, ".", "/") : "(#.*)?";
59
- const _path = path ? replaceWildcard(path, "[^?#]", "/") : "/[^?#]*";
60
- let _query = "(\\?[^#]*)?";
58
+ const { scheme, domain, path, query, fragment } = parsed;
59
+ const schemePattern = scheme ? escapeRegex(scheme) : "[a-z\\d]+";
60
+ const domainPattern = domain ? processUrlPattern(domain, "[^/]", ".") : "[^/]*";
61
+ const portPattern = "(:\\d+)?";
62
+ const pathPattern = path ? processUrlPattern(path, "[^?#]", "/") : "/[^?#]*";
63
+ let queryPattern;
61
64
  if (query) {
65
+ queryPattern = "";
62
66
  new URLSearchParams(query).forEach((value, key) => {
63
- const _str = value === "" ? "=?" : value === "*" ? "(=[^&#]*)?" : `=${replaceWildcard(value, "[^#]")}`;
64
- _query += `(?=.*[?&]${replaceSpecialWords(key)}${_str}([&#]|$))`;
67
+ let valuePattern;
68
+ if (value === "") {
69
+ valuePattern = "=?";
70
+ } else if (value === "*") {
71
+ valuePattern = "(=[^&#]*)?";
72
+ } else {
73
+ const encodedValue = value.split(/\*/g).map((part) => encodeURI(part)).join("*");
74
+ valuePattern = `=${processUrlPattern(encodedValue, "[^#]")}`;
75
+ }
76
+ queryPattern += `(?=.*[?&]${escapeRegex(key)}${valuePattern}([&#]|$))`;
65
77
  });
66
- _query += "\\?[^#]*";
78
+ queryPattern += "\\?[^#]*";
79
+ } else {
80
+ queryPattern = "(\\?[^#]*)?";
67
81
  }
68
- return new RegExp(`^${_scheme}://${_domain}(:\\d+)?${_path}${_query}${_fragment}$`);
69
- };
70
- var isMatchUrlPattern = (_url, includes, excludes) => {
71
- const isMatchIncludesConditions = includes.length > 0 ? includes.some((_include) => {
72
- const reg = parsePattern(_include);
73
- if (reg) {
74
- return reg.test(_url);
75
- }
82
+ const fragmentPattern = fragment ? processUrlPattern(fragment, ".", "/") : "(#.*)?";
83
+ return new RegExp(
84
+ `^${schemePattern}://${domainPattern}${portPattern}${pathPattern}${queryPattern}${fragmentPattern}$`
85
+ );
86
+ }
87
+ function matchUrlPattern(urlPattern, url) {
88
+ if (urlPattern.includes.length === 0 && urlPattern.excludes.length === 0) {
76
89
  return false;
77
- }) : true;
78
- const isMatchExcludesConditions = excludes.length > 0 ? excludes.some((_exclude) => {
79
- const reg = parsePattern(_exclude);
80
- if (reg) {
81
- return reg.test(_url);
82
- }
83
- return false;
84
- }) : false;
85
- return isMatchIncludesConditions && !isMatchExcludesConditions;
90
+ }
91
+ const matchesInclude = urlPattern.includes.length === 0 || urlPattern.includes.some((includePattern) => {
92
+ const regex = urlPatternToRegex(includePattern);
93
+ return regex && url.match(regex);
94
+ });
95
+ const matchesExclude = urlPattern.excludes.some((excludePattern) => {
96
+ const regex = urlPatternToRegex(excludePattern);
97
+ return regex && url.match(regex);
98
+ });
99
+ return matchesInclude && !matchesExclude;
100
+ }
101
+
102
+ // src/conditions/url.ts
103
+ var isMatchUrlPattern = (_url, includes, excludes) => {
104
+ return matchUrlPattern({ includes, excludes }, _url);
86
105
  };
87
106
  var evaluateUrlCondition = (rules, url) => {
88
- const { excludes, includes } = rules.data;
107
+ const { excludes = [], includes = [] } = rules.data || {};
89
108
  return isMatchUrlPattern(url, includes, excludes);
90
109
  };
91
110
  // Annotate the CommonJS export names for ESM import in node:
@@ -1,7 +1,8 @@
1
1
  import {
2
2
  evaluateUrlCondition,
3
3
  isMatchUrlPattern
4
- } from "../chunk-BC7KXBMF.js";
4
+ } from "../chunk-YYIGUZNZ.js";
5
+ import "../chunk-PAESAL23.js";
5
6
  import "../chunk-XEO3YXBM.js";
6
7
  export {
7
8
  evaluateUrlCondition,
package/dist/index.cjs CHANGED
@@ -55,6 +55,7 @@ __export(src_exports, {
55
55
  evaluateTimeCondition: () => evaluateTimeCondition,
56
56
  evaluateUrlCondition: () => evaluateUrlCondition,
57
57
  fetch: () => fetch,
58
+ filterConditionsByType: () => filterConditionsByType,
58
59
  formatDate: () => formatDate,
59
60
  generateAutoStateColors: () => generateAutoStateColors,
60
61
  getAuthToken: () => getAuthToken,
@@ -79,6 +80,7 @@ __export(src_exports, {
79
80
  hexToRgb: () => hexToRgb,
80
81
  isArray: () => isArray,
81
82
  isBoolean: () => isBoolean,
83
+ isConditionsActived: () => isConditionsActived,
82
84
  isDark: () => isDark,
83
85
  isDate: () => isDate,
84
86
  isDocument: () => isDocument,
@@ -961,124 +963,89 @@ var getRandomColor = () => {
961
963
  };
962
964
 
963
965
  // src/conditions/condition.ts
966
+ var import_types5 = require("@usertour/types");
964
967
  var import_fast_deep_equal = __toESM(require("fast-deep-equal"), 1);
965
- var compareConditionsItem = (item1, item2) => {
966
- const { data = {}, ...others1 } = item1;
967
- const { data: data2 = {}, ...others2 } = item2;
968
- if (!(0, import_fast_deep_equal.default)(others2, others1)) {
969
- return false;
970
- }
971
- for (const key in data) {
972
- if (!(0, import_fast_deep_equal.default)(data[key], data2[key])) {
973
- return false;
974
- }
975
- }
976
- return true;
977
- };
978
- var conditionsIsSame = (rr1, rr2) => {
979
- const r1 = [...rr1];
980
- const r2 = [...rr2];
981
- if (r1.length === 0 && r2.length === 0) {
982
- return true;
983
- }
984
- if (r1.length !== r2.length) {
985
- return false;
986
- }
987
- const group1 = r1.filter((item) => item.type === "group");
988
- const group2 = r2.filter((item) => item.type === "group");
989
- if (group1.length !== group2.length) {
990
- return false;
991
- }
992
- for (let index = 0; index < r1.length; index++) {
993
- const item1 = r1[index];
994
- const item2 = r2[index];
995
- if (!item1 || !item2) {
996
- return false;
997
- }
998
- if (item1.type === "group") {
999
- if (!item2.conditions) {
1000
- return false;
1001
- }
1002
- const c1 = item1.conditions;
1003
- const c2 = item2.conditions;
1004
- if (item1.operators !== item2.operators) {
1005
- return false;
1006
- }
1007
- if (!conditionsIsSame(c1, c2)) {
1008
- return false;
1009
- }
1010
- } else {
1011
- if (!compareConditionsItem(item1, item2)) {
1012
- return false;
1013
- }
1014
- }
1015
- }
1016
- return true;
1017
- };
1018
968
 
1019
- // src/conditions/url.ts
1020
- var parseUrl = (url) => {
1021
- const urlPatterns = url.match(/^(([a-z\d]+):\/\/)?([^/?#]+)?(\/[^?#]*)?(\?([^#]*))?(#.*)?$/i);
1022
- if (!urlPatterns) {
969
+ // src/conditions/url-v2.ts
970
+ function parseUrl(url) {
971
+ const match = url.match(/^(([a-z\d]+):\/\/)?([^/?#]+)?(\/[^?#]*)?(\?([^#]*))?(#.*)?$/i);
972
+ if (!match)
1023
973
  return null;
1024
- }
1025
- const [, , scheme = "", domain = "", path = "", , query = "", fragment = ""] = urlPatterns;
1026
- return { scheme, domain, path, query, fragment };
1027
- };
1028
- var replaceWildcard = (input, s1, s2) => {
1029
- const withSpecialWords = replaceSpecialWords(input);
1030
- const withWildcard = withSpecialWords.replace(/\\\*/g, `${s1}*`);
1031
- if (!s2) {
1032
- return withWildcard;
1033
- }
1034
- return withWildcard.replace(/:[a-z0-9_]+/g, `[^${s2}]+`);
1035
- };
1036
- var replaceSpecialWords = (str) => {
974
+ const [, , scheme, domain, path, , query, fragment] = match;
975
+ return {
976
+ scheme: scheme || "",
977
+ domain: domain || "",
978
+ path: path || "",
979
+ query: query || "",
980
+ fragment: fragment || ""
981
+ };
982
+ }
983
+ function escapeRegex(str) {
1037
984
  return str.replace(/[-/\\^$*+?.()|[\]{}]/g, "\\$&");
1038
- };
1039
- var parsePattern = (pattern) => {
1040
- if (!pattern || !pattern.trim()) {
1041
- return null;
985
+ }
986
+ function processUrlPattern(pattern, wildcardReplacement, paramReplacement) {
987
+ let processed = escapeRegex(pattern);
988
+ processed = processed.replace(/\\\*/g, `${wildcardReplacement}*`);
989
+ if (paramReplacement) {
990
+ processed = processed.replace(/:[a-zA-Z0-9_]+/g, `[^${paramReplacement}]+`);
1042
991
  }
1043
- const _pattern = parseUrl(pattern);
1044
- if (!_pattern) {
1045
- console.error("Invalid URL pattern:", pattern);
992
+ return processed;
993
+ }
994
+ function urlPatternToRegex(pattern) {
995
+ const parsed = parseUrl(pattern);
996
+ if (!parsed) {
1046
997
  return null;
1047
998
  }
1048
- const { scheme, domain, path, query, fragment } = _pattern;
1049
- const _scheme = scheme ? replaceSpecialWords(scheme) : "[a-z\\d]+";
1050
- const _domain = domain ? replaceWildcard(domain, "[^/]", ".") : "[^/]*";
1051
- const _fragment = fragment ? replaceWildcard(fragment, ".", "/") : "(#.*)?";
1052
- const _path = path ? replaceWildcard(path, "[^?#]", "/") : "/[^?#]*";
1053
- let _query = "(\\?[^#]*)?";
999
+ const { scheme, domain, path, query, fragment } = parsed;
1000
+ const schemePattern = scheme ? escapeRegex(scheme) : "[a-z\\d]+";
1001
+ const domainPattern = domain ? processUrlPattern(domain, "[^/]", ".") : "[^/]*";
1002
+ const portPattern = "(:\\d+)?";
1003
+ const pathPattern = path ? processUrlPattern(path, "[^?#]", "/") : "/[^?#]*";
1004
+ let queryPattern;
1054
1005
  if (query) {
1006
+ queryPattern = "";
1055
1007
  new URLSearchParams(query).forEach((value, key) => {
1056
- const _str = value === "" ? "=?" : value === "*" ? "(=[^&#]*)?" : `=${replaceWildcard(value, "[^#]")}`;
1057
- _query += `(?=.*[?&]${replaceSpecialWords(key)}${_str}([&#]|$))`;
1008
+ let valuePattern;
1009
+ if (value === "") {
1010
+ valuePattern = "=?";
1011
+ } else if (value === "*") {
1012
+ valuePattern = "(=[^&#]*)?";
1013
+ } else {
1014
+ const encodedValue = value.split(/\*/g).map((part) => encodeURI(part)).join("*");
1015
+ valuePattern = `=${processUrlPattern(encodedValue, "[^#]")}`;
1016
+ }
1017
+ queryPattern += `(?=.*[?&]${escapeRegex(key)}${valuePattern}([&#]|$))`;
1058
1018
  });
1059
- _query += "\\?[^#]*";
1019
+ queryPattern += "\\?[^#]*";
1020
+ } else {
1021
+ queryPattern = "(\\?[^#]*)?";
1060
1022
  }
1061
- return new RegExp(`^${_scheme}://${_domain}(:\\d+)?${_path}${_query}${_fragment}$`);
1062
- };
1063
- var isMatchUrlPattern = (_url, includes, excludes) => {
1064
- const isMatchIncludesConditions = includes.length > 0 ? includes.some((_include) => {
1065
- const reg = parsePattern(_include);
1066
- if (reg) {
1067
- return reg.test(_url);
1068
- }
1069
- return false;
1070
- }) : true;
1071
- const isMatchExcludesConditions = excludes.length > 0 ? excludes.some((_exclude) => {
1072
- const reg = parsePattern(_exclude);
1073
- if (reg) {
1074
- return reg.test(_url);
1075
- }
1023
+ const fragmentPattern = fragment ? processUrlPattern(fragment, ".", "/") : "(#.*)?";
1024
+ return new RegExp(
1025
+ `^${schemePattern}://${domainPattern}${portPattern}${pathPattern}${queryPattern}${fragmentPattern}$`
1026
+ );
1027
+ }
1028
+ function matchUrlPattern(urlPattern, url) {
1029
+ if (urlPattern.includes.length === 0 && urlPattern.excludes.length === 0) {
1076
1030
  return false;
1077
- }) : false;
1078
- return isMatchIncludesConditions && !isMatchExcludesConditions;
1031
+ }
1032
+ const matchesInclude = urlPattern.includes.length === 0 || urlPattern.includes.some((includePattern) => {
1033
+ const regex = urlPatternToRegex(includePattern);
1034
+ return regex && url.match(regex);
1035
+ });
1036
+ const matchesExclude = urlPattern.excludes.some((excludePattern) => {
1037
+ const regex = urlPatternToRegex(excludePattern);
1038
+ return regex && url.match(regex);
1039
+ });
1040
+ return matchesInclude && !matchesExclude;
1041
+ }
1042
+
1043
+ // src/conditions/url.ts
1044
+ var isMatchUrlPattern = (_url, includes, excludes) => {
1045
+ return matchUrlPattern({ includes, excludes }, _url);
1079
1046
  };
1080
1047
  var evaluateUrlCondition = (rules, url) => {
1081
- const { excludes, includes } = rules.data;
1048
+ const { excludes = [], includes = [] } = rules.data || {};
1082
1049
  return isMatchUrlPattern(url, includes, excludes);
1083
1050
  };
1084
1051
 
@@ -1289,6 +1256,41 @@ function evaluateDateTimeCondition(logic, actualValue, expectedValue) {
1289
1256
  return false;
1290
1257
  }
1291
1258
  }
1259
+
1260
+ // src/conditions/condition.ts
1261
+ var conditionsIsSame = (rr1, rr2) => {
1262
+ return (0, import_fast_deep_equal.default)(rr1, rr2);
1263
+ };
1264
+ var isConditionsActived = (conditions) => {
1265
+ if (!conditions || conditions.length === 0) {
1266
+ return false;
1267
+ }
1268
+ const operator = conditions[0].operators;
1269
+ const actives = conditions.filter((rule) => {
1270
+ if (!rule.conditions) {
1271
+ return rule.actived;
1272
+ }
1273
+ return isConditionsActived(rule.conditions);
1274
+ });
1275
+ return operator === "and" ? actives.length === conditions.length : actives.length > 0;
1276
+ };
1277
+ var filterConditionsByType = (conditions, allowedTypes) => {
1278
+ return conditions.filter((condition) => {
1279
+ if (condition.type === "group" && condition.conditions) {
1280
+ const filteredGroupConditions = filterConditionsByType(condition.conditions, allowedTypes);
1281
+ return filteredGroupConditions.length > 0;
1282
+ }
1283
+ return allowedTypes.includes(condition.type);
1284
+ }).map((condition) => {
1285
+ if (condition.type === "group" && condition.conditions) {
1286
+ return {
1287
+ ...condition,
1288
+ conditions: filterConditionsByType(condition.conditions, allowedTypes)
1289
+ };
1290
+ }
1291
+ return condition;
1292
+ });
1293
+ };
1292
1294
  // Annotate the CommonJS export names for ESM import in node:
1293
1295
  0 && (module.exports = {
1294
1296
  AbortController,
@@ -1311,6 +1313,7 @@ function evaluateDateTimeCondition(logic, actualValue, expectedValue) {
1311
1313
  evaluateTimeCondition,
1312
1314
  evaluateUrlCondition,
1313
1315
  fetch,
1316
+ filterConditionsByType,
1314
1317
  formatDate,
1315
1318
  generateAutoStateColors,
1316
1319
  getAuthToken,
@@ -1335,6 +1338,7 @@ function evaluateDateTimeCondition(logic, actualValue, expectedValue) {
1335
1338
  hexToRgb,
1336
1339
  isArray,
1337
1340
  isBoolean,
1341
+ isConditionsActived,
1338
1342
  isDark,
1339
1343
  isDate,
1340
1344
  isDocument,
package/dist/index.d.cts CHANGED
@@ -9,7 +9,7 @@ export { buildConfig, defaultContentConfig, isPublishedAtLeastOneEnvironment, is
9
9
  export { deepClone } from './utils.cjs';
10
10
  export { generateAutoStateColors, hexToHSLString, hexToRGBStr } from './color.cjs';
11
11
  export { absoluteUrl, cn, cuid, evalCode, formatDate, getRandomColor, hexToRgb, isDark, uuidV4 } from './helper.cjs';
12
- export { conditionsIsSame } from './conditions/condition.cjs';
12
+ export { conditionsIsSame, filterConditionsByType, isConditionsActived } from './conditions/condition.cjs';
13
13
  export { evaluateUrlCondition, isMatchUrlPattern } from './conditions/url.cjs';
14
14
  export { evaluateTimeCondition } from './conditions/time.cjs';
15
15
  export { evaluateAttributeCondition } from './conditions/attribute.cjs';
package/dist/index.d.ts CHANGED
@@ -9,7 +9,7 @@ export { buildConfig, defaultContentConfig, isPublishedAtLeastOneEnvironment, is
9
9
  export { deepClone } from './utils.js';
10
10
  export { generateAutoStateColors, hexToHSLString, hexToRGBStr } from './color.js';
11
11
  export { absoluteUrl, cn, cuid, evalCode, formatDate, getRandomColor, hexToRgb, isDark, uuidV4 } from './helper.js';
12
- export { conditionsIsSame } from './conditions/condition.js';
12
+ export { conditionsIsSame, filterConditionsByType, isConditionsActived } from './conditions/condition.js';
13
13
  export { evaluateUrlCondition, isMatchUrlPattern } from './conditions/url.js';
14
14
  export { evaluateTimeCondition } from './conditions/time.js';
15
15
  export { evaluateAttributeCondition } from './conditions/attribute.js';
package/dist/index.js CHANGED
@@ -1,8 +1,4 @@
1
1
  import "./chunk-7ODE2AIC.js";
2
- import {
3
- evaluateUrlCondition,
4
- isMatchUrlPattern
5
- } from "./chunk-BC7KXBMF.js";
6
2
  import {
7
3
  isUrl
8
4
  } from "./chunk-ZNFXGN3M.js";
@@ -12,13 +8,20 @@ import {
12
8
  import {
13
9
  deepClone
14
10
  } from "./chunk-2AEGAICC.js";
15
- import {
16
- evaluateAttributeCondition
17
- } from "./chunk-PBZSPV5R.js";
18
11
  import {
19
12
  conditionsIsSame,
13
+ filterConditionsByType,
14
+ isConditionsActived,
20
15
  isEqual
21
- } from "./chunk-YOFQHQ7D.js";
16
+ } from "./chunk-TB7JGI2Q.js";
17
+ import {
18
+ evaluateUrlCondition,
19
+ isMatchUrlPattern
20
+ } from "./chunk-YYIGUZNZ.js";
21
+ import "./chunk-PAESAL23.js";
22
+ import {
23
+ evaluateAttributeCondition
24
+ } from "./chunk-PBZSPV5R.js";
22
25
  import {
23
26
  evaluateTimeCondition
24
27
  } from "./chunk-CEK3SCQO.js";
@@ -128,6 +131,7 @@ export {
128
131
  evaluateTimeCondition,
129
132
  evaluateUrlCondition,
130
133
  fetch,
134
+ filterConditionsByType,
131
135
  formatDate,
132
136
  generateAutoStateColors,
133
137
  getAuthToken,
@@ -152,6 +156,7 @@ export {
152
156
  hexToRgb,
153
157
  isArray,
154
158
  isBoolean,
159
+ isConditionsActived,
155
160
  isDark,
156
161
  isDate,
157
162
  isDocument,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@usertour/helpers",
3
- "version": "0.0.18",
3
+ "version": "0.0.20",
4
4
  "type": "module",
5
5
  "description": "Utility functions and helpers shared across the UserTour project",
6
6
  "homepage": "https://www.usertour.io",
@@ -29,7 +29,7 @@
29
29
  },
30
30
  "dependencies": {
31
31
  "@paralleldrive/cuid2": "^2.2.2",
32
- "@usertour/types": "^0.0.9",
32
+ "@usertour/types": "^0.0.11",
33
33
  "chroma-js": "^3.1.2",
34
34
  "class-variance-authority": "^0.4.0",
35
35
  "clsx": "^1.2.1",
@@ -1,92 +0,0 @@
1
- // src/conditions/condition.ts
2
- import isEqual from "fast-deep-equal";
3
- var compareConditionsItem = (item1, item2) => {
4
- const { data = {}, ...others1 } = item1;
5
- const { data: data2 = {}, ...others2 } = item2;
6
- if (!isEqual(others2, others1)) {
7
- return false;
8
- }
9
- for (const key in data) {
10
- if (!isEqual(data[key], data2[key])) {
11
- return false;
12
- }
13
- }
14
- return true;
15
- };
16
- var conditionsIsSame = (rr1, rr2) => {
17
- const r1 = [...rr1];
18
- const r2 = [...rr2];
19
- if (r1.length === 0 && r2.length === 0) {
20
- return true;
21
- }
22
- if (r1.length !== r2.length) {
23
- return false;
24
- }
25
- const group1 = r1.filter((item) => item.type === "group");
26
- const group2 = r2.filter((item) => item.type === "group");
27
- if (group1.length !== group2.length) {
28
- return false;
29
- }
30
- for (let index = 0; index < r1.length; index++) {
31
- const item1 = r1[index];
32
- const item2 = r2[index];
33
- if (!item1 || !item2) {
34
- return false;
35
- }
36
- if (item1.type === "group") {
37
- if (!item2.conditions) {
38
- return false;
39
- }
40
- const c1 = item1.conditions;
41
- const c2 = item2.conditions;
42
- if (item1.operators !== item2.operators) {
43
- return false;
44
- }
45
- if (!conditionsIsSame(c1, c2)) {
46
- return false;
47
- }
48
- } else {
49
- if (!compareConditionsItem(item1, item2)) {
50
- return false;
51
- }
52
- }
53
- }
54
- return true;
55
- };
56
- var isConditionsActived = (conditions) => {
57
- if (!conditions || conditions.length === 0) {
58
- return false;
59
- }
60
- const operator = conditions[0].operators;
61
- const actives = conditions.filter((rule) => {
62
- if (!rule.conditions) {
63
- return rule.actived;
64
- }
65
- return isConditionsActived(rule.conditions);
66
- });
67
- return operator === "and" ? actives.length === conditions.length : actives.length > 0;
68
- };
69
- function filterConditionsByType(conditions, allowedTypes) {
70
- return conditions.filter((condition) => {
71
- if (condition.type === "group" && condition.conditions) {
72
- const filteredGroupConditions = filterConditionsByType(condition.conditions, allowedTypes);
73
- return filteredGroupConditions.length > 0;
74
- }
75
- return allowedTypes.includes(condition.type);
76
- }).map((condition) => {
77
- if (condition.type === "group" && condition.conditions) {
78
- return {
79
- ...condition,
80
- conditions: filterConditionsByType(condition.conditions, allowedTypes)
81
- };
82
- }
83
- return condition;
84
- });
85
- }
86
-
87
- export {
88
- isEqual,
89
- conditionsIsSame,
90
- isConditionsActived,
91
- filterConditionsByType
92
- };