@usertour/helpers 0.0.17 → 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,66 +30,387 @@ 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
+ filterConditionsByType: () => filterConditionsByType,
36
+ isConditionsActived: () => isConditionsActived,
34
37
  isEqual: () => import_fast_deep_equal.default
35
38
  });
36
39
  module.exports = __toCommonJS(condition_exports);
40
+ var import_types2 = require("@usertour/types");
37
41
  var import_fast_deep_equal = __toESM(require("fast-deep-equal"), 1);
38
- var compareConditionsItem = (item1, item2) => {
39
- const { data = {}, ...others1 } = item1;
40
- const { data: data2 = {}, ...others2 } = item2;
41
- 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) {
42
104
  return false;
43
105
  }
44
- for (const key in data) {
45
- 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) {
46
132
  return false;
47
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)) {
146
+ return false;
147
+ }
148
+ return (0, import_date_fns.isAfter)(now, startTime) && (0, import_date_fns.isBefore)(now, endTime);
149
+ } catch {
150
+ return false;
48
151
  }
49
- return true;
50
152
  };
51
- var conditionsIsSame = (rr1, rr2) => {
52
- const r1 = [...rr1];
53
- const r2 = [...rr2];
54
- if (r1.length === 0 && r2.length === 0) {
55
- 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;
56
161
  }
57
- if (r1.length !== r2.length) {
162
+ const { logic, value, attrId, value2, listValues = [] } = data;
163
+ if (!attrId) {
58
164
  return false;
59
165
  }
60
- const group1 = r1.filter((item) => item.type === "group");
61
- const group2 = r2.filter((item) => item.type === "group");
62
- if (group1.length !== group2.length) {
166
+ const attr = attributes.find((attr2) => attr2.id === attrId);
167
+ if (!attr) {
63
168
  return false;
64
169
  }
65
- for (let index = 0; index < r1.length; index++) {
66
- const item1 = r1[index];
67
- const item2 = r2[index];
68
- if (!item1 || !item2) {
69
- 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;
70
209
  }
71
- if (item1.type === "group") {
72
- if (!item2.conditions) {
73
- return false;
74
- }
75
- const c1 = item1.conditions;
76
- const c2 = item2.conditions;
77
- if (item1.operators !== item2.operators) {
78
- return false;
79
- }
80
- if (!conditionsIsSame(c1, c2)) {
81
- return false;
82
- }
83
- } else {
84
- 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:
85
268
  return false;
86
- }
87
269
  }
88
270
  }
89
- 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);
337
+ };
338
+ var isConditionsActived = (conditions) => {
339
+ if (!conditions || conditions.length === 0) {
340
+ return false;
341
+ }
342
+ const operator = conditions[0].operators;
343
+ const actives = conditions.filter((rule) => {
344
+ if (!rule.conditions) {
345
+ return rule.actived;
346
+ }
347
+ return isConditionsActived(rule.conditions);
348
+ });
349
+ return operator === "and" ? actives.length === conditions.length : actives.length > 0;
350
+ };
351
+ var filterConditionsByType = (conditions, allowedTypes) => {
352
+ return conditions.filter((condition) => {
353
+ if (condition.type === "group" && condition.conditions) {
354
+ const filteredGroupConditions = filterConditionsByType(condition.conditions, allowedTypes);
355
+ return filteredGroupConditions.length > 0;
356
+ }
357
+ return allowedTypes.includes(condition.type);
358
+ }).map((condition) => {
359
+ if (condition.type === "group" && condition.conditions) {
360
+ return {
361
+ ...condition,
362
+ conditions: filterConditionsByType(condition.conditions, allowedTypes)
363
+ };
364
+ }
365
+ return condition;
366
+ });
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
+ });
90
408
  };
91
409
  // Annotate the CommonJS export names for ESM import in node:
92
410
  0 && (module.exports = {
411
+ activedRulesConditions,
93
412
  conditionsIsSame,
413
+ filterConditionsByType,
414
+ isConditionsActived,
94
415
  isEqual
95
416
  });
@@ -1,6 +1,15 @@
1
- import { RulesCondition } 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;
5
+ declare const isConditionsActived: (conditions: RulesCondition[]) => boolean;
6
+ /**
7
+ * Recursively filter conditions by RulesType
8
+ * @param conditions - Array of rules conditions
9
+ * @param allowedTypes - Array of RulesType to filter by
10
+ * @returns Filtered conditions array
11
+ */
12
+ declare const filterConditionsByType: (conditions: RulesCondition[], allowedTypes: RulesType[]) => RulesCondition[];
13
+ declare const activedRulesConditions: (conditions: RulesCondition[], options?: RulesEvaluationOptions) => RulesCondition[];
5
14
 
6
- export { conditionsIsSame };
15
+ export { activedRulesConditions, conditionsIsSame, filterConditionsByType, isConditionsActived };
@@ -1,6 +1,15 @@
1
- import { RulesCondition } 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;
5
+ declare const isConditionsActived: (conditions: RulesCondition[]) => boolean;
6
+ /**
7
+ * Recursively filter conditions by RulesType
8
+ * @param conditions - Array of rules conditions
9
+ * @param allowedTypes - Array of RulesType to filter by
10
+ * @returns Filtered conditions array
11
+ */
12
+ declare const filterConditionsByType: (conditions: RulesCondition[], allowedTypes: RulesType[]) => RulesCondition[];
13
+ declare const activedRulesConditions: (conditions: RulesCondition[], options?: RulesEvaluationOptions) => RulesCondition[];
5
14
 
6
- export { conditionsIsSame };
15
+ export { activedRulesConditions, conditionsIsSame, filterConditionsByType, isConditionsActived };
@@ -1,9 +1,19 @@
1
1
  import {
2
+ activedRulesConditions,
2
3
  conditionsIsSame,
4
+ filterConditionsByType,
5
+ isConditionsActived,
3
6
  isEqual
4
- } from "../chunk-7CC4WXB3.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";
5
12
  import "../chunk-XEO3YXBM.js";
6
13
  export {
14
+ activedRulesConditions,
7
15
  conditionsIsSame,
16
+ filterConditionsByType,
17
+ isConditionsActived,
8
18
  isEqual
9
19
  };