@usertour/helpers 0.0.15 → 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.
- package/dist/__tests__/attribute.test.cjs +1036 -0
- package/dist/__tests__/attribute.test.d.cts +2 -0
- package/dist/__tests__/attribute.test.d.ts +2 -0
- package/dist/__tests__/attribute.test.js +802 -0
- package/dist/__tests__/condition.test.cjs +257 -0
- package/dist/__tests__/condition.test.d.cts +2 -0
- package/dist/__tests__/condition.test.d.ts +2 -0
- package/dist/__tests__/condition.test.js +204 -0
- package/dist/__tests__/time.test.cjs +73 -0
- package/dist/__tests__/time.test.d.cts +2 -0
- package/dist/__tests__/time.test.d.ts +2 -0
- package/dist/__tests__/time.test.js +48 -0
- package/dist/__tests__/url.test.cjs +259 -0
- package/dist/__tests__/url.test.d.cts +2 -0
- package/dist/__tests__/url.test.d.ts +2 -0
- package/dist/__tests__/url.test.js +197 -0
- package/dist/{chunk-B4DTY6GN.js → chunk-4LLDSAHJ.js} +0 -12
- package/dist/chunk-7ODE2AIC.js +0 -0
- package/dist/{chunk-IZFZYGPU.js → chunk-BC7KXBMF.js} +5 -56
- package/dist/chunk-CEK3SCQO.js +31 -0
- package/dist/chunk-PBZSPV5R.js +239 -0
- package/dist/chunk-YOFQHQ7D.js +92 -0
- package/dist/conditions/attribute.cjs +264 -0
- package/dist/conditions/attribute.d.cts +28 -0
- package/dist/conditions/attribute.d.ts +28 -0
- package/dist/conditions/attribute.js +9 -0
- package/dist/conditions/condition.cjs +129 -0
- package/dist/conditions/condition.d.cts +14 -0
- package/dist/conditions/condition.d.ts +14 -0
- package/dist/conditions/condition.js +13 -0
- package/dist/conditions/index.cjs +379 -0
- package/dist/conditions/index.d.cts +6 -0
- package/dist/conditions/index.d.ts +6 -0
- package/dist/conditions/index.js +24 -0
- package/dist/conditions/time.cjs +55 -0
- package/dist/conditions/time.d.cts +10 -0
- package/dist/conditions/time.d.ts +10 -0
- package/dist/conditions/time.js +7 -0
- package/dist/{conditions.cjs → conditions/url.cjs} +9 -71
- package/dist/conditions/url.d.cts +6 -0
- package/dist/conditions/url.d.ts +6 -0
- package/dist/conditions/url.js +9 -0
- package/dist/content.cjs +0 -13
- package/dist/content.d.cts +1 -2
- package/dist/content.d.ts +1 -2
- package/dist/content.js +1 -3
- package/dist/index.cjs +261 -54
- package/dist/index.d.cts +5 -2
- package/dist/index.d.ts +5 -2
- package/dist/index.js +19 -8
- package/package.json +15 -9
- package/dist/conditions.d.cts +0 -7
- package/dist/conditions.d.ts +0 -7
- package/dist/conditions.js +0 -11
|
@@ -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,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
|
+
});
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
// src/conditions/time.ts
|
|
4
|
+
var import_date_fns = require("date-fns");
|
|
5
|
+
var evaluateTimeCondition = (rules) => {
|
|
6
|
+
try {
|
|
7
|
+
const { endDate, endDateHour, endDateMinute, startDate, startDateHour, startDateMinute } = rules.data || {};
|
|
8
|
+
if (!startDate || !startDateHour || !startDateMinute) {
|
|
9
|
+
return false;
|
|
10
|
+
}
|
|
11
|
+
const startTimeString = `${startDate}T${startDateHour.padStart(2, "0")}:${startDateMinute.padStart(2, "0")}:00`;
|
|
12
|
+
const startTime = (0, import_date_fns.parseISO)(startTimeString);
|
|
13
|
+
if (!(0, import_date_fns.isValid)(startTime)) {
|
|
14
|
+
return false;
|
|
15
|
+
}
|
|
16
|
+
const now = /* @__PURE__ */ new Date();
|
|
17
|
+
if (!endDate || !endDateHour || !endDateMinute) {
|
|
18
|
+
return (0, import_date_fns.isAfter)(now, startTime);
|
|
19
|
+
}
|
|
20
|
+
const endTimeString = `${endDate}T${endDateHour.padStart(2, "0")}:${endDateMinute.padStart(2, "0")}:00`;
|
|
21
|
+
const endTime = (0, import_date_fns.parseISO)(endTimeString);
|
|
22
|
+
if (!(0, import_date_fns.isValid)(endTime)) {
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
return (0, import_date_fns.isAfter)(now, startTime) && (0, import_date_fns.isBefore)(now, endTime);
|
|
26
|
+
} catch {
|
|
27
|
+
return false;
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
// src/__tests__/time.test.ts
|
|
32
|
+
describe("Time Condition Evaluation", () => {
|
|
33
|
+
const mockNow = /* @__PURE__ */ new Date("2024-01-15T12:00:00Z");
|
|
34
|
+
beforeAll(() => {
|
|
35
|
+
jest.useFakeTimers();
|
|
36
|
+
jest.setSystemTime(mockNow);
|
|
37
|
+
});
|
|
38
|
+
afterAll(() => {
|
|
39
|
+
jest.useRealTimers();
|
|
40
|
+
});
|
|
41
|
+
test("should return true when current time is after start time and no end time", () => {
|
|
42
|
+
const rules = {
|
|
43
|
+
id: "condition-1",
|
|
44
|
+
type: "condition",
|
|
45
|
+
operators: "and",
|
|
46
|
+
data: {
|
|
47
|
+
startDate: "2024-01-15",
|
|
48
|
+
startDateHour: "10",
|
|
49
|
+
startDateMinute: "00",
|
|
50
|
+
endDate: "",
|
|
51
|
+
endDateHour: "",
|
|
52
|
+
endDateMinute: ""
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
expect(evaluateTimeCondition(rules)).toBe(true);
|
|
56
|
+
});
|
|
57
|
+
test("should return true when current time is within start and end time range", () => {
|
|
58
|
+
const rules = {
|
|
59
|
+
id: "condition-2",
|
|
60
|
+
type: "condition",
|
|
61
|
+
operators: "and",
|
|
62
|
+
data: {
|
|
63
|
+
startDate: "2024-01-15",
|
|
64
|
+
startDateHour: "10",
|
|
65
|
+
startDateMinute: "00",
|
|
66
|
+
endDate: "2024-01-15",
|
|
67
|
+
endDateHour: "14",
|
|
68
|
+
endDateMinute: "00"
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
expect(evaluateTimeCondition(rules)).toBe(false);
|
|
72
|
+
});
|
|
73
|
+
});
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import {
|
|
2
|
+
evaluateTimeCondition
|
|
3
|
+
} from "../chunk-CEK3SCQO.js";
|
|
4
|
+
import "../chunk-XEO3YXBM.js";
|
|
5
|
+
|
|
6
|
+
// src/__tests__/time.test.ts
|
|
7
|
+
describe("Time Condition Evaluation", () => {
|
|
8
|
+
const mockNow = /* @__PURE__ */ new Date("2024-01-15T12:00:00Z");
|
|
9
|
+
beforeAll(() => {
|
|
10
|
+
jest.useFakeTimers();
|
|
11
|
+
jest.setSystemTime(mockNow);
|
|
12
|
+
});
|
|
13
|
+
afterAll(() => {
|
|
14
|
+
jest.useRealTimers();
|
|
15
|
+
});
|
|
16
|
+
test("should return true when current time is after start time and no end time", () => {
|
|
17
|
+
const rules = {
|
|
18
|
+
id: "condition-1",
|
|
19
|
+
type: "condition",
|
|
20
|
+
operators: "and",
|
|
21
|
+
data: {
|
|
22
|
+
startDate: "2024-01-15",
|
|
23
|
+
startDateHour: "10",
|
|
24
|
+
startDateMinute: "00",
|
|
25
|
+
endDate: "",
|
|
26
|
+
endDateHour: "",
|
|
27
|
+
endDateMinute: ""
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
expect(evaluateTimeCondition(rules)).toBe(true);
|
|
31
|
+
});
|
|
32
|
+
test("should return true when current time is within start and end time range", () => {
|
|
33
|
+
const rules = {
|
|
34
|
+
id: "condition-2",
|
|
35
|
+
type: "condition",
|
|
36
|
+
operators: "and",
|
|
37
|
+
data: {
|
|
38
|
+
startDate: "2024-01-15",
|
|
39
|
+
startDateHour: "10",
|
|
40
|
+
startDateMinute: "00",
|
|
41
|
+
endDate: "2024-01-15",
|
|
42
|
+
endDateHour: "14",
|
|
43
|
+
endDateMinute: "00"
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
expect(evaluateTimeCondition(rules)).toBe(false);
|
|
47
|
+
});
|
|
48
|
+
});
|