@usertour/helpers 0.0.17 → 0.0.18

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.
@@ -0,0 +1,257 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __copyProps = (to, from, except, desc) => {
9
+ if (from && typeof from === "object" || typeof from === "function") {
10
+ for (let key of __getOwnPropNames(from))
11
+ if (!__hasOwnProp.call(to, key) && key !== except)
12
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
13
+ }
14
+ return to;
15
+ };
16
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
17
+ // If the importer is in node compatibility mode or this is not an ESM
18
+ // file that has been converted to a CommonJS file using a Babel-
19
+ // compatible transform (i.e. "__esModule" has not been set), then set
20
+ // "default" to the CommonJS "module.exports" for node compatibility.
21
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
22
+ mod
23
+ ));
24
+
25
+ // src/__tests__/condition.test.ts
26
+ var import_types = require("@usertour/types");
27
+
28
+ // src/conditions/condition.ts
29
+ var import_fast_deep_equal = __toESM(require("fast-deep-equal"), 1);
30
+ var isConditionsActived = (conditions) => {
31
+ if (!conditions || conditions.length === 0) {
32
+ return false;
33
+ }
34
+ const operator = conditions[0].operators;
35
+ const actives = conditions.filter((rule) => {
36
+ if (!rule.conditions) {
37
+ return rule.actived;
38
+ }
39
+ return isConditionsActived(rule.conditions);
40
+ });
41
+ return operator === "and" ? actives.length === conditions.length : actives.length > 0;
42
+ };
43
+ function filterConditionsByType(conditions, allowedTypes) {
44
+ return conditions.filter((condition) => {
45
+ if (condition.type === "group" && condition.conditions) {
46
+ const filteredGroupConditions = filterConditionsByType(condition.conditions, allowedTypes);
47
+ return filteredGroupConditions.length > 0;
48
+ }
49
+ return allowedTypes.includes(condition.type);
50
+ }).map((condition) => {
51
+ if (condition.type === "group" && condition.conditions) {
52
+ return {
53
+ ...condition,
54
+ conditions: filterConditionsByType(condition.conditions, allowedTypes)
55
+ };
56
+ }
57
+ return condition;
58
+ });
59
+ }
60
+
61
+ // src/__tests__/condition.test.ts
62
+ describe("filterConditionsByType", () => {
63
+ test("should return empty array when no conditions provided", () => {
64
+ const result = filterConditionsByType([], [import_types.RulesType.CURRENT_PAGE, import_types.RulesType.TIME]);
65
+ expect(result).toEqual([]);
66
+ });
67
+ test("should filter out non-matching types", () => {
68
+ const conditions = [
69
+ {
70
+ id: "1",
71
+ type: "element",
72
+ operators: "and",
73
+ actived: true,
74
+ data: {}
75
+ },
76
+ {
77
+ id: "2",
78
+ type: "current-page",
79
+ operators: "and",
80
+ actived: true,
81
+ data: {}
82
+ }
83
+ ];
84
+ const result = filterConditionsByType(conditions, [import_types.RulesType.CURRENT_PAGE, import_types.RulesType.TIME]);
85
+ expect(result).toHaveLength(1);
86
+ expect(result[0].type).toBe("current-page");
87
+ });
88
+ test("should keep matching types", () => {
89
+ const conditions = [
90
+ {
91
+ id: "1",
92
+ type: "current-page",
93
+ operators: "and",
94
+ actived: true,
95
+ data: {}
96
+ },
97
+ {
98
+ id: "2",
99
+ type: "time",
100
+ operators: "and",
101
+ actived: true,
102
+ data: {}
103
+ }
104
+ ];
105
+ const result = filterConditionsByType(conditions, [import_types.RulesType.CURRENT_PAGE, import_types.RulesType.TIME]);
106
+ expect(result).toHaveLength(2);
107
+ expect(result.map((c) => c.type)).toEqual(["current-page", "time"]);
108
+ });
109
+ test("should handle nested group conditions", () => {
110
+ const conditions = [
111
+ {
112
+ id: "1",
113
+ type: "group",
114
+ operators: "and",
115
+ data: {},
116
+ conditions: [
117
+ {
118
+ id: "2",
119
+ type: "current-page",
120
+ operators: "and",
121
+ actived: true,
122
+ data: {}
123
+ },
124
+ {
125
+ id: "3",
126
+ type: "element",
127
+ // This should be filtered out
128
+ operators: "and",
129
+ actived: true,
130
+ data: {}
131
+ }
132
+ ]
133
+ }
134
+ ];
135
+ const result = filterConditionsByType(conditions, [import_types.RulesType.CURRENT_PAGE, import_types.RulesType.TIME]);
136
+ expect(result).toHaveLength(1);
137
+ expect(result[0].type).toBe("group");
138
+ expect(result[0].conditions).toHaveLength(1);
139
+ expect(result[0].conditions[0].type).toBe("current-page");
140
+ });
141
+ test("should remove groups with no matching conditions", () => {
142
+ const conditions = [
143
+ {
144
+ id: "1",
145
+ type: "group",
146
+ operators: "and",
147
+ data: {},
148
+ conditions: [
149
+ {
150
+ id: "2",
151
+ type: "element",
152
+ // This should be filtered out
153
+ operators: "and",
154
+ actived: true,
155
+ data: {}
156
+ }
157
+ ]
158
+ }
159
+ ];
160
+ const result = filterConditionsByType(conditions, [import_types.RulesType.CURRENT_PAGE, import_types.RulesType.TIME]);
161
+ expect(result).toHaveLength(0);
162
+ });
163
+ });
164
+ describe("isConditionsActived", () => {
165
+ test("should return false when no conditions provided", () => {
166
+ const result = isConditionsActived([]);
167
+ expect(result).toBe(false);
168
+ });
169
+ test("should return true when all conditions are actived with AND logic", () => {
170
+ const conditions = [
171
+ {
172
+ id: "1",
173
+ type: "current-page",
174
+ operators: "and",
175
+ actived: true,
176
+ data: {}
177
+ },
178
+ {
179
+ id: "2",
180
+ type: "time",
181
+ operators: "and",
182
+ actived: true,
183
+ data: {}
184
+ }
185
+ ];
186
+ const result = isConditionsActived(conditions);
187
+ expect(result).toBe(true);
188
+ });
189
+ test("should return false when one condition is not actived with AND logic", () => {
190
+ const conditions = [
191
+ {
192
+ id: "1",
193
+ type: "current-page",
194
+ operators: "and",
195
+ actived: true,
196
+ data: {}
197
+ },
198
+ {
199
+ id: "2",
200
+ type: "time",
201
+ operators: "and",
202
+ actived: false,
203
+ data: {}
204
+ }
205
+ ];
206
+ const result = isConditionsActived(conditions);
207
+ expect(result).toBe(false);
208
+ });
209
+ test("should return true when at least one condition is actived with OR logic", () => {
210
+ const conditions = [
211
+ {
212
+ id: "1",
213
+ type: "current-page",
214
+ operators: "or",
215
+ actived: false,
216
+ data: {}
217
+ },
218
+ {
219
+ id: "2",
220
+ type: "time",
221
+ operators: "or",
222
+ actived: true,
223
+ data: {}
224
+ }
225
+ ];
226
+ const result = isConditionsActived(conditions);
227
+ expect(result).toBe(true);
228
+ });
229
+ test("should handle nested group conditions", () => {
230
+ const conditions = [
231
+ {
232
+ id: "1",
233
+ type: "group",
234
+ operators: "and",
235
+ data: {},
236
+ conditions: [
237
+ {
238
+ id: "2",
239
+ type: "current-page",
240
+ operators: "and",
241
+ actived: true,
242
+ data: {}
243
+ },
244
+ {
245
+ id: "3",
246
+ type: "time",
247
+ operators: "and",
248
+ actived: true,
249
+ data: {}
250
+ }
251
+ ]
252
+ }
253
+ ];
254
+ const result = isConditionsActived(conditions);
255
+ expect(result).toBe(true);
256
+ });
257
+ });
@@ -0,0 +1,2 @@
1
+
2
+ export { }
@@ -0,0 +1,2 @@
1
+
2
+ export { }
@@ -0,0 +1,204 @@
1
+ import {
2
+ filterConditionsByType,
3
+ isConditionsActived
4
+ } from "../chunk-YOFQHQ7D.js";
5
+ import "../chunk-XEO3YXBM.js";
6
+
7
+ // src/__tests__/condition.test.ts
8
+ import { RulesType } from "@usertour/types";
9
+ describe("filterConditionsByType", () => {
10
+ test("should return empty array when no conditions provided", () => {
11
+ const result = filterConditionsByType([], [RulesType.CURRENT_PAGE, RulesType.TIME]);
12
+ expect(result).toEqual([]);
13
+ });
14
+ test("should filter out non-matching types", () => {
15
+ const conditions = [
16
+ {
17
+ id: "1",
18
+ type: "element",
19
+ operators: "and",
20
+ actived: true,
21
+ data: {}
22
+ },
23
+ {
24
+ id: "2",
25
+ type: "current-page",
26
+ operators: "and",
27
+ actived: true,
28
+ data: {}
29
+ }
30
+ ];
31
+ const result = filterConditionsByType(conditions, [RulesType.CURRENT_PAGE, RulesType.TIME]);
32
+ expect(result).toHaveLength(1);
33
+ expect(result[0].type).toBe("current-page");
34
+ });
35
+ test("should keep matching types", () => {
36
+ const conditions = [
37
+ {
38
+ id: "1",
39
+ type: "current-page",
40
+ operators: "and",
41
+ actived: true,
42
+ data: {}
43
+ },
44
+ {
45
+ id: "2",
46
+ type: "time",
47
+ operators: "and",
48
+ actived: true,
49
+ data: {}
50
+ }
51
+ ];
52
+ const result = filterConditionsByType(conditions, [RulesType.CURRENT_PAGE, RulesType.TIME]);
53
+ expect(result).toHaveLength(2);
54
+ expect(result.map((c) => c.type)).toEqual(["current-page", "time"]);
55
+ });
56
+ test("should handle nested group conditions", () => {
57
+ const conditions = [
58
+ {
59
+ id: "1",
60
+ type: "group",
61
+ operators: "and",
62
+ data: {},
63
+ conditions: [
64
+ {
65
+ id: "2",
66
+ type: "current-page",
67
+ operators: "and",
68
+ actived: true,
69
+ data: {}
70
+ },
71
+ {
72
+ id: "3",
73
+ type: "element",
74
+ // This should be filtered out
75
+ operators: "and",
76
+ actived: true,
77
+ data: {}
78
+ }
79
+ ]
80
+ }
81
+ ];
82
+ const result = filterConditionsByType(conditions, [RulesType.CURRENT_PAGE, RulesType.TIME]);
83
+ expect(result).toHaveLength(1);
84
+ expect(result[0].type).toBe("group");
85
+ expect(result[0].conditions).toHaveLength(1);
86
+ expect(result[0].conditions[0].type).toBe("current-page");
87
+ });
88
+ test("should remove groups with no matching conditions", () => {
89
+ const conditions = [
90
+ {
91
+ id: "1",
92
+ type: "group",
93
+ operators: "and",
94
+ data: {},
95
+ conditions: [
96
+ {
97
+ id: "2",
98
+ type: "element",
99
+ // This should be filtered out
100
+ operators: "and",
101
+ actived: true,
102
+ data: {}
103
+ }
104
+ ]
105
+ }
106
+ ];
107
+ const result = filterConditionsByType(conditions, [RulesType.CURRENT_PAGE, RulesType.TIME]);
108
+ expect(result).toHaveLength(0);
109
+ });
110
+ });
111
+ describe("isConditionsActived", () => {
112
+ test("should return false when no conditions provided", () => {
113
+ const result = isConditionsActived([]);
114
+ expect(result).toBe(false);
115
+ });
116
+ test("should return true when all conditions are actived with AND logic", () => {
117
+ const conditions = [
118
+ {
119
+ id: "1",
120
+ type: "current-page",
121
+ operators: "and",
122
+ actived: true,
123
+ data: {}
124
+ },
125
+ {
126
+ id: "2",
127
+ type: "time",
128
+ operators: "and",
129
+ actived: true,
130
+ data: {}
131
+ }
132
+ ];
133
+ const result = isConditionsActived(conditions);
134
+ expect(result).toBe(true);
135
+ });
136
+ test("should return false when one condition is not actived with AND logic", () => {
137
+ const conditions = [
138
+ {
139
+ id: "1",
140
+ type: "current-page",
141
+ operators: "and",
142
+ actived: true,
143
+ data: {}
144
+ },
145
+ {
146
+ id: "2",
147
+ type: "time",
148
+ operators: "and",
149
+ actived: false,
150
+ data: {}
151
+ }
152
+ ];
153
+ const result = isConditionsActived(conditions);
154
+ expect(result).toBe(false);
155
+ });
156
+ test("should return true when at least one condition is actived with OR logic", () => {
157
+ const conditions = [
158
+ {
159
+ id: "1",
160
+ type: "current-page",
161
+ operators: "or",
162
+ actived: false,
163
+ data: {}
164
+ },
165
+ {
166
+ id: "2",
167
+ type: "time",
168
+ operators: "or",
169
+ actived: true,
170
+ data: {}
171
+ }
172
+ ];
173
+ const result = isConditionsActived(conditions);
174
+ expect(result).toBe(true);
175
+ });
176
+ test("should handle nested group conditions", () => {
177
+ const conditions = [
178
+ {
179
+ id: "1",
180
+ type: "group",
181
+ operators: "and",
182
+ data: {},
183
+ conditions: [
184
+ {
185
+ id: "2",
186
+ type: "current-page",
187
+ operators: "and",
188
+ actived: true,
189
+ data: {}
190
+ },
191
+ {
192
+ id: "3",
193
+ type: "time",
194
+ operators: "and",
195
+ actived: true,
196
+ data: {}
197
+ }
198
+ ]
199
+ }
200
+ ];
201
+ const result = isConditionsActived(conditions);
202
+ expect(result).toBe(true);
203
+ });
204
+ });
@@ -53,8 +53,40 @@ var conditionsIsSame = (rr1, rr2) => {
53
53
  }
54
54
  return true;
55
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
+ }
56
86
 
57
87
  export {
58
88
  isEqual,
59
- conditionsIsSame
89
+ conditionsIsSame,
90
+ isConditionsActived,
91
+ filterConditionsByType
60
92
  };
@@ -31,6 +31,8 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
31
31
  var condition_exports = {};
32
32
  __export(condition_exports, {
33
33
  conditionsIsSame: () => conditionsIsSame,
34
+ filterConditionsByType: () => filterConditionsByType,
35
+ isConditionsActived: () => isConditionsActived,
34
36
  isEqual: () => import_fast_deep_equal.default
35
37
  });
36
38
  module.exports = __toCommonJS(condition_exports);
@@ -88,8 +90,40 @@ var conditionsIsSame = (rr1, rr2) => {
88
90
  }
89
91
  return true;
90
92
  };
93
+ var isConditionsActived = (conditions) => {
94
+ if (!conditions || conditions.length === 0) {
95
+ return false;
96
+ }
97
+ const operator = conditions[0].operators;
98
+ const actives = conditions.filter((rule) => {
99
+ if (!rule.conditions) {
100
+ return rule.actived;
101
+ }
102
+ return isConditionsActived(rule.conditions);
103
+ });
104
+ return operator === "and" ? actives.length === conditions.length : actives.length > 0;
105
+ };
106
+ function filterConditionsByType(conditions, allowedTypes) {
107
+ return conditions.filter((condition) => {
108
+ if (condition.type === "group" && condition.conditions) {
109
+ const filteredGroupConditions = filterConditionsByType(condition.conditions, allowedTypes);
110
+ return filteredGroupConditions.length > 0;
111
+ }
112
+ return allowedTypes.includes(condition.type);
113
+ }).map((condition) => {
114
+ if (condition.type === "group" && condition.conditions) {
115
+ return {
116
+ ...condition,
117
+ conditions: filterConditionsByType(condition.conditions, allowedTypes)
118
+ };
119
+ }
120
+ return condition;
121
+ });
122
+ }
91
123
  // Annotate the CommonJS export names for ESM import in node:
92
124
  0 && (module.exports = {
93
125
  conditionsIsSame,
126
+ filterConditionsByType,
127
+ isConditionsActived,
94
128
  isEqual
95
129
  });
@@ -1,6 +1,14 @@
1
- import { RulesCondition } from '@usertour/types';
1
+ import { RulesCondition, RulesType } 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 function filterConditionsByType(conditions: RulesCondition[], allowedTypes: RulesType[]): RulesCondition[];
5
13
 
6
- export { conditionsIsSame };
14
+ export { conditionsIsSame, filterConditionsByType, isConditionsActived };
@@ -1,6 +1,14 @@
1
- import { RulesCondition } from '@usertour/types';
1
+ import { RulesCondition, RulesType } 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 function filterConditionsByType(conditions: RulesCondition[], allowedTypes: RulesType[]): RulesCondition[];
5
13
 
6
- export { conditionsIsSame };
14
+ export { conditionsIsSame, filterConditionsByType, isConditionsActived };
@@ -1,9 +1,13 @@
1
1
  import {
2
2
  conditionsIsSame,
3
+ filterConditionsByType,
4
+ isConditionsActived,
3
5
  isEqual
4
- } from "../chunk-7CC4WXB3.js";
6
+ } from "../chunk-YOFQHQ7D.js";
5
7
  import "../chunk-XEO3YXBM.js";
6
8
  export {
7
9
  conditionsIsSame,
10
+ filterConditionsByType,
11
+ isConditionsActived,
8
12
  isEqual
9
13
  };
@@ -1,18 +1,18 @@
1
1
  import "../chunk-7ODE2AIC.js";
2
2
  import {
3
- conditionsIsSame,
4
- isEqual
5
- } from "../chunk-7CC4WXB3.js";
3
+ evaluateUrlCondition,
4
+ isMatchUrlPattern
5
+ } from "../chunk-BC7KXBMF.js";
6
6
  import {
7
7
  evaluateAttributeCondition
8
8
  } from "../chunk-PBZSPV5R.js";
9
+ import {
10
+ conditionsIsSame,
11
+ isEqual
12
+ } from "../chunk-YOFQHQ7D.js";
9
13
  import {
10
14
  evaluateTimeCondition
11
15
  } from "../chunk-CEK3SCQO.js";
12
- import {
13
- evaluateUrlCondition,
14
- isMatchUrlPattern
15
- } from "../chunk-BC7KXBMF.js";
16
16
  import "../chunk-XEO3YXBM.js";
17
17
  export {
18
18
  conditionsIsSame,
package/dist/index.js CHANGED
@@ -1,8 +1,8 @@
1
1
  import "./chunk-7ODE2AIC.js";
2
2
  import {
3
- conditionsIsSame,
4
- isEqual
5
- } from "./chunk-7CC4WXB3.js";
3
+ evaluateUrlCondition,
4
+ isMatchUrlPattern
5
+ } from "./chunk-BC7KXBMF.js";
6
6
  import {
7
7
  isUrl
8
8
  } from "./chunk-ZNFXGN3M.js";
@@ -15,13 +15,13 @@ import {
15
15
  import {
16
16
  evaluateAttributeCondition
17
17
  } from "./chunk-PBZSPV5R.js";
18
+ import {
19
+ conditionsIsSame,
20
+ isEqual
21
+ } from "./chunk-YOFQHQ7D.js";
18
22
  import {
19
23
  evaluateTimeCondition
20
24
  } from "./chunk-CEK3SCQO.js";
21
- import {
22
- evaluateUrlCondition,
23
- isMatchUrlPattern
24
- } from "./chunk-BC7KXBMF.js";
25
25
  import {
26
26
  getAuthToken,
27
27
  removeAuthToken,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@usertour/helpers",
3
- "version": "0.0.17",
3
+ "version": "0.0.18",
4
4
  "type": "module",
5
5
  "description": "Utility functions and helpers shared across the UserTour project",
6
6
  "homepage": "https://www.usertour.io",