zmdms-utils 0.0.21 → 0.0.23

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,3 @@
1
+ var parseTimeTest = {};
2
+
3
+ export { parseTimeTest as __exports };
@@ -0,0 +1,3 @@
1
+ var test = {};
2
+
3
+ export { test as __exports };
@@ -0,0 +1,25 @@
1
+ type IType = "string" | "object";
2
+ type IEndType = "startOf" | "endOf";
3
+ interface IConfig {
4
+ params: string | number | object;
5
+ format?: string | string[];
6
+ type?: IType | IType[];
7
+ fromKeys?: Array<string | string[]>;
8
+ toKeys?: Array<string | string[]>;
9
+ endType?: Array<IEndType | IEndType[]>;
10
+ }
11
+ /**
12
+ * 解析时间
13
+ * 可能转换的结果
14
+ * 日期对象转字符串
15
+ * Dayjs => "20xx-xx-xx"
16
+ * { key: Dayjs } => { key: "20xx-xx-xx" }
17
+ * { mergeKey: [Dayjs, Dayjs] } => { key1: "20xx-xx-xx", key2: "20xx-xx-xx" }
18
+ * 字符串转日期对象
19
+ * "20xx-xx-xx" => Dayjs
20
+ * { key: "20xx-xx-xx" } => { key: Dayjs }
21
+ * { key1: "20xx-xx-xx", key1: "20xx-xx-xx" } => { mergeKey: [Dayjs, Dayjs] }
22
+ */
23
+ declare function parseTime(config: IConfig): any;
24
+
25
+ export { parseTime as default };
@@ -0,0 +1,170 @@
1
+ import dayjs from 'dayjs';
2
+
3
+ var defaultFormat = "YYYY-MM-DD";
4
+ /**
5
+ * 解析时间
6
+ * 可能转换的结果
7
+ * 日期对象转字符串
8
+ * Dayjs => "20xx-xx-xx"
9
+ * { key: Dayjs } => { key: "20xx-xx-xx" }
10
+ * { mergeKey: [Dayjs, Dayjs] } => { key1: "20xx-xx-xx", key2: "20xx-xx-xx" }
11
+ * 字符串转日期对象
12
+ * "20xx-xx-xx" => Dayjs
13
+ * { key: "20xx-xx-xx" } => { key: Dayjs }
14
+ * { key1: "20xx-xx-xx", key1: "20xx-xx-xx" } => { mergeKey: [Dayjs, Dayjs] }
15
+ */
16
+ function parseTime(config) {
17
+ var params = config.params, _a = config.format, format = _a === void 0 ? defaultFormat : _a, _b = config.type, type = _b === void 0 ? "string" : _b, _c = config.fromKeys, fromKeys = _c === void 0 ? [] : _c, _d = config.toKeys, toKeys = _d === void 0 ? [] : _d, endType = config.endType;
18
+ // 获取最终结果
19
+ var getParseResult = parseByResult({ format: format, type: type });
20
+ // 如果传入的是一个字符串(就是单字段)
21
+ if (typeof params === "string" || typeof params === "number") {
22
+ // 1. 结果类型是一个日期对象
23
+ // 2. 如果结果类型是字符串,那么字符串转字符串可能的一个场景是:将一个日期格式的数据转成另外一种格式的日期
24
+ return getParseResult({
25
+ value: params,
26
+ endType: typeof endType === "string" ? endType : undefined,
27
+ });
28
+ }
29
+ else {
30
+ // 浅拷贝一下
31
+ var newParams_1 = Object.assign({}, params);
32
+ fromKeys.forEach(function (fromKey, index) {
33
+ var toKey = toKeys[index];
34
+ var endTypeItem = Array.isArray(endType) ? endType[index] : endType;
35
+ // 1. 从一个键 转成 一个键
36
+ if (typeof fromKey === "string" && typeof toKey === "string") {
37
+ newParams_1[toKey] = getParseResult({
38
+ value: newParams_1[fromKey],
39
+ index: index,
40
+ endType: endTypeItem,
41
+ });
42
+ if (fromKey !== toKey) {
43
+ // 删除原有key
44
+ Reflect.deleteProperty(newParams_1, fromKey);
45
+ }
46
+ }
47
+ // 2. 从一个键 转成 两个键
48
+ if (typeof fromKey === "string" && typeof toKey === "object") {
49
+ var toKey1 = toKey[0], toKey2 = toKey[1];
50
+ // 初始定义转义类型 是否拼接一天的开始 一天的结束
51
+ var startEndType = "startOf";
52
+ var endEndType = "endOf";
53
+ if (Array.isArray(endTypeItem)) {
54
+ startEndType = endTypeItem[0];
55
+ endEndType = endTypeItem[1];
56
+ }
57
+ newParams_1[toKey1] = getParseResult({
58
+ value: newParams_1[fromKey],
59
+ index: index,
60
+ endType: startEndType,
61
+ });
62
+ newParams_1[toKey2] = getParseResult({
63
+ value: newParams_1[fromKey],
64
+ index: index,
65
+ endType: endEndType,
66
+ });
67
+ if (fromKey !== toKey1 && fromKey !== toKey2) {
68
+ Reflect.deleteProperty(newParams_1, fromKey);
69
+ }
70
+ }
71
+ // 3. 从两个键 转成 一个键
72
+ if (typeof fromKey === "object" && typeof toKey === "string") {
73
+ var fromKey1 = fromKey[0], fromtKey2 = fromKey[1];
74
+ // 初始定义转义类型 是否拼接一天的开始 一天的结束
75
+ var startEndType = "startOf";
76
+ var endEndType = "endOf";
77
+ if (Array.isArray(endTypeItem)) {
78
+ startEndType = endTypeItem[0];
79
+ endEndType = endTypeItem[1];
80
+ }
81
+ newParams_1[toKey] = [
82
+ getParseResult({
83
+ value: newParams_1[fromKey1],
84
+ index: index,
85
+ endType: startEndType,
86
+ }),
87
+ getParseResult({
88
+ value: newParams_1[fromtKey2],
89
+ index: index,
90
+ endType: endEndType,
91
+ }),
92
+ ];
93
+ if (fromKey1 !== toKey) {
94
+ Reflect.deleteProperty(newParams_1, fromKey1);
95
+ }
96
+ if (fromtKey2 !== toKey) {
97
+ Reflect.deleteProperty(newParams_1, fromtKey2);
98
+ }
99
+ }
100
+ // 4. 从两个键 转成 两个键
101
+ if (typeof fromKey === "object" && typeof toKey === "object") {
102
+ var fromKey1 = fromKey[0], fromtKey2 = fromKey[1];
103
+ var toKey1 = toKey[0], toKey2 = toKey[1];
104
+ // 初始定义转义类型 是否拼接一天的开始 一天的结束
105
+ var startEndType = undefined;
106
+ var endEndType = undefined;
107
+ if (Array.isArray(endTypeItem)) {
108
+ startEndType = endTypeItem[0];
109
+ endEndType = endTypeItem[1];
110
+ }
111
+ newParams_1[toKey1] = getParseResult({
112
+ value: newParams_1[fromKey1],
113
+ index: index,
114
+ endType: startEndType,
115
+ });
116
+ newParams_1[toKey2] = getParseResult({
117
+ value: newParams_1[fromtKey2],
118
+ index: index,
119
+ endType: endEndType,
120
+ });
121
+ if (fromKey1 !== toKey1) {
122
+ // 删除原有key
123
+ Reflect.deleteProperty(newParams_1, fromKey1);
124
+ }
125
+ if (fromtKey2 !== toKey2) {
126
+ // 删除原有key
127
+ Reflect.deleteProperty(newParams_1, fromtKey2);
128
+ }
129
+ }
130
+ });
131
+ return newParams_1;
132
+ }
133
+ }
134
+ function parseByResult(option) {
135
+ var _a = option.format, format = _a === void 0 ? defaultFormat : _a, _b = option.type, type = _b === void 0 ? "string" : _b;
136
+ return function (option) {
137
+ var value = option.value, _a = option.index, index = _a === void 0 ? 0 : _a, endType = option.endType;
138
+ if (!value) {
139
+ return undefined;
140
+ }
141
+ var parseType = typeof type === "string" ? type : type[index];
142
+ var parseFormat = typeof format === "string" ? format : format[index];
143
+ var parseEndType = Array.isArray(endType) ? endType[0] : endType;
144
+ if (parseType === "string") {
145
+ var dayjsValue = dayjs.isDayjs(value) ? value : dayjs(value);
146
+ if (parseEndType === "startOf") {
147
+ return dayjsValue
148
+ .startOf("day")
149
+ .format(parseFormat.length === 10 ? "".concat(parseFormat, " HH:mm:ss") : parseFormat);
150
+ }
151
+ if (parseEndType === "endOf") {
152
+ return dayjsValue
153
+ .endOf("day")
154
+ .format(parseFormat.length === 10 ? "".concat(parseFormat, " HH:mm:ss") : parseFormat);
155
+ }
156
+ return dayjsValue.format(parseFormat);
157
+ }
158
+ else {
159
+ if (parseEndType === "startOf") {
160
+ return dayjs(value).startOf("day");
161
+ }
162
+ if (parseEndType === "endOf") {
163
+ return dayjs(value).endOf("day");
164
+ }
165
+ return dayjs(value);
166
+ }
167
+ };
168
+ }
169
+
170
+ export { parseTime as default };
@@ -0,0 +1,2 @@
1
+ import './src/parseTimeTest.js';
2
+ export { __exports as default } from './_virtual/parseTimeTest.js';
@@ -0,0 +1,244 @@
1
+ import dayjs$1 from 'dayjs';
2
+
3
+ const dayjs = dayjs$1;
4
+ const defaultFormat = "YYYY-MM-DD";
5
+ function parseTime(config) {
6
+ const {
7
+ params,
8
+ format = defaultFormat,
9
+ type = "string",
10
+ fromKeys = [],
11
+ toKeys = [],
12
+ endType,
13
+ } = config;
14
+
15
+ // 获取最终结果
16
+ const getParseResult = parseByResult({ format, type });
17
+
18
+ // 如果传入的是一个字符串(就是单字段)
19
+ if (typeof params === "string" || typeof params === "number") {
20
+ // 1. 结果类型是一个日期对象
21
+ // 2. 如果结果类型是字符串,那么字符串转字符串可能的一个场景是:将一个日期格式的数据转成另外一种格式的日期
22
+ return getParseResult({
23
+ value: params,
24
+ endType: typeof endType === "string" ? endType : undefined,
25
+ });
26
+ } else {
27
+ // 浅拷贝一下
28
+ const newParams = Object.assign({}, params);
29
+
30
+ fromKeys.forEach((fromKey, index) => {
31
+ const toKey = toKeys[index];
32
+ const endTypeItem = Array.isArray(endType) ? endType[index] : endType;
33
+ // 1. 从一个键 转成 一个键
34
+ if (typeof fromKey === "string" && typeof toKey === "string") {
35
+ newParams[toKey] = getParseResult({
36
+ value: newParams[fromKey],
37
+ index,
38
+ endType: endTypeItem,
39
+ });
40
+ if (fromKey !== toKey) {
41
+ // 删除原有key
42
+ Reflect.deleteProperty(newParams, fromKey);
43
+ }
44
+ }
45
+ // 2. 从一个键 转成 两个键
46
+ if (typeof fromKey === "string" && typeof toKey === "object") {
47
+ const [toKey1, toKey2] = toKey;
48
+ // 初始定义转义类型 是否拼接一天的开始 一天的结束
49
+ let startEndType = "startOf";
50
+ let endEndType = "endOf";
51
+ if (Array.isArray(endTypeItem)) {
52
+ startEndType = endTypeItem[0];
53
+ endEndType = endTypeItem[1];
54
+ }
55
+ newParams[toKey1] = getParseResult({
56
+ value: newParams[fromKey],
57
+ index,
58
+ endType: startEndType,
59
+ });
60
+ newParams[toKey2] = getParseResult({
61
+ value: newParams[fromKey],
62
+ index,
63
+ endType: endEndType,
64
+ });
65
+ if (fromKey !== toKey1 && fromKey !== toKey2) {
66
+ Reflect.deleteProperty(newParams, fromKey);
67
+ }
68
+ }
69
+ // 3. 从两个键 转成 一个键
70
+ if (typeof fromKey === "object" && typeof toKey === "string") {
71
+ const [fromKey1, fromtKey2] = fromKey;
72
+ // 初始定义转义类型 是否拼接一天的开始 一天的结束
73
+ let startEndType = "startOf";
74
+ let endEndType = "endOf";
75
+ if (Array.isArray(endTypeItem)) {
76
+ startEndType = endTypeItem[0];
77
+ endEndType = endTypeItem[1];
78
+ }
79
+ newParams[toKey] = [
80
+ getParseResult({
81
+ value: newParams[fromKey1],
82
+ index,
83
+ endType: startEndType,
84
+ }),
85
+ getParseResult({
86
+ value: newParams[fromtKey2],
87
+ index,
88
+ endType: endEndType,
89
+ }),
90
+ ];
91
+ if (fromKey1 !== toKey) {
92
+ Reflect.deleteProperty(newParams, fromKey1);
93
+ }
94
+ if (fromtKey2 !== toKey) {
95
+ Reflect.deleteProperty(newParams, fromtKey2);
96
+ }
97
+ }
98
+ // 4. 从两个键 转成 两个键
99
+ if (typeof fromKey === "object" && typeof toKey === "object") {
100
+ const [fromKey1, fromtKey2] = fromKey;
101
+ const [toKey1, toKey2] = toKey;
102
+ // 初始定义转义类型 是否拼接一天的开始 一天的结束
103
+ let startEndType = undefined;
104
+ let endEndType = undefined;
105
+ if (Array.isArray(endTypeItem)) {
106
+ startEndType = endTypeItem[0];
107
+ endEndType = endTypeItem[1];
108
+ }
109
+ newParams[toKey1] = getParseResult({
110
+ value: newParams[fromKey1],
111
+ index,
112
+ endType: startEndType,
113
+ });
114
+ newParams[toKey2] = getParseResult({
115
+ value: newParams[fromtKey2],
116
+ index,
117
+ endType: endEndType,
118
+ });
119
+ if (fromKey1 !== toKey1) {
120
+ // 删除原有key
121
+ Reflect.deleteProperty(newParams, fromKey1);
122
+ }
123
+ if (fromtKey2 !== toKey2) {
124
+ // 删除原有key
125
+ Reflect.deleteProperty(newParams, fromtKey2);
126
+ }
127
+ }
128
+ });
129
+
130
+ return newParams;
131
+ }
132
+ }
133
+
134
+ function parseByResult(option) {
135
+ const { format = defaultFormat, type = "string" } = option;
136
+
137
+ return (option) => {
138
+ const { value, index = 0, endType } = option;
139
+ if (!value) {
140
+ return undefined;
141
+ }
142
+ const parseType = typeof type === "string" ? type : type[index];
143
+ const parseFormat = typeof format === "string" ? format : format[index];
144
+ const parseEndType = Array.isArray(endType) ? endType[0] : endType;
145
+ if (parseType === "string") {
146
+ const dayjsValue = dayjs.isDayjs(value) ? value : dayjs(value);
147
+ if (parseEndType === "startOf") {
148
+ return dayjsValue
149
+ .startOf("day")
150
+ .format(
151
+ parseFormat.length === 10 ? `${parseFormat} HH:mm:ss` : parseFormat
152
+ );
153
+ }
154
+ if (parseEndType === "endOf") {
155
+ return dayjsValue
156
+ .endOf("day")
157
+ .format(
158
+ parseFormat.length === 10 ? `${parseFormat} HH:mm:ss` : parseFormat
159
+ );
160
+ }
161
+ return dayjsValue.format(parseFormat);
162
+ } else {
163
+ if (parseEndType === "startOf") {
164
+ return dayjs(value).startOf("day");
165
+ }
166
+ if (parseEndType === "endOf") {
167
+ return dayjs(value).endOf("day");
168
+ }
169
+ return dayjs(value);
170
+ }
171
+ };
172
+ }
173
+
174
+ // 处理单字段
175
+ // 时间戳处理
176
+ // console.log(
177
+ // parseTime({
178
+ // params: Date.now(),
179
+ // })
180
+ // );
181
+ // 将日期格式字符串转换成dayjs
182
+ // console.log(
183
+ // parseTime({
184
+ // params: "2023-09-22",
185
+ // type: "object",
186
+ // })
187
+ // );
188
+ // 添加一天的开始 结束
189
+ // console.log(
190
+ // parseTime({
191
+ // params: "2023-09-22",
192
+ // endType: "startOf",
193
+ // })
194
+ // );
195
+ // console.log(
196
+ // parseTime({
197
+ // params: "2023-09-22",
198
+ // endType: "endOf",
199
+ // })
200
+ // );
201
+
202
+ // 多字段用例
203
+ console.log(
204
+ parseTime({
205
+ params: { time: "2023-08-11" },
206
+ fromKeys: ["time"],
207
+ toKeys: ["resultTime"],
208
+ endType: "endOf",
209
+ })
210
+ );
211
+ console.log(
212
+ parseTime({
213
+ params: { time: "2023-08-11", time1: "2023-08-12" },
214
+ fromKeys: ["time", "time1"],
215
+ toKeys: ["resultTime", "resultTime2"],
216
+ endType: ["startOf", "endOf"],
217
+ })
218
+ );
219
+ console.log(
220
+ parseTime({
221
+ params: { time: "2023-08-11", time1: "2023-08-12" },
222
+ fromKeys: [["time", "time1"]],
223
+ toKeys: ["resultTime"],
224
+ type: "object",
225
+ endType: ["startOf", "endOf"],
226
+ })
227
+ );
228
+ console.log(
229
+ parseTime({
230
+ params: {
231
+ time: "2023-08-11",
232
+ time1: "2023-08-12",
233
+ time2: "2023-08-08",
234
+ time3: "2023-08-01",
235
+ },
236
+ fromKeys: [["time", "time1"], "time2", "time3"],
237
+ toKeys: ["resultTime", ["time2Start", "time2End"], "time3Result"],
238
+ // type: "object",
239
+ endType: [
240
+ ["endOf", "endOf"],
241
+ ["startOf", "startOf"],
242
+ ],
243
+ })
244
+ );
@@ -0,0 +1,71 @@
1
+ import dayjs$1 from 'dayjs';
2
+
3
+ function unescapeString(input) {
4
+ if (!input) {
5
+ return "";
6
+ }
7
+
8
+ const escapedChars = {
9
+ "&amp;": "&",
10
+ "&lt;": "<",
11
+ "&gt;": ">",
12
+ "&quot;": '"',
13
+ "&#039;": "'",
14
+ };
15
+
16
+ return input.replace(/&(amp|lt|gt|quot|#039);/g, (match) => {
17
+ return escapedChars[match] || match;
18
+ });
19
+ }
20
+
21
+ function dangerouslySetXss(input) {
22
+ if (!input) {
23
+ return "";
24
+ }
25
+ const escapedChars = {
26
+ "&lt;": "<",
27
+ "&gt;": ">",
28
+ "&nbsp;": " ",
29
+ "&amp;": "&",
30
+ "&quot;": '"',
31
+ };
32
+
33
+ return input.replace(/&(amp|lt|gt|quot|nbsp);/g, (match) => {
34
+ return escapedChars[match] || match;
35
+ });
36
+ }
37
+
38
+ function dangerouslySetXss1(input) {
39
+ if (!input) {
40
+ return "";
41
+ }
42
+ const escapedChars = {
43
+ lt: "<",
44
+ gt: ">",
45
+ nbsp: " ",
46
+ amp: "&",
47
+ quot: '"',
48
+ };
49
+
50
+ return input.replace(/&(amp|lt|gt|quot|nbsp);/gi, (match, t) => {
51
+ return escapedChars[t] || match;
52
+ });
53
+ }
54
+
55
+ console.log(unescapeString("&amp;&amp;&amp;啊你"));
56
+
57
+ console.log(dangerouslySetXss("&amp;&amp;&amp;啊你"));
58
+
59
+ console.log(dangerouslySetXss1("&amp;&amp;&amp;啊你"));
60
+ console.log(unescapeString("1、aa&gt;80%\n2、&gt;&lt;\n3、ss&lt;80%"));
61
+ console.log(dangerouslySetXss("1、aa&gt;80%\n2、&gt;&lt;\n3、ss&lt;80%"));
62
+ console.log(dangerouslySetXss1("1、aa&gt;80%\n2、&gt;&lt;\n3、ss&lt;80%"));
63
+
64
+ const dayjs = dayjs$1;
65
+
66
+ const a = dayjs("2023-01-01 11:11").startOf("day");
67
+ const formattedDate = a.format("YYYY-MM-DD HH:mm:ss");
68
+
69
+ const b = dayjs("2023-01-01").endOf("day");
70
+ const formattedDate1 = b.format("YYYY-MM-DD HH:mm:ss");
71
+ console.log(formattedDate, formattedDate1);
package/dist/es/test.js CHANGED
@@ -1,158 +1,2 @@
1
- function unescapeString(input) {
2
- if (!input) {
3
- return "";
4
- }
5
-
6
- const escapedChars = {
7
- "&amp;": "&",
8
- "&lt;": "<",
9
- "&gt;": ">",
10
- "&quot;": '"',
11
- "&#039;": "'",
12
- };
13
-
14
- return input.replace(/&(amp|lt|gt|quot|#039);/g, (match) => {
15
- return escapedChars[match] || match;
16
- });
17
- }
18
-
19
- function dangerouslySetXss(input) {
20
- if (!input) {
21
- return "";
22
- }
23
- const escapedChars = {
24
- "&lt;": "<",
25
- "&gt;": ">",
26
- "&nbsp;": " ",
27
- "&amp;": "&",
28
- "&quot;": '"',
29
- };
30
-
31
- return input.replace(/&(amp|lt|gt|quot|nbsp);/g, (match) => {
32
- return escapedChars[match] || match;
33
- });
34
- }
35
-
36
- function dangerouslySetXss1(input) {
37
- if (!input) {
38
- return "";
39
- }
40
- const escapedChars = {
41
- lt: "<",
42
- gt: ">",
43
- nbsp: " ",
44
- amp: "&",
45
- quot: '"',
46
- };
47
-
48
- return input.replace(/&(amp|lt|gt|quot|nbsp);/gi, (match, t) => {
49
- return escapedChars[t] || match;
50
- });
51
- }
52
-
53
- console.log(unescapeString("&amp;&amp;&amp;啊你"));
54
-
55
- console.log(dangerouslySetXss("&amp;&amp;&amp;啊你"));
56
-
57
- console.log(dangerouslySetXss1("&amp;&amp;&amp;啊你"));
58
- console.log(unescapeString("1、aa&gt;80%\n2、&gt;&lt;\n3、ss&lt;80%"));
59
- console.log(dangerouslySetXss("1、aa&gt;80%\n2、&gt;&lt;\n3、ss&lt;80%"));
60
- console.log(dangerouslySetXss1("1、aa&gt;80%\n2、&gt;&lt;\n3、ss&lt;80%"));
61
-
62
- // functions and constants
63
- // console.log("functions and constants");
64
- // print(math.round(math.e, 3)); // 2.718
65
- // print(math.atan2(3, -3) / math.pi); // 0.75
66
- // print(math.log(10000, 10)); // 4
67
- // print(math.sqrt(-4)); // 2i
68
- // print(
69
- // math.pow(
70
- // [
71
- // [-1, 2],
72
- // [3, 1],
73
- // ],
74
- // 2
75
- // )
76
- // ); // [[7, 0], [0, 7]]
77
- // print(math.derivative("x^2 + x", "x")); // 2 * x + 1
78
- // console.log();
79
-
80
- // // expressions
81
- // console.log("expressions");
82
- // print(math.evaluate("1.2 * (2 + 4.5)")); // 7.8
83
- // print(math.evaluate("12.7 cm to inch")); // 5 inch
84
- // print(math.evaluate("sin(45 deg) ^ 2")); // 0.5
85
- // print(math.evaluate("9 / 3 + 2i")); // 3 + 2i
86
- // print(math.evaluate("det([-1, 2; 3, 1])")); // -7
87
- // print(math.evaluate("NaN + '0.2'")); // -7
88
- // console.log();
89
-
90
- // // chained operations
91
- // console.log("chained operations");
92
- // const a = math.chain(3).add(4).multiply(2).done();
93
- // print(a); // 14
94
- // console.log();
95
-
96
- // // mixed use of different data types in functions
97
- // console.log("mixed use of data types");
98
- // print(math.add(4, [5, 6])); // number + Array, [9, 10]
99
- // print(math.multiply(math.unit("5 mm"), 3)); // Unit * number, 15 mm
100
- // print(math.subtract([2, 3, 4], 5)); // Array - number, [-3, -2, -1]
101
- // print(math.add(math.matrix([2, 3]), [4, 5])); // Matrix + Array, [6, 8]
102
- // console.log();
103
-
104
- // /**
105
- // * Helper function to output a value in the console. Value will be formatted.
106
- // * @param {*} value
107
- // */
108
- // function print(value) {
109
- // const precision = 14;
110
- // console.log(math.format(value, precision));
111
- // }
112
-
113
- // const math = require("mathjs");
114
- // function print(value) {
115
- // const precision = 14;
116
- // console.log(math.format(value, precision));
117
- // }
118
-
119
- // console.log(performance.now());
120
- // console.log(math.add(0.1, 0.2));
121
- // console.log(performance.now());
122
- // console.log(performance.now());
123
- // console.log(0.1 + 0.2);
124
- // console.log(performance.now());
125
- // const BigNumber = require("bignumber.js");
126
- // console.log(performance.now());
127
- // const num1 = new BigNumber(0.1);
128
- // const num2 = new BigNumber(0.2);
129
-
130
- // const result = num1.plus(num2);
131
-
132
- // console.log(result.toString());
133
-
134
- // console.log(performance.now());
135
-
136
- // const Decimal = require("decimal.js");
137
-
138
- // const num1 = new Decimal("0.1");
139
-
140
- // console.log(num1.add(0.21).toNumber(), typeof num1.add(0.21).toNumber());
141
-
142
- // console.log(num1.sub(0.21).toNumber(), typeof num1.sub(0.21).toNumber());
143
-
144
- // console.log(num1.mul(0.21).toNumber(), typeof num1.mul(0.21).toNumber());
145
- // Decimal.set({ precision: 5 });
146
- // console.log(
147
- // new Decimal(1).div(1.1).toNumber(),
148
- // typeof new Decimal(1).div(1.1).toNumber()
149
- // );
150
- // console.log(
151
- // new Decimal(1).dividedBy(1.1).toNumber(),
152
- // typeof new Decimal(1).dividedBy(1.1).toNumber()
153
- // );
154
-
155
- // console.log(
156
- // Number(new Decimal(1.1).toFixed(2)),
157
- // typeof Number(new Decimal(1.1).toFixed(2)).toFixed(2)
158
- // );
1
+ import './src/test.js';
2
+ export { __exports as default } from './_virtual/test.js';
package/dist/index.d.ts CHANGED
@@ -10,4 +10,5 @@ export { divide, exactRound, formatUnit, minus, plus, times } from './es/math.js
10
10
  export { validatePassword } from './es/passwordValidate.js';
11
11
  export { emailValidate, idCardValidate, morePhoneValidate, phoneValidate, strLenValidate } from './es/validate.js';
12
12
  export { batchDownloadFiles, createDeleteFileLink, createDownloadLink, createOriginalLink, createThumbnailLink, createUploadFileLink, downloadFile, exportBolb, previewFile } from './es/file.js';
13
+ export { default as parseTime } from './es/parseTime.js';
13
14
  export { EMITTER_CCP_KEY, EMITTER_PCC_KEY, EMITTER_TYPE } from './es/constants.js';
package/dist/index.js CHANGED
@@ -10,4 +10,5 @@ export { divide, exactRound, formatUnit, minus, plus, times } from './es/math.js
10
10
  export { validatePassword } from './es/passwordValidate.js';
11
11
  export { emailValidate, idCardValidate, morePhoneValidate, phoneValidate, strLenValidate } from './es/validate.js';
12
12
  export { batchDownloadFiles, createDeleteFileLink, createDownloadLink, createOriginalLink, createThumbnailLink, createUploadFileLink, downloadFile, exportBolb, previewFile } from './es/file.js';
13
+ export { default as parseTime } from './es/parseTime.js';
13
14
  export { EMITTER_CCP_KEY, EMITTER_PCC_KEY, EMITTER_TYPE } from './es/constants.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zmdms-utils",
3
- "version": "0.0.21",
3
+ "version": "0.0.23",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.js",
@@ -15,7 +15,8 @@
15
15
  "peerDependencies": {
16
16
  "axios": "^1.4.0",
17
17
  "crypto-js": "^4.1.1",
18
- "qiankun": "^2.10.8"
18
+ "qiankun": "^2.10.8",
19
+ "dayjs": "^1.11.7"
19
20
  },
20
21
  "dependencies": {
21
22
  "decimal.js": "^10.4.3",
@@ -28,6 +29,7 @@
28
29
  "@types/crypto-js": "^4.1.1",
29
30
  "axios": "^1.4.0",
30
31
  "crypto-js": "^4.1.1",
32
+ "dayjs": "^1.11.10",
31
33
  "qiankun": "^2.10.8",
32
34
  "rimraf": "^4.4.1",
33
35
  "rollup": "^3.20.2",
package/src/index.ts CHANGED
@@ -39,6 +39,10 @@ export {
39
39
  createUploadFileLink,
40
40
  createDeleteFileLink,
41
41
  } from "./file";
42
+ /**
43
+ * 时间处理函数
44
+ */
45
+ export { default as parseTime } from "./parseTime";
42
46
 
43
47
  /**
44
48
  * 常量相关
@@ -0,0 +1,210 @@
1
+ import dayjs from "dayjs";
2
+
3
+ const defaultFormat = "YYYY-MM-DD";
4
+
5
+ type IType = "string" | "object";
6
+ // 传入要转换成当天的第一秒 还是当天的最后一秒
7
+ type IEndType = "startOf" | "endOf";
8
+ interface IConfig {
9
+ // 需要转换的数据
10
+ params: string | number | object;
11
+ // 格式
12
+ format?: string | string[];
13
+ // 结果类型
14
+ type?: IType | IType[];
15
+ // 转换前的字段
16
+ fromKeys?: Array<string | string[]>;
17
+ // 转换后的字段
18
+ toKeys?: Array<string | string[]>;
19
+ // 末尾是要接 今日开始 今日结束 无
20
+ endType?: Array<IEndType | IEndType[]>;
21
+ }
22
+ /**
23
+ * 解析时间
24
+ * 可能转换的结果
25
+ * 日期对象转字符串
26
+ * Dayjs => "20xx-xx-xx"
27
+ * { key: Dayjs } => { key: "20xx-xx-xx" }
28
+ * { mergeKey: [Dayjs, Dayjs] } => { key1: "20xx-xx-xx", key2: "20xx-xx-xx" }
29
+ * 字符串转日期对象
30
+ * "20xx-xx-xx" => Dayjs
31
+ * { key: "20xx-xx-xx" } => { key: Dayjs }
32
+ * { key1: "20xx-xx-xx", key1: "20xx-xx-xx" } => { mergeKey: [Dayjs, Dayjs] }
33
+ */
34
+ export default function parseTime(config: IConfig) {
35
+ const {
36
+ params,
37
+ format = defaultFormat,
38
+ type = "string",
39
+ fromKeys = [],
40
+ toKeys = [],
41
+ endType,
42
+ } = config;
43
+
44
+ // 获取最终结果
45
+ const getParseResult = parseByResult({ format, type });
46
+
47
+ // 如果传入的是一个字符串(就是单字段)
48
+ if (typeof params === "string" || typeof params === "number") {
49
+ // 1. 结果类型是一个日期对象
50
+ // 2. 如果结果类型是字符串,那么字符串转字符串可能的一个场景是:将一个日期格式的数据转成另外一种格式的日期
51
+ return getParseResult({
52
+ value: params,
53
+ endType: typeof endType === "string" ? endType : undefined,
54
+ });
55
+ } else {
56
+ // 浅拷贝一下
57
+ const newParams: any = Object.assign({}, params);
58
+
59
+ fromKeys.forEach((fromKey, index) => {
60
+ const toKey = toKeys[index];
61
+ const endTypeItem = Array.isArray(endType) ? endType[index] : endType;
62
+ // 1. 从一个键 转成 一个键
63
+ if (typeof fromKey === "string" && typeof toKey === "string") {
64
+ newParams[toKey] = getParseResult({
65
+ value: newParams[fromKey],
66
+ index,
67
+ endType: endTypeItem,
68
+ });
69
+ if (fromKey !== toKey) {
70
+ // 删除原有key
71
+ Reflect.deleteProperty(newParams, fromKey);
72
+ }
73
+ }
74
+ // 2. 从一个键 转成 两个键
75
+ if (typeof fromKey === "string" && typeof toKey === "object") {
76
+ const [toKey1, toKey2] = toKey;
77
+ // 初始定义转义类型 是否拼接一天的开始 一天的结束
78
+ let startEndType: IEndType | undefined = "startOf";
79
+ let endEndType: IEndType | undefined = "endOf";
80
+ if (Array.isArray(endTypeItem)) {
81
+ startEndType = endTypeItem[0];
82
+ endEndType = endTypeItem[1];
83
+ }
84
+ newParams[toKey1] = getParseResult({
85
+ value: newParams[fromKey],
86
+ index,
87
+ endType: startEndType,
88
+ });
89
+ newParams[toKey2] = getParseResult({
90
+ value: newParams[fromKey],
91
+ index,
92
+ endType: endEndType,
93
+ });
94
+ if (fromKey !== toKey1 && fromKey !== toKey2) {
95
+ Reflect.deleteProperty(newParams, fromKey);
96
+ }
97
+ }
98
+ // 3. 从两个键 转成 一个键
99
+ if (typeof fromKey === "object" && typeof toKey === "string") {
100
+ const [fromKey1, fromtKey2] = fromKey;
101
+ // 初始定义转义类型 是否拼接一天的开始 一天的结束
102
+ let startEndType: IEndType | undefined = "startOf";
103
+ let endEndType: IEndType | undefined = "endOf";
104
+ if (Array.isArray(endTypeItem)) {
105
+ startEndType = endTypeItem[0];
106
+ endEndType = endTypeItem[1];
107
+ }
108
+ newParams[toKey] = [
109
+ getParseResult({
110
+ value: newParams[fromKey1],
111
+ index,
112
+ endType: startEndType,
113
+ }),
114
+ getParseResult({
115
+ value: newParams[fromtKey2],
116
+ index,
117
+ endType: endEndType,
118
+ }),
119
+ ];
120
+ if (fromKey1 !== toKey) {
121
+ Reflect.deleteProperty(newParams, fromKey1);
122
+ }
123
+ if (fromtKey2 !== toKey) {
124
+ Reflect.deleteProperty(newParams, fromtKey2);
125
+ }
126
+ }
127
+ // 4. 从两个键 转成 两个键
128
+ if (typeof fromKey === "object" && typeof toKey === "object") {
129
+ const [fromKey1, fromtKey2] = fromKey;
130
+ const [toKey1, toKey2] = toKey;
131
+ // 初始定义转义类型 是否拼接一天的开始 一天的结束
132
+ let startEndType: IEndType | undefined = undefined;
133
+ let endEndType: IEndType | undefined = undefined;
134
+ if (Array.isArray(endTypeItem)) {
135
+ startEndType = endTypeItem[0];
136
+ endEndType = endTypeItem[1];
137
+ }
138
+ newParams[toKey1] = getParseResult({
139
+ value: newParams[fromKey1],
140
+ index,
141
+ endType: startEndType,
142
+ });
143
+ newParams[toKey2] = getParseResult({
144
+ value: newParams[fromtKey2],
145
+ index,
146
+ endType: endEndType,
147
+ });
148
+ if (fromKey1 !== toKey1) {
149
+ // 删除原有key
150
+ Reflect.deleteProperty(newParams, fromKey1);
151
+ }
152
+ if (fromtKey2 !== toKey2) {
153
+ // 删除原有key
154
+ Reflect.deleteProperty(newParams, fromtKey2);
155
+ }
156
+ }
157
+ });
158
+
159
+ return newParams;
160
+ }
161
+ }
162
+
163
+ interface IProps {
164
+ format?: string | string[];
165
+ type?: IType | IType[];
166
+ }
167
+ interface IResultProps {
168
+ value?: any;
169
+ index?: number;
170
+ endType?: IEndType | IEndType[];
171
+ }
172
+ function parseByResult(option: IProps) {
173
+ const { format = defaultFormat, type = "string" } = option;
174
+
175
+ return (option: IResultProps) => {
176
+ const { value, index = 0, endType } = option;
177
+ if (!value) {
178
+ return undefined;
179
+ }
180
+ const parseType = typeof type === "string" ? type : type[index];
181
+ const parseFormat = typeof format === "string" ? format : format[index];
182
+ const parseEndType = Array.isArray(endType) ? endType[0] : endType;
183
+ if (parseType === "string") {
184
+ const dayjsValue = dayjs.isDayjs(value) ? value : dayjs(value);
185
+ if (parseEndType === "startOf") {
186
+ return dayjsValue
187
+ .startOf("day")
188
+ .format(
189
+ parseFormat.length === 10 ? `${parseFormat} HH:mm:ss` : parseFormat
190
+ );
191
+ }
192
+ if (parseEndType === "endOf") {
193
+ return dayjsValue
194
+ .endOf("day")
195
+ .format(
196
+ parseFormat.length === 10 ? `${parseFormat} HH:mm:ss` : parseFormat
197
+ );
198
+ }
199
+ return dayjsValue.format(parseFormat);
200
+ } else {
201
+ if (parseEndType === "startOf") {
202
+ return dayjs(value).startOf("day");
203
+ }
204
+ if (parseEndType === "endOf") {
205
+ return dayjs(value).endOf("day");
206
+ }
207
+ return dayjs(value);
208
+ }
209
+ };
210
+ }
@@ -0,0 +1,242 @@
1
+ const dayjs = require("dayjs");
2
+ const defaultFormat = "YYYY-MM-DD";
3
+ function parseTime(config) {
4
+ const {
5
+ params,
6
+ format = defaultFormat,
7
+ type = "string",
8
+ fromKeys = [],
9
+ toKeys = [],
10
+ endType,
11
+ } = config;
12
+
13
+ // 获取最终结果
14
+ const getParseResult = parseByResult({ format, type });
15
+
16
+ // 如果传入的是一个字符串(就是单字段)
17
+ if (typeof params === "string" || typeof params === "number") {
18
+ // 1. 结果类型是一个日期对象
19
+ // 2. 如果结果类型是字符串,那么字符串转字符串可能的一个场景是:将一个日期格式的数据转成另外一种格式的日期
20
+ return getParseResult({
21
+ value: params,
22
+ endType: typeof endType === "string" ? endType : undefined,
23
+ });
24
+ } else {
25
+ // 浅拷贝一下
26
+ const newParams = Object.assign({}, params);
27
+
28
+ fromKeys.forEach((fromKey, index) => {
29
+ const toKey = toKeys[index];
30
+ const endTypeItem = Array.isArray(endType) ? endType[index] : endType;
31
+ // 1. 从一个键 转成 一个键
32
+ if (typeof fromKey === "string" && typeof toKey === "string") {
33
+ newParams[toKey] = getParseResult({
34
+ value: newParams[fromKey],
35
+ index,
36
+ endType: endTypeItem,
37
+ });
38
+ if (fromKey !== toKey) {
39
+ // 删除原有key
40
+ Reflect.deleteProperty(newParams, fromKey);
41
+ }
42
+ }
43
+ // 2. 从一个键 转成 两个键
44
+ if (typeof fromKey === "string" && typeof toKey === "object") {
45
+ const [toKey1, toKey2] = toKey;
46
+ // 初始定义转义类型 是否拼接一天的开始 一天的结束
47
+ let startEndType = "startOf";
48
+ let endEndType = "endOf";
49
+ if (Array.isArray(endTypeItem)) {
50
+ startEndType = endTypeItem[0];
51
+ endEndType = endTypeItem[1];
52
+ }
53
+ newParams[toKey1] = getParseResult({
54
+ value: newParams[fromKey],
55
+ index,
56
+ endType: startEndType,
57
+ });
58
+ newParams[toKey2] = getParseResult({
59
+ value: newParams[fromKey],
60
+ index,
61
+ endType: endEndType,
62
+ });
63
+ if (fromKey !== toKey1 && fromKey !== toKey2) {
64
+ Reflect.deleteProperty(newParams, fromKey);
65
+ }
66
+ }
67
+ // 3. 从两个键 转成 一个键
68
+ if (typeof fromKey === "object" && typeof toKey === "string") {
69
+ const [fromKey1, fromtKey2] = fromKey;
70
+ // 初始定义转义类型 是否拼接一天的开始 一天的结束
71
+ let startEndType = "startOf";
72
+ let endEndType = "endOf";
73
+ if (Array.isArray(endTypeItem)) {
74
+ startEndType = endTypeItem[0];
75
+ endEndType = endTypeItem[1];
76
+ }
77
+ newParams[toKey] = [
78
+ getParseResult({
79
+ value: newParams[fromKey1],
80
+ index,
81
+ endType: startEndType,
82
+ }),
83
+ getParseResult({
84
+ value: newParams[fromtKey2],
85
+ index,
86
+ endType: endEndType,
87
+ }),
88
+ ];
89
+ if (fromKey1 !== toKey) {
90
+ Reflect.deleteProperty(newParams, fromKey1);
91
+ }
92
+ if (fromtKey2 !== toKey) {
93
+ Reflect.deleteProperty(newParams, fromtKey2);
94
+ }
95
+ }
96
+ // 4. 从两个键 转成 两个键
97
+ if (typeof fromKey === "object" && typeof toKey === "object") {
98
+ const [fromKey1, fromtKey2] = fromKey;
99
+ const [toKey1, toKey2] = toKey;
100
+ // 初始定义转义类型 是否拼接一天的开始 一天的结束
101
+ let startEndType = undefined;
102
+ let endEndType = undefined;
103
+ if (Array.isArray(endTypeItem)) {
104
+ startEndType = endTypeItem[0];
105
+ endEndType = endTypeItem[1];
106
+ }
107
+ newParams[toKey1] = getParseResult({
108
+ value: newParams[fromKey1],
109
+ index,
110
+ endType: startEndType,
111
+ });
112
+ newParams[toKey2] = getParseResult({
113
+ value: newParams[fromtKey2],
114
+ index,
115
+ endType: endEndType,
116
+ });
117
+ if (fromKey1 !== toKey1) {
118
+ // 删除原有key
119
+ Reflect.deleteProperty(newParams, fromKey1);
120
+ }
121
+ if (fromtKey2 !== toKey2) {
122
+ // 删除原有key
123
+ Reflect.deleteProperty(newParams, fromtKey2);
124
+ }
125
+ }
126
+ });
127
+
128
+ return newParams;
129
+ }
130
+ }
131
+
132
+ function parseByResult(option) {
133
+ const { format = defaultFormat, type = "string" } = option;
134
+
135
+ return (option) => {
136
+ const { value, index = 0, endType } = option;
137
+ if (!value) {
138
+ return undefined;
139
+ }
140
+ const parseType = typeof type === "string" ? type : type[index];
141
+ const parseFormat = typeof format === "string" ? format : format[index];
142
+ const parseEndType = Array.isArray(endType) ? endType[0] : endType;
143
+ if (parseType === "string") {
144
+ const dayjsValue = dayjs.isDayjs(value) ? value : dayjs(value);
145
+ if (parseEndType === "startOf") {
146
+ return dayjsValue
147
+ .startOf("day")
148
+ .format(
149
+ parseFormat.length === 10 ? `${parseFormat} HH:mm:ss` : parseFormat
150
+ );
151
+ }
152
+ if (parseEndType === "endOf") {
153
+ return dayjsValue
154
+ .endOf("day")
155
+ .format(
156
+ parseFormat.length === 10 ? `${parseFormat} HH:mm:ss` : parseFormat
157
+ );
158
+ }
159
+ return dayjsValue.format(parseFormat);
160
+ } else {
161
+ if (parseEndType === "startOf") {
162
+ return dayjs(value).startOf("day");
163
+ }
164
+ if (parseEndType === "endOf") {
165
+ return dayjs(value).endOf("day");
166
+ }
167
+ return dayjs(value);
168
+ }
169
+ };
170
+ }
171
+
172
+ // 处理单字段
173
+ // 时间戳处理
174
+ // console.log(
175
+ // parseTime({
176
+ // params: Date.now(),
177
+ // })
178
+ // );
179
+ // 将日期格式字符串转换成dayjs
180
+ // console.log(
181
+ // parseTime({
182
+ // params: "2023-09-22",
183
+ // type: "object",
184
+ // })
185
+ // );
186
+ // 添加一天的开始 结束
187
+ // console.log(
188
+ // parseTime({
189
+ // params: "2023-09-22",
190
+ // endType: "startOf",
191
+ // })
192
+ // );
193
+ // console.log(
194
+ // parseTime({
195
+ // params: "2023-09-22",
196
+ // endType: "endOf",
197
+ // })
198
+ // );
199
+
200
+ // 多字段用例
201
+ console.log(
202
+ parseTime({
203
+ params: { time: "2023-08-11" },
204
+ fromKeys: ["time"],
205
+ toKeys: ["resultTime"],
206
+ endType: "endOf",
207
+ })
208
+ );
209
+ console.log(
210
+ parseTime({
211
+ params: { time: "2023-08-11", time1: "2023-08-12" },
212
+ fromKeys: ["time", "time1"],
213
+ toKeys: ["resultTime", "resultTime2"],
214
+ endType: ["startOf", "endOf"],
215
+ })
216
+ );
217
+ console.log(
218
+ parseTime({
219
+ params: { time: "2023-08-11", time1: "2023-08-12" },
220
+ fromKeys: [["time", "time1"]],
221
+ toKeys: ["resultTime"],
222
+ type: "object",
223
+ endType: ["startOf", "endOf"],
224
+ })
225
+ );
226
+ console.log(
227
+ parseTime({
228
+ params: {
229
+ time: "2023-08-11",
230
+ time1: "2023-08-12",
231
+ time2: "2023-08-08",
232
+ time3: "2023-08-01",
233
+ },
234
+ fromKeys: [["time", "time1"], "time2", "time3"],
235
+ toKeys: ["resultTime", ["time2Start", "time2End"], "time3Result"],
236
+ // type: "object",
237
+ endType: [
238
+ ["endOf", "endOf"],
239
+ ["startOf", "startOf"],
240
+ ],
241
+ })
242
+ );
package/src/test.js CHANGED
@@ -59,6 +59,15 @@ console.log(unescapeString("1、aa&gt;80%\n2、&gt;&lt;\n3、ss&lt;80%"));
59
59
  console.log(dangerouslySetXss("1、aa&gt;80%\n2、&gt;&lt;\n3、ss&lt;80%"));
60
60
  console.log(dangerouslySetXss1("1、aa&gt;80%\n2、&gt;&lt;\n3、ss&lt;80%"));
61
61
 
62
+ const dayjs = require("dayjs");
63
+
64
+ const a = dayjs("2023-01-01 11:11").startOf("day");
65
+ const formattedDate = a.format("YYYY-MM-DD HH:mm:ss");
66
+
67
+ const b = dayjs("2023-01-01").endOf("day");
68
+ const formattedDate1 = b.format("YYYY-MM-DD HH:mm:ss");
69
+ console.log(formattedDate, formattedDate1);
70
+
62
71
  // functions and constants
63
72
  // console.log("functions and constants");
64
73
  // print(math.round(math.e, 3)); // 2.718