@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.
@@ -30,65 +30,310 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
30
30
  // src/conditions/condition.ts
31
31
  var condition_exports = {};
32
32
  __export(condition_exports, {
33
+ activedRulesConditions: () => activedRulesConditions,
33
34
  conditionsIsSame: () => conditionsIsSame,
34
35
  filterConditionsByType: () => filterConditionsByType,
35
36
  isConditionsActived: () => isConditionsActived,
36
37
  isEqual: () => import_fast_deep_equal.default
37
38
  });
38
39
  module.exports = __toCommonJS(condition_exports);
40
+ var import_types2 = require("@usertour/types");
39
41
  var import_fast_deep_equal = __toESM(require("fast-deep-equal"), 1);
40
- var compareConditionsItem = (item1, item2) => {
41
- const { data = {}, ...others1 } = item1;
42
- const { data: data2 = {}, ...others2 } = item2;
43
- if (!(0, import_fast_deep_equal.default)(others2, others1)) {
42
+
43
+ // src/conditions/url-v2.ts
44
+ function parseUrl(url) {
45
+ const match = url.match(/^(([a-z\d]+):\/\/)?([^/?#]+)?(\/[^?#]*)?(\?([^#]*))?(#.*)?$/i);
46
+ if (!match)
47
+ return null;
48
+ const [, , scheme, domain, path, , query, fragment] = match;
49
+ return {
50
+ scheme: scheme || "",
51
+ domain: domain || "",
52
+ path: path || "",
53
+ query: query || "",
54
+ fragment: fragment || ""
55
+ };
56
+ }
57
+ function escapeRegex(str) {
58
+ return str.replace(/[-/\\^$*+?.()|[\]{}]/g, "\\$&");
59
+ }
60
+ function processUrlPattern(pattern, wildcardReplacement, paramReplacement) {
61
+ let processed = escapeRegex(pattern);
62
+ processed = processed.replace(/\\\*/g, `${wildcardReplacement}*`);
63
+ if (paramReplacement) {
64
+ processed = processed.replace(/:[a-zA-Z0-9_]+/g, `[^${paramReplacement}]+`);
65
+ }
66
+ return processed;
67
+ }
68
+ function urlPatternToRegex(pattern) {
69
+ const parsed = parseUrl(pattern);
70
+ if (!parsed) {
71
+ return null;
72
+ }
73
+ const { scheme, domain, path, query, fragment } = parsed;
74
+ const schemePattern = scheme ? escapeRegex(scheme) : "[a-z\\d]+";
75
+ const domainPattern = domain ? processUrlPattern(domain, "[^/]", ".") : "[^/]*";
76
+ const portPattern = "(:\\d+)?";
77
+ const pathPattern = path ? processUrlPattern(path, "[^?#]", "/") : "/[^?#]*";
78
+ let queryPattern;
79
+ if (query) {
80
+ queryPattern = "";
81
+ new URLSearchParams(query).forEach((value, key) => {
82
+ let valuePattern;
83
+ if (value === "") {
84
+ valuePattern = "=?";
85
+ } else if (value === "*") {
86
+ valuePattern = "(=[^&#]*)?";
87
+ } else {
88
+ const encodedValue = value.split(/\*/g).map((part) => encodeURI(part)).join("*");
89
+ valuePattern = `=${processUrlPattern(encodedValue, "[^#]")}`;
90
+ }
91
+ queryPattern += `(?=.*[?&]${escapeRegex(key)}${valuePattern}([&#]|$))`;
92
+ });
93
+ queryPattern += "\\?[^#]*";
94
+ } else {
95
+ queryPattern = "(\\?[^#]*)?";
96
+ }
97
+ const fragmentPattern = fragment ? processUrlPattern(fragment, ".", "/") : "(#.*)?";
98
+ return new RegExp(
99
+ `^${schemePattern}://${domainPattern}${portPattern}${pathPattern}${queryPattern}${fragmentPattern}$`
100
+ );
101
+ }
102
+ function matchUrlPattern(urlPattern, url) {
103
+ if (urlPattern.includes.length === 0 && urlPattern.excludes.length === 0) {
44
104
  return false;
45
105
  }
46
- for (const key in data) {
47
- if (!(0, import_fast_deep_equal.default)(data[key], data2[key])) {
106
+ const matchesInclude = urlPattern.includes.length === 0 || urlPattern.includes.some((includePattern) => {
107
+ const regex = urlPatternToRegex(includePattern);
108
+ return regex && url.match(regex);
109
+ });
110
+ const matchesExclude = urlPattern.excludes.some((excludePattern) => {
111
+ const regex = urlPatternToRegex(excludePattern);
112
+ return regex && url.match(regex);
113
+ });
114
+ return matchesInclude && !matchesExclude;
115
+ }
116
+
117
+ // src/conditions/url.ts
118
+ var isMatchUrlPattern = (_url, includes, excludes) => {
119
+ return matchUrlPattern({ includes, excludes }, _url);
120
+ };
121
+ var evaluateUrlCondition = (rules, url) => {
122
+ const { excludes = [], includes = [] } = rules.data || {};
123
+ return isMatchUrlPattern(url, includes, excludes);
124
+ };
125
+
126
+ // src/conditions/time.ts
127
+ var import_date_fns = require("date-fns");
128
+ var evaluateTimeCondition = (rules) => {
129
+ try {
130
+ const { endDate, endDateHour, endDateMinute, startDate, startDateHour, startDateMinute } = rules.data || {};
131
+ if (!startDate || !startDateHour || !startDateMinute) {
132
+ return false;
133
+ }
134
+ const startTimeString = `${startDate}T${startDateHour.padStart(2, "0")}:${startDateMinute.padStart(2, "0")}:00`;
135
+ const startTime = (0, import_date_fns.parseISO)(startTimeString);
136
+ if (!(0, import_date_fns.isValid)(startTime)) {
137
+ return false;
138
+ }
139
+ const now = /* @__PURE__ */ new Date();
140
+ if (!endDate || !endDateHour || !endDateMinute) {
141
+ return (0, import_date_fns.isAfter)(now, startTime);
142
+ }
143
+ const endTimeString = `${endDate}T${endDateHour.padStart(2, "0")}:${endDateMinute.padStart(2, "0")}:00`;
144
+ const endTime = (0, import_date_fns.parseISO)(endTimeString);
145
+ if (!(0, import_date_fns.isValid)(endTime)) {
48
146
  return false;
49
147
  }
148
+ return (0, import_date_fns.isAfter)(now, startTime) && (0, import_date_fns.isBefore)(now, endTime);
149
+ } catch {
150
+ return false;
50
151
  }
51
- return true;
52
152
  };
53
- var conditionsIsSame = (rr1, rr2) => {
54
- const r1 = [...rr1];
55
- const r2 = [...rr2];
56
- if (r1.length === 0 && r2.length === 0) {
57
- return true;
153
+
154
+ // src/conditions/attribute.ts
155
+ var import_types = require("@usertour/types");
156
+ var import_date_fns2 = require("date-fns");
157
+ function evaluateAttributeCondition(condition, attributes, userAttributes) {
158
+ const { data } = condition;
159
+ if (!data) {
160
+ return false;
58
161
  }
59
- if (r1.length !== r2.length) {
162
+ const { logic, value, attrId, value2, listValues = [] } = data;
163
+ if (!attrId) {
60
164
  return false;
61
165
  }
62
- const group1 = r1.filter((item) => item.type === "group");
63
- const group2 = r2.filter((item) => item.type === "group");
64
- if (group1.length !== group2.length) {
166
+ const attr = attributes.find((attr2) => attr2.id === attrId);
167
+ if (!attr) {
65
168
  return false;
66
169
  }
67
- for (let index = 0; index < r1.length; index++) {
68
- const item1 = r1[index];
69
- const item2 = r2[index];
70
- if (!item1 || !item2) {
71
- return false;
170
+ const actualValue = getAttributeValue(attr.codeName, userAttributes);
171
+ if (attr.dataType === import_types.BizAttributeTypes.String) {
172
+ return evaluateStringCondition(logic, actualValue, value);
173
+ }
174
+ if (attr.dataType === import_types.BizAttributeTypes.Number) {
175
+ return evaluateNumberCondition(logic, actualValue, value, value2);
176
+ }
177
+ if (attr.dataType === import_types.BizAttributeTypes.Boolean) {
178
+ return evaluateBooleanCondition(logic, actualValue);
179
+ }
180
+ if (attr.dataType === import_types.BizAttributeTypes.List) {
181
+ return evaluateListCondition(logic, actualValue, listValues);
182
+ }
183
+ if (attr.dataType === import_types.BizAttributeTypes.DateTime) {
184
+ return evaluateDateTimeCondition(logic, actualValue, value);
185
+ }
186
+ return false;
187
+ }
188
+ function getAttributeValue(codeName, userAttributes) {
189
+ return userAttributes == null ? void 0 : userAttributes[codeName];
190
+ }
191
+ function evaluateStringCondition(logic, actualValue, expectedValue) {
192
+ const stringValue = actualValue === null || actualValue === void 0 ? "" : String(actualValue);
193
+ switch (logic) {
194
+ case "is":
195
+ return stringValue === expectedValue;
196
+ case "not":
197
+ return stringValue !== expectedValue;
198
+ case "contains":
199
+ return stringValue.includes(expectedValue);
200
+ case "notContain":
201
+ return !stringValue.includes(expectedValue);
202
+ case "startsWith":
203
+ return stringValue.startsWith(expectedValue);
204
+ case "endsWith":
205
+ return stringValue.endsWith(expectedValue);
206
+ case "empty": {
207
+ const isEmpty = !stringValue || stringValue === "";
208
+ return isEmpty;
72
209
  }
73
- if (item1.type === "group") {
74
- if (!item2.conditions) {
75
- return false;
76
- }
77
- const c1 = item1.conditions;
78
- const c2 = item2.conditions;
79
- if (item1.operators !== item2.operators) {
80
- return false;
81
- }
82
- if (!conditionsIsSame(c1, c2)) {
83
- return false;
84
- }
85
- } else {
86
- if (!compareConditionsItem(item1, item2)) {
210
+ case "any":
211
+ return Boolean(stringValue && stringValue !== "");
212
+ default:
213
+ return false;
214
+ }
215
+ }
216
+ function evaluateNumberCondition(logic, actualValue, expectedValue, expectedValue2) {
217
+ const numValue = Number(actualValue);
218
+ const numValue2 = Number(expectedValue2);
219
+ if (Number.isNaN(numValue)) {
220
+ return false;
221
+ }
222
+ switch (logic) {
223
+ case "is":
224
+ return numValue === expectedValue;
225
+ case "not":
226
+ return numValue !== expectedValue;
227
+ case "isLessThan":
228
+ return numValue < expectedValue;
229
+ case "isLessThanOrEqualTo":
230
+ return numValue <= expectedValue;
231
+ case "isGreaterThan":
232
+ return numValue > expectedValue;
233
+ case "isGreaterThanOrEqualTo":
234
+ return numValue >= expectedValue;
235
+ case "between":
236
+ return numValue >= expectedValue && numValue <= numValue2;
237
+ case "empty":
238
+ return actualValue === null || actualValue === void 0 || actualValue === "";
239
+ case "any":
240
+ return actualValue !== null && actualValue !== void 0 && actualValue !== "";
241
+ default:
242
+ return false;
243
+ }
244
+ }
245
+ function evaluateBooleanCondition(logic, actualValue) {
246
+ switch (logic) {
247
+ case "true":
248
+ return actualValue === true;
249
+ case "false":
250
+ return actualValue === false;
251
+ case "empty":
252
+ return actualValue === null || actualValue === void 0 || actualValue === "";
253
+ case "any":
254
+ return actualValue !== null && actualValue !== void 0 && actualValue !== "";
255
+ default:
256
+ return false;
257
+ }
258
+ }
259
+ function evaluateListCondition(logic, actualValue, expectedValues) {
260
+ const arrayValue = Array.isArray(actualValue) ? actualValue : [];
261
+ if (logic === "empty" || logic === "any") {
262
+ switch (logic) {
263
+ case "empty":
264
+ return !arrayValue || arrayValue.length === 0;
265
+ case "any":
266
+ return arrayValue && arrayValue.length > 0;
267
+ default:
87
268
  return false;
88
- }
89
269
  }
90
270
  }
91
- return true;
271
+ const filteredValues = expectedValues.filter(
272
+ (value) => value !== null && value !== void 0 && value !== ""
273
+ );
274
+ if (!filteredValues.length) {
275
+ return false;
276
+ }
277
+ switch (logic) {
278
+ case "includesAtLeastOne":
279
+ return filteredValues.some((value) => arrayValue.includes(value));
280
+ case "includesAll":
281
+ return filteredValues.every((value) => arrayValue.includes(value));
282
+ case "notIncludesAtLeastOne":
283
+ return !filteredValues.some((value) => arrayValue.includes(value));
284
+ case "notIncludesAll":
285
+ return !filteredValues.every((value) => arrayValue.includes(value));
286
+ default:
287
+ return false;
288
+ }
289
+ }
290
+ function evaluateDateTimeCondition(logic, actualValue, expectedValue) {
291
+ const actualDate = actualValue ? new Date(actualValue) : null;
292
+ const now = /* @__PURE__ */ new Date();
293
+ if (!actualDate || Number.isNaN(actualDate.getTime())) {
294
+ return false;
295
+ }
296
+ switch (logic) {
297
+ case "lessThan": {
298
+ const targetDate = (0, import_date_fns2.subDays)(now, Number(expectedValue));
299
+ return actualDate >= targetDate;
300
+ }
301
+ case "exactly": {
302
+ const targetDate = (0, import_date_fns2.subDays)(now, Number(expectedValue));
303
+ const start = (0, import_date_fns2.startOfDay)(targetDate);
304
+ const end = (0, import_date_fns2.endOfDay)(targetDate);
305
+ return actualDate >= start && actualDate <= end;
306
+ }
307
+ case "moreThan": {
308
+ const targetDate = (0, import_date_fns2.subDays)(now, Number(expectedValue));
309
+ return actualDate <= targetDate;
310
+ }
311
+ case "before": {
312
+ const expectedDate = new Date(expectedValue);
313
+ return actualDate <= expectedDate;
314
+ }
315
+ case "on": {
316
+ const expectedDateOn = new Date(expectedValue);
317
+ const start = (0, import_date_fns2.startOfDay)(expectedDateOn);
318
+ const end = (0, import_date_fns2.endOfDay)(expectedDateOn);
319
+ return actualDate >= start && actualDate <= end;
320
+ }
321
+ case "after": {
322
+ const expectedDateAfter = new Date(expectedValue);
323
+ return actualDate >= expectedDateAfter;
324
+ }
325
+ case "empty":
326
+ return !actualValue || actualValue === "";
327
+ case "any":
328
+ return actualValue && actualValue !== "";
329
+ default:
330
+ return false;
331
+ }
332
+ }
333
+
334
+ // src/conditions/condition.ts
335
+ var conditionsIsSame = (rr1, rr2) => {
336
+ return (0, import_fast_deep_equal.default)(rr1, rr2);
92
337
  };
93
338
  var isConditionsActived = (conditions) => {
94
339
  if (!conditions || conditions.length === 0) {
@@ -103,7 +348,7 @@ var isConditionsActived = (conditions) => {
103
348
  });
104
349
  return operator === "and" ? actives.length === conditions.length : actives.length > 0;
105
350
  };
106
- function filterConditionsByType(conditions, allowedTypes) {
351
+ var filterConditionsByType = (conditions, allowedTypes) => {
107
352
  return conditions.filter((condition) => {
108
353
  if (condition.type === "group" && condition.conditions) {
109
354
  const filteredGroupConditions = filterConditionsByType(condition.conditions, allowedTypes);
@@ -119,9 +364,51 @@ function filterConditionsByType(conditions, allowedTypes) {
119
364
  }
120
365
  return condition;
121
366
  });
122
- }
367
+ };
368
+ var evaluateRule = (rule, options) => {
369
+ var _a;
370
+ const { typeControl = {}, activatedIds, deactivatedIds } = options;
371
+ const ruleId = rule.id;
372
+ if (activatedIds == null ? void 0 : activatedIds.includes(ruleId))
373
+ return true;
374
+ if (deactivatedIds == null ? void 0 : deactivatedIds.includes(ruleId))
375
+ return false;
376
+ if (typeControl[rule.type] === false) {
377
+ return rule.actived || false;
378
+ }
379
+ switch (rule.type) {
380
+ case import_types2.RulesType.CURRENT_PAGE:
381
+ return evaluateUrlCondition(rule, ((_a = options.clientContext) == null ? void 0 : _a.page_url) || "");
382
+ case import_types2.RulesType.TIME:
383
+ return evaluateTimeCondition(rule);
384
+ case import_types2.RulesType.USER_ATTR:
385
+ case import_types2.RulesType.COMPANY_ATTR:
386
+ return evaluateAttributeCondition(
387
+ rule,
388
+ options.attributes || [],
389
+ options.userAttributes || {}
390
+ );
391
+ default:
392
+ return rule.actived || false;
393
+ }
394
+ };
395
+ var activedRulesConditions = (conditions, options = {}) => {
396
+ return conditions.map((rule) => {
397
+ if (rule.type === "group" && rule.conditions) {
398
+ return {
399
+ ...rule,
400
+ conditions: activedRulesConditions(rule.conditions, options)
401
+ };
402
+ }
403
+ return {
404
+ ...rule,
405
+ actived: evaluateRule(rule, options)
406
+ };
407
+ });
408
+ };
123
409
  // Annotate the CommonJS export names for ESM import in node:
124
410
  0 && (module.exports = {
411
+ activedRulesConditions,
125
412
  conditionsIsSame,
126
413
  filterConditionsByType,
127
414
  isConditionsActived,
@@ -1,4 +1,4 @@
1
- import { RulesCondition, RulesType } from '@usertour/types';
1
+ import { RulesCondition, RulesType, RulesEvaluationOptions } from '@usertour/types';
2
2
  export { default as isEqual } from 'fast-deep-equal';
3
3
 
4
4
  declare const conditionsIsSame: (rr1: RulesCondition[], rr2: RulesCondition[]) => boolean;
@@ -9,6 +9,7 @@ declare const isConditionsActived: (conditions: RulesCondition[]) => boolean;
9
9
  * @param allowedTypes - Array of RulesType to filter by
10
10
  * @returns Filtered conditions array
11
11
  */
12
- declare function filterConditionsByType(conditions: RulesCondition[], allowedTypes: RulesType[]): RulesCondition[];
12
+ declare const filterConditionsByType: (conditions: RulesCondition[], allowedTypes: RulesType[]) => RulesCondition[];
13
+ declare const activedRulesConditions: (conditions: RulesCondition[], options?: RulesEvaluationOptions) => RulesCondition[];
13
14
 
14
- export { conditionsIsSame, filterConditionsByType, isConditionsActived };
15
+ export { activedRulesConditions, conditionsIsSame, filterConditionsByType, isConditionsActived };
@@ -1,4 +1,4 @@
1
- import { RulesCondition, RulesType } from '@usertour/types';
1
+ import { RulesCondition, RulesType, RulesEvaluationOptions } from '@usertour/types';
2
2
  export { default as isEqual } from 'fast-deep-equal';
3
3
 
4
4
  declare const conditionsIsSame: (rr1: RulesCondition[], rr2: RulesCondition[]) => boolean;
@@ -9,6 +9,7 @@ declare const isConditionsActived: (conditions: RulesCondition[]) => boolean;
9
9
  * @param allowedTypes - Array of RulesType to filter by
10
10
  * @returns Filtered conditions array
11
11
  */
12
- declare function filterConditionsByType(conditions: RulesCondition[], allowedTypes: RulesType[]): RulesCondition[];
12
+ declare const filterConditionsByType: (conditions: RulesCondition[], allowedTypes: RulesType[]) => RulesCondition[];
13
+ declare const activedRulesConditions: (conditions: RulesCondition[], options?: RulesEvaluationOptions) => RulesCondition[];
13
14
 
14
- export { conditionsIsSame, filterConditionsByType, isConditionsActived };
15
+ export { activedRulesConditions, conditionsIsSame, filterConditionsByType, isConditionsActived };
@@ -1,11 +1,17 @@
1
1
  import {
2
+ activedRulesConditions,
2
3
  conditionsIsSame,
3
4
  filterConditionsByType,
4
5
  isConditionsActived,
5
6
  isEqual
6
- } from "../chunk-YOFQHQ7D.js";
7
+ } from "../chunk-TB7JGI2Q.js";
8
+ import "../chunk-YYIGUZNZ.js";
9
+ import "../chunk-PAESAL23.js";
10
+ import "../chunk-PBZSPV5R.js";
11
+ import "../chunk-CEK3SCQO.js";
7
12
  import "../chunk-XEO3YXBM.js";
8
13
  export {
14
+ activedRulesConditions,
9
15
  conditionsIsSame,
10
16
  filterConditionsByType,
11
17
  isConditionsActived,