@usertour/helpers 0.0.34 → 0.0.36
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 +10 -1
- package/dist/__tests__/attribute.test.js +2 -1
- package/dist/__tests__/condition.test.cjs +1008 -21
- package/dist/__tests__/condition.test.js +902 -4
- package/dist/__tests__/get-attribute-type.test.cjs +195 -0
- package/dist/__tests__/get-attribute-type.test.d.cts +2 -0
- package/dist/__tests__/get-attribute-type.test.d.ts +2 -0
- package/dist/__tests__/get-attribute-type.test.js +133 -0
- package/dist/__tests__/time.test.cjs +482 -42
- package/dist/__tests__/time.test.js +349 -28
- package/dist/attribute.cjs +111 -0
- package/dist/attribute.d.cts +5 -0
- package/dist/attribute.d.ts +5 -0
- package/dist/attribute.js +12 -0
- package/dist/{chunk-A4KMGXB3.js → chunk-3UOSPZEP.js} +4 -4
- package/dist/chunk-EEYZG4JJ.js +78 -0
- package/dist/chunk-JQWKLXW6.js +147 -0
- package/dist/{chunk-7JXEY4A2.js → chunk-KYDXF7SU.js} +5 -1
- package/dist/conditions/attribute.cjs +10 -1
- package/dist/conditions/attribute.js +2 -1
- package/dist/conditions/condition.cjs +110 -20
- package/dist/conditions/condition.js +4 -3
- package/dist/conditions/index.cjs +152 -19
- package/dist/conditions/index.d.cts +1 -1
- package/dist/conditions/index.d.ts +1 -1
- package/dist/conditions/index.js +14 -5
- package/dist/conditions/time.cjs +150 -18
- package/dist/conditions/time.d.cts +29 -2
- package/dist/conditions/time.d.ts +29 -2
- package/dist/conditions/time.js +12 -3
- package/dist/index.cjs +209 -19
- package/dist/index.d.cts +2 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js +20 -4
- package/package.json +2 -2
- package/dist/chunk-CEK3SCQO.js +0 -31
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
// src/__tests__/get-attribute-type.test.ts
|
|
4
|
+
var import_types2 = require("@usertour/types");
|
|
5
|
+
|
|
6
|
+
// src/attribute.ts
|
|
7
|
+
var import_types = require("@usertour/types");
|
|
8
|
+
var import_date_fns = require("date-fns");
|
|
9
|
+
|
|
10
|
+
// src/type-utils.ts
|
|
11
|
+
var nativeIsArray = Array.isArray;
|
|
12
|
+
var ObjProto = Object.prototype;
|
|
13
|
+
var objToString = ObjProto.toString;
|
|
14
|
+
var objHasOwn = ObjProto.hasOwnProperty;
|
|
15
|
+
|
|
16
|
+
// src/attribute.ts
|
|
17
|
+
var isValidYear = (date) => {
|
|
18
|
+
const year = date.getFullYear();
|
|
19
|
+
return year >= 1900 && year <= 2100;
|
|
20
|
+
};
|
|
21
|
+
var tryParseDate = (parser) => {
|
|
22
|
+
try {
|
|
23
|
+
const date = parser();
|
|
24
|
+
return (0, import_date_fns.isValid)(date) && isValidYear(date);
|
|
25
|
+
} catch {
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
function isDateString(dateStr) {
|
|
30
|
+
if (!Number.isNaN(Number(dateStr)) || dateStr.length < 10) {
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
const dateFormats = [
|
|
34
|
+
"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'",
|
|
35
|
+
"yyyy-MM-dd'T'HH:mm:ss'Z'",
|
|
36
|
+
"yyyy-MM-dd'T'HH:mm:ss.SSS",
|
|
37
|
+
"yyyy-MM-dd'T'HH:mm:ss",
|
|
38
|
+
"yyyy-MM-dd",
|
|
39
|
+
"MM/dd/yyyy",
|
|
40
|
+
"dd/MM/yyyy",
|
|
41
|
+
"yyyy/MM/dd",
|
|
42
|
+
"MM-dd-yyyy",
|
|
43
|
+
"dd-MM-yyyy"
|
|
44
|
+
];
|
|
45
|
+
if (tryParseDate(() => (0, import_date_fns.parseISO)(dateStr))) {
|
|
46
|
+
return true;
|
|
47
|
+
}
|
|
48
|
+
return dateFormats.some((format) => tryParseDate(() => (0, import_date_fns.parse)(dateStr, format, /* @__PURE__ */ new Date())));
|
|
49
|
+
}
|
|
50
|
+
var getAttributeType = (attribute) => {
|
|
51
|
+
const t = typeof attribute;
|
|
52
|
+
if (t === "number") {
|
|
53
|
+
return import_types.BizAttributeTypes.Number;
|
|
54
|
+
}
|
|
55
|
+
if (t === "string") {
|
|
56
|
+
if (isDateString(attribute)) {
|
|
57
|
+
return import_types.BizAttributeTypes.DateTime;
|
|
58
|
+
}
|
|
59
|
+
return import_types.BizAttributeTypes.String;
|
|
60
|
+
}
|
|
61
|
+
if (t === "boolean") {
|
|
62
|
+
return import_types.BizAttributeTypes.Boolean;
|
|
63
|
+
}
|
|
64
|
+
if (t === "object" && Array.isArray(attribute)) {
|
|
65
|
+
return import_types.BizAttributeTypes.List;
|
|
66
|
+
}
|
|
67
|
+
return import_types.BizAttributeTypes.Nil;
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
// src/__tests__/get-attribute-type.test.ts
|
|
71
|
+
describe("getAttributeType", () => {
|
|
72
|
+
describe("Number type", () => {
|
|
73
|
+
test("should return Number for integer", () => {
|
|
74
|
+
expect(getAttributeType(42)).toBe(import_types2.BizAttributeTypes.Number);
|
|
75
|
+
});
|
|
76
|
+
test("should return Number for float", () => {
|
|
77
|
+
expect(getAttributeType(3.14)).toBe(import_types2.BizAttributeTypes.Number);
|
|
78
|
+
});
|
|
79
|
+
test("should return Number for negative number", () => {
|
|
80
|
+
expect(getAttributeType(-10)).toBe(import_types2.BizAttributeTypes.Number);
|
|
81
|
+
});
|
|
82
|
+
test("should return Number for zero", () => {
|
|
83
|
+
expect(getAttributeType(0)).toBe(import_types2.BizAttributeTypes.Number);
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
describe("String type", () => {
|
|
87
|
+
test("should return String for regular string", () => {
|
|
88
|
+
expect(getAttributeType("hello")).toBe(import_types2.BizAttributeTypes.String);
|
|
89
|
+
});
|
|
90
|
+
test("should return String for string with special characters", () => {
|
|
91
|
+
expect(getAttributeType("Nanjing-user-189327")).toBe(import_types2.BizAttributeTypes.String);
|
|
92
|
+
});
|
|
93
|
+
test("should return String for email-like string", () => {
|
|
94
|
+
expect(getAttributeType("user@example.com")).toBe(import_types2.BizAttributeTypes.String);
|
|
95
|
+
});
|
|
96
|
+
test("should return String for short string (< 10 chars)", () => {
|
|
97
|
+
expect(getAttributeType("short")).toBe(import_types2.BizAttributeTypes.String);
|
|
98
|
+
});
|
|
99
|
+
test("should return String for string that looks like number but is not pure number", () => {
|
|
100
|
+
expect(getAttributeType("123abc")).toBe(import_types2.BizAttributeTypes.String);
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
describe("DateTime type", () => {
|
|
104
|
+
test("should return DateTime for ISO 8601 format with time", () => {
|
|
105
|
+
expect(getAttributeType("2024-12-11T16:00:00.000Z")).toBe(import_types2.BizAttributeTypes.DateTime);
|
|
106
|
+
});
|
|
107
|
+
test("should return DateTime for ISO 8601 format without milliseconds", () => {
|
|
108
|
+
expect(getAttributeType("2024-12-11T16:00:00Z")).toBe(import_types2.BizAttributeTypes.DateTime);
|
|
109
|
+
});
|
|
110
|
+
test("should return DateTime for ISO 8601 format without timezone", () => {
|
|
111
|
+
expect(getAttributeType("2024-12-11T16:00:00")).toBe(import_types2.BizAttributeTypes.DateTime);
|
|
112
|
+
});
|
|
113
|
+
test("should return DateTime for ISO 8601 date only", () => {
|
|
114
|
+
expect(getAttributeType("2024-12-11")).toBe(import_types2.BizAttributeTypes.DateTime);
|
|
115
|
+
});
|
|
116
|
+
test("should return DateTime for MM/DD/YYYY format", () => {
|
|
117
|
+
expect(getAttributeType("12/12/2024")).toBe(import_types2.BizAttributeTypes.DateTime);
|
|
118
|
+
});
|
|
119
|
+
test("should return DateTime for YYYY/MM/DD format", () => {
|
|
120
|
+
expect(getAttributeType("2024/12/11")).toBe(import_types2.BizAttributeTypes.DateTime);
|
|
121
|
+
});
|
|
122
|
+
test("should return DateTime for MM-DD-YYYY format", () => {
|
|
123
|
+
expect(getAttributeType("12-12-2024")).toBe(import_types2.BizAttributeTypes.DateTime);
|
|
124
|
+
});
|
|
125
|
+
test("should return DateTime for DD-MM-YYYY format", () => {
|
|
126
|
+
expect(getAttributeType("11-12-2024")).toBe(import_types2.BizAttributeTypes.DateTime);
|
|
127
|
+
});
|
|
128
|
+
test("should return String for invalid date string", () => {
|
|
129
|
+
expect(getAttributeType("2024-13-45")).toBe(import_types2.BizAttributeTypes.String);
|
|
130
|
+
});
|
|
131
|
+
test("should return String for date string with invalid year (< 1900)", () => {
|
|
132
|
+
expect(getAttributeType("1899-12-11")).toBe(import_types2.BizAttributeTypes.String);
|
|
133
|
+
});
|
|
134
|
+
test("should return String for date string with invalid year (> 2100)", () => {
|
|
135
|
+
expect(getAttributeType("2101-12-11")).toBe(import_types2.BizAttributeTypes.String);
|
|
136
|
+
});
|
|
137
|
+
});
|
|
138
|
+
describe("Boolean type", () => {
|
|
139
|
+
test("should return Boolean for true", () => {
|
|
140
|
+
expect(getAttributeType(true)).toBe(import_types2.BizAttributeTypes.Boolean);
|
|
141
|
+
});
|
|
142
|
+
test("should return Boolean for false", () => {
|
|
143
|
+
expect(getAttributeType(false)).toBe(import_types2.BizAttributeTypes.Boolean);
|
|
144
|
+
});
|
|
145
|
+
});
|
|
146
|
+
describe("List type", () => {
|
|
147
|
+
test("should return List for array of strings", () => {
|
|
148
|
+
expect(getAttributeType(["a", "b", "c"])).toBe(import_types2.BizAttributeTypes.List);
|
|
149
|
+
});
|
|
150
|
+
test("should return List for array of numbers", () => {
|
|
151
|
+
expect(getAttributeType([1, 2, 3])).toBe(import_types2.BizAttributeTypes.List);
|
|
152
|
+
});
|
|
153
|
+
test("should return List for array of mixed types", () => {
|
|
154
|
+
expect(getAttributeType([1, "a", true])).toBe(import_types2.BizAttributeTypes.List);
|
|
155
|
+
});
|
|
156
|
+
test("should return List for empty array", () => {
|
|
157
|
+
expect(getAttributeType([])).toBe(import_types2.BizAttributeTypes.List);
|
|
158
|
+
});
|
|
159
|
+
});
|
|
160
|
+
describe("Nil type", () => {
|
|
161
|
+
test("should return Nil for null", () => {
|
|
162
|
+
expect(getAttributeType(null)).toBe(import_types2.BizAttributeTypes.Nil);
|
|
163
|
+
});
|
|
164
|
+
test("should return Nil for undefined", () => {
|
|
165
|
+
expect(getAttributeType(void 0)).toBe(import_types2.BizAttributeTypes.Nil);
|
|
166
|
+
});
|
|
167
|
+
test("should return Nil for object (non-array)", () => {
|
|
168
|
+
expect(getAttributeType({ key: "value" })).toBe(import_types2.BizAttributeTypes.Nil);
|
|
169
|
+
});
|
|
170
|
+
test("should return Nil for function", () => {
|
|
171
|
+
expect(getAttributeType(() => {
|
|
172
|
+
})).toBe(import_types2.BizAttributeTypes.Nil);
|
|
173
|
+
});
|
|
174
|
+
test("should return Nil for Symbol", () => {
|
|
175
|
+
expect(getAttributeType(Symbol("test"))).toBe(import_types2.BizAttributeTypes.Nil);
|
|
176
|
+
});
|
|
177
|
+
});
|
|
178
|
+
describe("Edge cases", () => {
|
|
179
|
+
test("should return String for string that is a number but length < 10", () => {
|
|
180
|
+
expect(getAttributeType("12345")).toBe(import_types2.BizAttributeTypes.String);
|
|
181
|
+
});
|
|
182
|
+
test("should return String for string that is exactly 9 characters", () => {
|
|
183
|
+
expect(getAttributeType("123456789")).toBe(import_types2.BizAttributeTypes.String);
|
|
184
|
+
});
|
|
185
|
+
test("should return DateTime for string that is exactly 10 characters and valid date", () => {
|
|
186
|
+
expect(getAttributeType("2024-12-11")).toBe(import_types2.BizAttributeTypes.DateTime);
|
|
187
|
+
});
|
|
188
|
+
test("should return String for string that contains date-like pattern but invalid", () => {
|
|
189
|
+
expect(getAttributeType("2024-99-99")).toBe(import_types2.BizAttributeTypes.String);
|
|
190
|
+
});
|
|
191
|
+
test("should return String for string starting with numbers but not a date", () => {
|
|
192
|
+
expect(getAttributeType("2024abc-def-ghi")).toBe(import_types2.BizAttributeTypes.String);
|
|
193
|
+
});
|
|
194
|
+
});
|
|
195
|
+
});
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import {
|
|
2
|
+
getAttributeType
|
|
3
|
+
} from "../chunk-EEYZG4JJ.js";
|
|
4
|
+
import "../chunk-GFH3VWOC.js";
|
|
5
|
+
import "../chunk-XEO3YXBM.js";
|
|
6
|
+
|
|
7
|
+
// src/__tests__/get-attribute-type.test.ts
|
|
8
|
+
import { BizAttributeTypes } from "@usertour/types";
|
|
9
|
+
describe("getAttributeType", () => {
|
|
10
|
+
describe("Number type", () => {
|
|
11
|
+
test("should return Number for integer", () => {
|
|
12
|
+
expect(getAttributeType(42)).toBe(BizAttributeTypes.Number);
|
|
13
|
+
});
|
|
14
|
+
test("should return Number for float", () => {
|
|
15
|
+
expect(getAttributeType(3.14)).toBe(BizAttributeTypes.Number);
|
|
16
|
+
});
|
|
17
|
+
test("should return Number for negative number", () => {
|
|
18
|
+
expect(getAttributeType(-10)).toBe(BizAttributeTypes.Number);
|
|
19
|
+
});
|
|
20
|
+
test("should return Number for zero", () => {
|
|
21
|
+
expect(getAttributeType(0)).toBe(BizAttributeTypes.Number);
|
|
22
|
+
});
|
|
23
|
+
});
|
|
24
|
+
describe("String type", () => {
|
|
25
|
+
test("should return String for regular string", () => {
|
|
26
|
+
expect(getAttributeType("hello")).toBe(BizAttributeTypes.String);
|
|
27
|
+
});
|
|
28
|
+
test("should return String for string with special characters", () => {
|
|
29
|
+
expect(getAttributeType("Nanjing-user-189327")).toBe(BizAttributeTypes.String);
|
|
30
|
+
});
|
|
31
|
+
test("should return String for email-like string", () => {
|
|
32
|
+
expect(getAttributeType("user@example.com")).toBe(BizAttributeTypes.String);
|
|
33
|
+
});
|
|
34
|
+
test("should return String for short string (< 10 chars)", () => {
|
|
35
|
+
expect(getAttributeType("short")).toBe(BizAttributeTypes.String);
|
|
36
|
+
});
|
|
37
|
+
test("should return String for string that looks like number but is not pure number", () => {
|
|
38
|
+
expect(getAttributeType("123abc")).toBe(BizAttributeTypes.String);
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
describe("DateTime type", () => {
|
|
42
|
+
test("should return DateTime for ISO 8601 format with time", () => {
|
|
43
|
+
expect(getAttributeType("2024-12-11T16:00:00.000Z")).toBe(BizAttributeTypes.DateTime);
|
|
44
|
+
});
|
|
45
|
+
test("should return DateTime for ISO 8601 format without milliseconds", () => {
|
|
46
|
+
expect(getAttributeType("2024-12-11T16:00:00Z")).toBe(BizAttributeTypes.DateTime);
|
|
47
|
+
});
|
|
48
|
+
test("should return DateTime for ISO 8601 format without timezone", () => {
|
|
49
|
+
expect(getAttributeType("2024-12-11T16:00:00")).toBe(BizAttributeTypes.DateTime);
|
|
50
|
+
});
|
|
51
|
+
test("should return DateTime for ISO 8601 date only", () => {
|
|
52
|
+
expect(getAttributeType("2024-12-11")).toBe(BizAttributeTypes.DateTime);
|
|
53
|
+
});
|
|
54
|
+
test("should return DateTime for MM/DD/YYYY format", () => {
|
|
55
|
+
expect(getAttributeType("12/12/2024")).toBe(BizAttributeTypes.DateTime);
|
|
56
|
+
});
|
|
57
|
+
test("should return DateTime for YYYY/MM/DD format", () => {
|
|
58
|
+
expect(getAttributeType("2024/12/11")).toBe(BizAttributeTypes.DateTime);
|
|
59
|
+
});
|
|
60
|
+
test("should return DateTime for MM-DD-YYYY format", () => {
|
|
61
|
+
expect(getAttributeType("12-12-2024")).toBe(BizAttributeTypes.DateTime);
|
|
62
|
+
});
|
|
63
|
+
test("should return DateTime for DD-MM-YYYY format", () => {
|
|
64
|
+
expect(getAttributeType("11-12-2024")).toBe(BizAttributeTypes.DateTime);
|
|
65
|
+
});
|
|
66
|
+
test("should return String for invalid date string", () => {
|
|
67
|
+
expect(getAttributeType("2024-13-45")).toBe(BizAttributeTypes.String);
|
|
68
|
+
});
|
|
69
|
+
test("should return String for date string with invalid year (< 1900)", () => {
|
|
70
|
+
expect(getAttributeType("1899-12-11")).toBe(BizAttributeTypes.String);
|
|
71
|
+
});
|
|
72
|
+
test("should return String for date string with invalid year (> 2100)", () => {
|
|
73
|
+
expect(getAttributeType("2101-12-11")).toBe(BizAttributeTypes.String);
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
describe("Boolean type", () => {
|
|
77
|
+
test("should return Boolean for true", () => {
|
|
78
|
+
expect(getAttributeType(true)).toBe(BizAttributeTypes.Boolean);
|
|
79
|
+
});
|
|
80
|
+
test("should return Boolean for false", () => {
|
|
81
|
+
expect(getAttributeType(false)).toBe(BizAttributeTypes.Boolean);
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
describe("List type", () => {
|
|
85
|
+
test("should return List for array of strings", () => {
|
|
86
|
+
expect(getAttributeType(["a", "b", "c"])).toBe(BizAttributeTypes.List);
|
|
87
|
+
});
|
|
88
|
+
test("should return List for array of numbers", () => {
|
|
89
|
+
expect(getAttributeType([1, 2, 3])).toBe(BizAttributeTypes.List);
|
|
90
|
+
});
|
|
91
|
+
test("should return List for array of mixed types", () => {
|
|
92
|
+
expect(getAttributeType([1, "a", true])).toBe(BizAttributeTypes.List);
|
|
93
|
+
});
|
|
94
|
+
test("should return List for empty array", () => {
|
|
95
|
+
expect(getAttributeType([])).toBe(BizAttributeTypes.List);
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
describe("Nil type", () => {
|
|
99
|
+
test("should return Nil for null", () => {
|
|
100
|
+
expect(getAttributeType(null)).toBe(BizAttributeTypes.Nil);
|
|
101
|
+
});
|
|
102
|
+
test("should return Nil for undefined", () => {
|
|
103
|
+
expect(getAttributeType(void 0)).toBe(BizAttributeTypes.Nil);
|
|
104
|
+
});
|
|
105
|
+
test("should return Nil for object (non-array)", () => {
|
|
106
|
+
expect(getAttributeType({ key: "value" })).toBe(BizAttributeTypes.Nil);
|
|
107
|
+
});
|
|
108
|
+
test("should return Nil for function", () => {
|
|
109
|
+
expect(getAttributeType(() => {
|
|
110
|
+
})).toBe(BizAttributeTypes.Nil);
|
|
111
|
+
});
|
|
112
|
+
test("should return Nil for Symbol", () => {
|
|
113
|
+
expect(getAttributeType(Symbol("test"))).toBe(BizAttributeTypes.Nil);
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
describe("Edge cases", () => {
|
|
117
|
+
test("should return String for string that is a number but length < 10", () => {
|
|
118
|
+
expect(getAttributeType("12345")).toBe(BizAttributeTypes.String);
|
|
119
|
+
});
|
|
120
|
+
test("should return String for string that is exactly 9 characters", () => {
|
|
121
|
+
expect(getAttributeType("123456789")).toBe(BizAttributeTypes.String);
|
|
122
|
+
});
|
|
123
|
+
test("should return DateTime for string that is exactly 10 characters and valid date", () => {
|
|
124
|
+
expect(getAttributeType("2024-12-11")).toBe(BizAttributeTypes.DateTime);
|
|
125
|
+
});
|
|
126
|
+
test("should return String for string that contains date-like pattern but invalid", () => {
|
|
127
|
+
expect(getAttributeType("2024-99-99")).toBe(BizAttributeTypes.String);
|
|
128
|
+
});
|
|
129
|
+
test("should return String for string starting with numbers but not a date", () => {
|
|
130
|
+
expect(getAttributeType("2024abc-def-ghi")).toBe(BizAttributeTypes.String);
|
|
131
|
+
});
|
|
132
|
+
});
|
|
133
|
+
});
|