@usertour/helpers 0.0.18 → 0.0.21

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