zmdms-utils 0.0.21 → 0.0.22
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/es/_virtual/parseTimeTest.js +3 -0
- package/dist/es/_virtual/test.js +3 -0
- package/dist/es/parseTime.d.ts +25 -0
- package/dist/es/parseTime.js +156 -0
- package/dist/es/parseTimeTest.js +2 -0
- package/dist/es/src/parseTimeTest.js +230 -0
- package/dist/es/src/test.js +71 -0
- package/dist/es/test.js +2 -158
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/package.json +4 -2
- package/src/index.ts +4 -0
- package/src/parseTime.ts +196 -0
- package/src/parseTimeTest.js +228 -0
- package/src/test.js +9 -0
|
@@ -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,156 @@
|
|
|
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
|
+
// 删除原有key
|
|
43
|
+
Reflect.deleteProperty(newParams_1, fromKey);
|
|
44
|
+
}
|
|
45
|
+
// 2. 从一个键 转成 两个键
|
|
46
|
+
if (typeof fromKey === "string" && typeof toKey === "object") {
|
|
47
|
+
var toKey1 = toKey[0], toKey2 = toKey[1];
|
|
48
|
+
// 初始定义转义类型 是否拼接一天的开始 一天的结束
|
|
49
|
+
var startEndType = "startOf";
|
|
50
|
+
var endEndType = "endOf";
|
|
51
|
+
if (Array.isArray(endTypeItem)) {
|
|
52
|
+
startEndType = endTypeItem[0];
|
|
53
|
+
endEndType = endTypeItem[1];
|
|
54
|
+
}
|
|
55
|
+
newParams_1[toKey1] = getParseResult({
|
|
56
|
+
value: newParams_1[fromKey],
|
|
57
|
+
index: index,
|
|
58
|
+
endType: startEndType,
|
|
59
|
+
});
|
|
60
|
+
newParams_1[toKey2] = getParseResult({
|
|
61
|
+
value: newParams_1[fromKey],
|
|
62
|
+
index: index,
|
|
63
|
+
endType: endEndType,
|
|
64
|
+
});
|
|
65
|
+
Reflect.deleteProperty(newParams_1, fromKey);
|
|
66
|
+
}
|
|
67
|
+
// 3. 从两个键 转成 一个键
|
|
68
|
+
if (typeof fromKey === "object" && typeof toKey === "string") {
|
|
69
|
+
var fromKey1 = fromKey[0], fromtKey2 = fromKey[1];
|
|
70
|
+
// 初始定义转义类型 是否拼接一天的开始 一天的结束
|
|
71
|
+
var startEndType = "startOf";
|
|
72
|
+
var endEndType = "endOf";
|
|
73
|
+
if (Array.isArray(endTypeItem)) {
|
|
74
|
+
startEndType = endTypeItem[0];
|
|
75
|
+
endEndType = endTypeItem[1];
|
|
76
|
+
}
|
|
77
|
+
newParams_1[toKey] = [
|
|
78
|
+
getParseResult({
|
|
79
|
+
value: newParams_1[fromKey1],
|
|
80
|
+
index: index,
|
|
81
|
+
endType: startEndType,
|
|
82
|
+
}),
|
|
83
|
+
getParseResult({
|
|
84
|
+
value: newParams_1[fromtKey2],
|
|
85
|
+
index: index,
|
|
86
|
+
endType: endEndType,
|
|
87
|
+
}),
|
|
88
|
+
];
|
|
89
|
+
Reflect.deleteProperty(newParams_1, fromKey1);
|
|
90
|
+
Reflect.deleteProperty(newParams_1, fromtKey2);
|
|
91
|
+
}
|
|
92
|
+
// 4. 从两个键 转成 两个键
|
|
93
|
+
if (typeof fromKey === "object" && typeof toKey === "object") {
|
|
94
|
+
var fromKey1 = fromKey[0], fromtKey2 = fromKey[1];
|
|
95
|
+
var toKey1 = toKey[0], toKey2 = toKey[1];
|
|
96
|
+
// 初始定义转义类型 是否拼接一天的开始 一天的结束
|
|
97
|
+
var startEndType = undefined;
|
|
98
|
+
var endEndType = undefined;
|
|
99
|
+
if (Array.isArray(endTypeItem)) {
|
|
100
|
+
startEndType = endTypeItem[0];
|
|
101
|
+
endEndType = endTypeItem[1];
|
|
102
|
+
}
|
|
103
|
+
newParams_1[toKey1] = getParseResult({
|
|
104
|
+
value: newParams_1[fromKey1],
|
|
105
|
+
index: index,
|
|
106
|
+
endType: startEndType,
|
|
107
|
+
});
|
|
108
|
+
newParams_1[toKey2] = getParseResult({
|
|
109
|
+
value: newParams_1[fromtKey2],
|
|
110
|
+
index: index,
|
|
111
|
+
endType: endEndType,
|
|
112
|
+
});
|
|
113
|
+
Reflect.deleteProperty(newParams_1, fromKey1);
|
|
114
|
+
Reflect.deleteProperty(newParams_1, fromtKey2);
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
return newParams_1;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
function parseByResult(option) {
|
|
121
|
+
var _a = option.format, format = _a === void 0 ? defaultFormat : _a, _b = option.type, type = _b === void 0 ? "string" : _b;
|
|
122
|
+
return function (option) {
|
|
123
|
+
var value = option.value, _a = option.index, index = _a === void 0 ? 0 : _a, endType = option.endType;
|
|
124
|
+
if (!value) {
|
|
125
|
+
return undefined;
|
|
126
|
+
}
|
|
127
|
+
var parseType = typeof type === "string" ? type : type[index];
|
|
128
|
+
var parseFormat = typeof format === "string" ? format : format[index];
|
|
129
|
+
var parseEndType = Array.isArray(endType) ? endType[0] : endType;
|
|
130
|
+
if (parseType === "string") {
|
|
131
|
+
var dayjsValue = dayjs.isDayjs(value) ? value : dayjs(value);
|
|
132
|
+
if (parseEndType === "startOf") {
|
|
133
|
+
return dayjsValue
|
|
134
|
+
.startOf("day")
|
|
135
|
+
.format(parseFormat.length === 10 ? "".concat(parseFormat, " HH:mm:ss") : parseFormat);
|
|
136
|
+
}
|
|
137
|
+
if (parseEndType === "endOf") {
|
|
138
|
+
return dayjsValue
|
|
139
|
+
.endOf("day")
|
|
140
|
+
.format(parseFormat.length === 10 ? "".concat(parseFormat, " HH:mm:ss") : parseFormat);
|
|
141
|
+
}
|
|
142
|
+
return dayjsValue.format(parseFormat);
|
|
143
|
+
}
|
|
144
|
+
else {
|
|
145
|
+
if (parseEndType === "startOf") {
|
|
146
|
+
return dayjs(value).startOf("day");
|
|
147
|
+
}
|
|
148
|
+
if (parseEndType === "endOf") {
|
|
149
|
+
return dayjs(value).endOf("day");
|
|
150
|
+
}
|
|
151
|
+
return dayjs(value);
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
export { parseTime as default };
|
|
@@ -0,0 +1,230 @@
|
|
|
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
|
+
// 删除原有key
|
|
41
|
+
Reflect.deleteProperty(newParams, fromKey);
|
|
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
|
+
Reflect.deleteProperty(newParams, fromKey);
|
|
64
|
+
}
|
|
65
|
+
// 3. 从两个键 转成 一个键
|
|
66
|
+
if (typeof fromKey === "object" && typeof toKey === "string") {
|
|
67
|
+
const [fromKey1, fromtKey2] = fromKey;
|
|
68
|
+
// 初始定义转义类型 是否拼接一天的开始 一天的结束
|
|
69
|
+
let startEndType = "startOf";
|
|
70
|
+
let endEndType = "endOf";
|
|
71
|
+
if (Array.isArray(endTypeItem)) {
|
|
72
|
+
startEndType = endTypeItem[0];
|
|
73
|
+
endEndType = endTypeItem[1];
|
|
74
|
+
}
|
|
75
|
+
newParams[toKey] = [
|
|
76
|
+
getParseResult({
|
|
77
|
+
value: newParams[fromKey1],
|
|
78
|
+
index,
|
|
79
|
+
endType: startEndType,
|
|
80
|
+
}),
|
|
81
|
+
getParseResult({
|
|
82
|
+
value: newParams[fromtKey2],
|
|
83
|
+
index,
|
|
84
|
+
endType: endEndType,
|
|
85
|
+
}),
|
|
86
|
+
];
|
|
87
|
+
Reflect.deleteProperty(newParams, fromKey1);
|
|
88
|
+
Reflect.deleteProperty(newParams, fromtKey2);
|
|
89
|
+
}
|
|
90
|
+
// 4. 从两个键 转成 两个键
|
|
91
|
+
if (typeof fromKey === "object" && typeof toKey === "object") {
|
|
92
|
+
const [fromKey1, fromtKey2] = fromKey;
|
|
93
|
+
const [toKey1, toKey2] = toKey;
|
|
94
|
+
// 初始定义转义类型 是否拼接一天的开始 一天的结束
|
|
95
|
+
let startEndType = undefined;
|
|
96
|
+
let endEndType = undefined;
|
|
97
|
+
if (Array.isArray(endTypeItem)) {
|
|
98
|
+
startEndType = endTypeItem[0];
|
|
99
|
+
endEndType = endTypeItem[1];
|
|
100
|
+
}
|
|
101
|
+
newParams[toKey1] = getParseResult({
|
|
102
|
+
value: newParams[fromKey1],
|
|
103
|
+
index,
|
|
104
|
+
endType: startEndType,
|
|
105
|
+
});
|
|
106
|
+
newParams[toKey2] = getParseResult({
|
|
107
|
+
value: newParams[fromtKey2],
|
|
108
|
+
index,
|
|
109
|
+
endType: endEndType,
|
|
110
|
+
});
|
|
111
|
+
Reflect.deleteProperty(newParams, fromKey1);
|
|
112
|
+
Reflect.deleteProperty(newParams, fromtKey2);
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
return newParams;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function parseByResult(option) {
|
|
121
|
+
const { format = defaultFormat, type = "string" } = option;
|
|
122
|
+
|
|
123
|
+
return (option) => {
|
|
124
|
+
const { value, index = 0, endType } = option;
|
|
125
|
+
if (!value) {
|
|
126
|
+
return undefined;
|
|
127
|
+
}
|
|
128
|
+
const parseType = typeof type === "string" ? type : type[index];
|
|
129
|
+
const parseFormat = typeof format === "string" ? format : format[index];
|
|
130
|
+
const parseEndType = Array.isArray(endType) ? endType[0] : endType;
|
|
131
|
+
if (parseType === "string") {
|
|
132
|
+
const dayjsValue = dayjs.isDayjs(value) ? value : dayjs(value);
|
|
133
|
+
if (parseEndType === "startOf") {
|
|
134
|
+
return dayjsValue
|
|
135
|
+
.startOf("day")
|
|
136
|
+
.format(
|
|
137
|
+
parseFormat.length === 10 ? `${parseFormat} HH:mm:ss` : parseFormat
|
|
138
|
+
);
|
|
139
|
+
}
|
|
140
|
+
if (parseEndType === "endOf") {
|
|
141
|
+
return dayjsValue
|
|
142
|
+
.endOf("day")
|
|
143
|
+
.format(
|
|
144
|
+
parseFormat.length === 10 ? `${parseFormat} HH:mm:ss` : parseFormat
|
|
145
|
+
);
|
|
146
|
+
}
|
|
147
|
+
return dayjsValue.format(parseFormat);
|
|
148
|
+
} else {
|
|
149
|
+
if (parseEndType === "startOf") {
|
|
150
|
+
return dayjs(value).startOf("day");
|
|
151
|
+
}
|
|
152
|
+
if (parseEndType === "endOf") {
|
|
153
|
+
return dayjs(value).endOf("day");
|
|
154
|
+
}
|
|
155
|
+
return dayjs(value);
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// 处理单字段
|
|
161
|
+
// 时间戳处理
|
|
162
|
+
// console.log(
|
|
163
|
+
// parseTime({
|
|
164
|
+
// params: Date.now(),
|
|
165
|
+
// })
|
|
166
|
+
// );
|
|
167
|
+
// 将日期格式字符串转换成dayjs
|
|
168
|
+
// console.log(
|
|
169
|
+
// parseTime({
|
|
170
|
+
// params: "2023-09-22",
|
|
171
|
+
// type: "object",
|
|
172
|
+
// })
|
|
173
|
+
// );
|
|
174
|
+
// 添加一天的开始 结束
|
|
175
|
+
// console.log(
|
|
176
|
+
// parseTime({
|
|
177
|
+
// params: "2023-09-22",
|
|
178
|
+
// endType: "startOf",
|
|
179
|
+
// })
|
|
180
|
+
// );
|
|
181
|
+
// console.log(
|
|
182
|
+
// parseTime({
|
|
183
|
+
// params: "2023-09-22",
|
|
184
|
+
// endType: "endOf",
|
|
185
|
+
// })
|
|
186
|
+
// );
|
|
187
|
+
|
|
188
|
+
// 多字段用例
|
|
189
|
+
console.log(
|
|
190
|
+
parseTime({
|
|
191
|
+
params: { time: "2023-08-11" },
|
|
192
|
+
fromKeys: ["time"],
|
|
193
|
+
toKeys: ["resultTime"],
|
|
194
|
+
endType: "endOf",
|
|
195
|
+
})
|
|
196
|
+
);
|
|
197
|
+
console.log(
|
|
198
|
+
parseTime({
|
|
199
|
+
params: { time: "2023-08-11", time1: "2023-08-12" },
|
|
200
|
+
fromKeys: ["time", "time1"],
|
|
201
|
+
toKeys: ["resultTime", "resultTime2"],
|
|
202
|
+
endType: ["startOf", "endOf"],
|
|
203
|
+
})
|
|
204
|
+
);
|
|
205
|
+
console.log(
|
|
206
|
+
parseTime({
|
|
207
|
+
params: { time: "2023-08-11", time1: "2023-08-12" },
|
|
208
|
+
fromKeys: [["time", "time1"]],
|
|
209
|
+
toKeys: ["resultTime"],
|
|
210
|
+
type: "object",
|
|
211
|
+
endType: ["startOf", "endOf"],
|
|
212
|
+
})
|
|
213
|
+
);
|
|
214
|
+
console.log(
|
|
215
|
+
parseTime({
|
|
216
|
+
params: {
|
|
217
|
+
time: "2023-08-11",
|
|
218
|
+
time1: "2023-08-12",
|
|
219
|
+
time2: "2023-08-08",
|
|
220
|
+
time3: "2023-08-01",
|
|
221
|
+
},
|
|
222
|
+
fromKeys: [["time", "time1"], "time2", "time3"],
|
|
223
|
+
toKeys: ["resultTime", ["time2Start", "time2End"], "time3Result"],
|
|
224
|
+
// type: "object",
|
|
225
|
+
endType: [
|
|
226
|
+
["endOf", "endOf"],
|
|
227
|
+
["startOf", "startOf"],
|
|
228
|
+
],
|
|
229
|
+
})
|
|
230
|
+
);
|
|
@@ -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
|
+
"&": "&",
|
|
10
|
+
"<": "<",
|
|
11
|
+
">": ">",
|
|
12
|
+
""": '"',
|
|
13
|
+
"'": "'",
|
|
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
|
+
"<": "<",
|
|
27
|
+
">": ">",
|
|
28
|
+
" ": " ",
|
|
29
|
+
"&": "&",
|
|
30
|
+
""": '"',
|
|
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("&&&啊你"));
|
|
56
|
+
|
|
57
|
+
console.log(dangerouslySetXss("&&&啊你"));
|
|
58
|
+
|
|
59
|
+
console.log(dangerouslySetXss1("&&&啊你"));
|
|
60
|
+
console.log(unescapeString("1、aa>80%\n2、><\n3、ss<80%"));
|
|
61
|
+
console.log(dangerouslySetXss("1、aa>80%\n2、><\n3、ss<80%"));
|
|
62
|
+
console.log(dangerouslySetXss1("1、aa>80%\n2、><\n3、ss<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
|
-
|
|
2
|
-
|
|
3
|
-
return "";
|
|
4
|
-
}
|
|
5
|
-
|
|
6
|
-
const escapedChars = {
|
|
7
|
-
"&": "&",
|
|
8
|
-
"<": "<",
|
|
9
|
-
">": ">",
|
|
10
|
-
""": '"',
|
|
11
|
-
"'": "'",
|
|
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
|
-
"<": "<",
|
|
25
|
-
">": ">",
|
|
26
|
-
" ": " ",
|
|
27
|
-
"&": "&",
|
|
28
|
-
""": '"',
|
|
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("&&&啊你"));
|
|
54
|
-
|
|
55
|
-
console.log(dangerouslySetXss("&&&啊你"));
|
|
56
|
-
|
|
57
|
-
console.log(dangerouslySetXss1("&&&啊你"));
|
|
58
|
-
console.log(unescapeString("1、aa>80%\n2、><\n3、ss<80%"));
|
|
59
|
-
console.log(dangerouslySetXss("1、aa>80%\n2、><\n3、ss<80%"));
|
|
60
|
-
console.log(dangerouslySetXss1("1、aa>80%\n2、><\n3、ss<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.
|
|
3
|
+
"version": "0.0.22",
|
|
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
package/src/parseTime.ts
ADDED
|
@@ -0,0 +1,196 @@
|
|
|
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
|
+
// 删除原有key
|
|
70
|
+
Reflect.deleteProperty(newParams, fromKey);
|
|
71
|
+
}
|
|
72
|
+
// 2. 从一个键 转成 两个键
|
|
73
|
+
if (typeof fromKey === "string" && typeof toKey === "object") {
|
|
74
|
+
const [toKey1, toKey2] = toKey;
|
|
75
|
+
// 初始定义转义类型 是否拼接一天的开始 一天的结束
|
|
76
|
+
let startEndType: IEndType | undefined = "startOf";
|
|
77
|
+
let endEndType: IEndType | undefined = "endOf";
|
|
78
|
+
if (Array.isArray(endTypeItem)) {
|
|
79
|
+
startEndType = endTypeItem[0];
|
|
80
|
+
endEndType = endTypeItem[1];
|
|
81
|
+
}
|
|
82
|
+
newParams[toKey1] = getParseResult({
|
|
83
|
+
value: newParams[fromKey],
|
|
84
|
+
index,
|
|
85
|
+
endType: startEndType,
|
|
86
|
+
});
|
|
87
|
+
newParams[toKey2] = getParseResult({
|
|
88
|
+
value: newParams[fromKey],
|
|
89
|
+
index,
|
|
90
|
+
endType: endEndType,
|
|
91
|
+
});
|
|
92
|
+
Reflect.deleteProperty(newParams, fromKey);
|
|
93
|
+
}
|
|
94
|
+
// 3. 从两个键 转成 一个键
|
|
95
|
+
if (typeof fromKey === "object" && typeof toKey === "string") {
|
|
96
|
+
const [fromKey1, fromtKey2] = fromKey;
|
|
97
|
+
// 初始定义转义类型 是否拼接一天的开始 一天的结束
|
|
98
|
+
let startEndType: IEndType | undefined = "startOf";
|
|
99
|
+
let endEndType: IEndType | undefined = "endOf";
|
|
100
|
+
if (Array.isArray(endTypeItem)) {
|
|
101
|
+
startEndType = endTypeItem[0];
|
|
102
|
+
endEndType = endTypeItem[1];
|
|
103
|
+
}
|
|
104
|
+
newParams[toKey] = [
|
|
105
|
+
getParseResult({
|
|
106
|
+
value: newParams[fromKey1],
|
|
107
|
+
index,
|
|
108
|
+
endType: startEndType,
|
|
109
|
+
}),
|
|
110
|
+
getParseResult({
|
|
111
|
+
value: newParams[fromtKey2],
|
|
112
|
+
index,
|
|
113
|
+
endType: endEndType,
|
|
114
|
+
}),
|
|
115
|
+
];
|
|
116
|
+
Reflect.deleteProperty(newParams, fromKey1);
|
|
117
|
+
Reflect.deleteProperty(newParams, fromtKey2);
|
|
118
|
+
}
|
|
119
|
+
// 4. 从两个键 转成 两个键
|
|
120
|
+
if (typeof fromKey === "object" && typeof toKey === "object") {
|
|
121
|
+
const [fromKey1, fromtKey2] = fromKey;
|
|
122
|
+
const [toKey1, toKey2] = toKey;
|
|
123
|
+
// 初始定义转义类型 是否拼接一天的开始 一天的结束
|
|
124
|
+
let startEndType: IEndType | undefined = undefined;
|
|
125
|
+
let endEndType: IEndType | undefined = undefined;
|
|
126
|
+
if (Array.isArray(endTypeItem)) {
|
|
127
|
+
startEndType = endTypeItem[0];
|
|
128
|
+
endEndType = endTypeItem[1];
|
|
129
|
+
}
|
|
130
|
+
newParams[toKey1] = getParseResult({
|
|
131
|
+
value: newParams[fromKey1],
|
|
132
|
+
index,
|
|
133
|
+
endType: startEndType,
|
|
134
|
+
});
|
|
135
|
+
newParams[toKey2] = getParseResult({
|
|
136
|
+
value: newParams[fromtKey2],
|
|
137
|
+
index,
|
|
138
|
+
endType: endEndType,
|
|
139
|
+
});
|
|
140
|
+
Reflect.deleteProperty(newParams, fromKey1);
|
|
141
|
+
Reflect.deleteProperty(newParams, fromtKey2);
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
return newParams;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
interface IProps {
|
|
150
|
+
format?: string | string[];
|
|
151
|
+
type?: IType | IType[];
|
|
152
|
+
}
|
|
153
|
+
interface IResultProps {
|
|
154
|
+
value?: any;
|
|
155
|
+
index?: number;
|
|
156
|
+
endType?: IEndType | IEndType[];
|
|
157
|
+
}
|
|
158
|
+
function parseByResult(option: IProps) {
|
|
159
|
+
const { format = defaultFormat, type = "string" } = option;
|
|
160
|
+
|
|
161
|
+
return (option: IResultProps) => {
|
|
162
|
+
const { value, index = 0, endType } = option;
|
|
163
|
+
if (!value) {
|
|
164
|
+
return undefined;
|
|
165
|
+
}
|
|
166
|
+
const parseType = typeof type === "string" ? type : type[index];
|
|
167
|
+
const parseFormat = typeof format === "string" ? format : format[index];
|
|
168
|
+
const parseEndType = Array.isArray(endType) ? endType[0] : endType;
|
|
169
|
+
if (parseType === "string") {
|
|
170
|
+
const dayjsValue = dayjs.isDayjs(value) ? value : dayjs(value);
|
|
171
|
+
if (parseEndType === "startOf") {
|
|
172
|
+
return dayjsValue
|
|
173
|
+
.startOf("day")
|
|
174
|
+
.format(
|
|
175
|
+
parseFormat.length === 10 ? `${parseFormat} HH:mm:ss` : parseFormat
|
|
176
|
+
);
|
|
177
|
+
}
|
|
178
|
+
if (parseEndType === "endOf") {
|
|
179
|
+
return dayjsValue
|
|
180
|
+
.endOf("day")
|
|
181
|
+
.format(
|
|
182
|
+
parseFormat.length === 10 ? `${parseFormat} HH:mm:ss` : parseFormat
|
|
183
|
+
);
|
|
184
|
+
}
|
|
185
|
+
return dayjsValue.format(parseFormat);
|
|
186
|
+
} else {
|
|
187
|
+
if (parseEndType === "startOf") {
|
|
188
|
+
return dayjs(value).startOf("day");
|
|
189
|
+
}
|
|
190
|
+
if (parseEndType === "endOf") {
|
|
191
|
+
return dayjs(value).endOf("day");
|
|
192
|
+
}
|
|
193
|
+
return dayjs(value);
|
|
194
|
+
}
|
|
195
|
+
};
|
|
196
|
+
}
|
|
@@ -0,0 +1,228 @@
|
|
|
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
|
+
// 删除原有key
|
|
39
|
+
Reflect.deleteProperty(newParams, fromKey);
|
|
40
|
+
}
|
|
41
|
+
// 2. 从一个键 转成 两个键
|
|
42
|
+
if (typeof fromKey === "string" && typeof toKey === "object") {
|
|
43
|
+
const [toKey1, toKey2] = toKey;
|
|
44
|
+
// 初始定义转义类型 是否拼接一天的开始 一天的结束
|
|
45
|
+
let startEndType = "startOf";
|
|
46
|
+
let endEndType = "endOf";
|
|
47
|
+
if (Array.isArray(endTypeItem)) {
|
|
48
|
+
startEndType = endTypeItem[0];
|
|
49
|
+
endEndType = endTypeItem[1];
|
|
50
|
+
}
|
|
51
|
+
newParams[toKey1] = getParseResult({
|
|
52
|
+
value: newParams[fromKey],
|
|
53
|
+
index,
|
|
54
|
+
endType: startEndType,
|
|
55
|
+
});
|
|
56
|
+
newParams[toKey2] = getParseResult({
|
|
57
|
+
value: newParams[fromKey],
|
|
58
|
+
index,
|
|
59
|
+
endType: endEndType,
|
|
60
|
+
});
|
|
61
|
+
Reflect.deleteProperty(newParams, fromKey);
|
|
62
|
+
}
|
|
63
|
+
// 3. 从两个键 转成 一个键
|
|
64
|
+
if (typeof fromKey === "object" && typeof toKey === "string") {
|
|
65
|
+
const [fromKey1, fromtKey2] = fromKey;
|
|
66
|
+
// 初始定义转义类型 是否拼接一天的开始 一天的结束
|
|
67
|
+
let startEndType = "startOf";
|
|
68
|
+
let endEndType = "endOf";
|
|
69
|
+
if (Array.isArray(endTypeItem)) {
|
|
70
|
+
startEndType = endTypeItem[0];
|
|
71
|
+
endEndType = endTypeItem[1];
|
|
72
|
+
}
|
|
73
|
+
newParams[toKey] = [
|
|
74
|
+
getParseResult({
|
|
75
|
+
value: newParams[fromKey1],
|
|
76
|
+
index,
|
|
77
|
+
endType: startEndType,
|
|
78
|
+
}),
|
|
79
|
+
getParseResult({
|
|
80
|
+
value: newParams[fromtKey2],
|
|
81
|
+
index,
|
|
82
|
+
endType: endEndType,
|
|
83
|
+
}),
|
|
84
|
+
];
|
|
85
|
+
Reflect.deleteProperty(newParams, fromKey1);
|
|
86
|
+
Reflect.deleteProperty(newParams, fromtKey2);
|
|
87
|
+
}
|
|
88
|
+
// 4. 从两个键 转成 两个键
|
|
89
|
+
if (typeof fromKey === "object" && typeof toKey === "object") {
|
|
90
|
+
const [fromKey1, fromtKey2] = fromKey;
|
|
91
|
+
const [toKey1, toKey2] = toKey;
|
|
92
|
+
// 初始定义转义类型 是否拼接一天的开始 一天的结束
|
|
93
|
+
let startEndType = undefined;
|
|
94
|
+
let endEndType = undefined;
|
|
95
|
+
if (Array.isArray(endTypeItem)) {
|
|
96
|
+
startEndType = endTypeItem[0];
|
|
97
|
+
endEndType = endTypeItem[1];
|
|
98
|
+
}
|
|
99
|
+
newParams[toKey1] = getParseResult({
|
|
100
|
+
value: newParams[fromKey1],
|
|
101
|
+
index,
|
|
102
|
+
endType: startEndType,
|
|
103
|
+
});
|
|
104
|
+
newParams[toKey2] = getParseResult({
|
|
105
|
+
value: newParams[fromtKey2],
|
|
106
|
+
index,
|
|
107
|
+
endType: endEndType,
|
|
108
|
+
});
|
|
109
|
+
Reflect.deleteProperty(newParams, fromKey1);
|
|
110
|
+
Reflect.deleteProperty(newParams, fromtKey2);
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
return newParams;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function parseByResult(option) {
|
|
119
|
+
const { format = defaultFormat, type = "string" } = option;
|
|
120
|
+
|
|
121
|
+
return (option) => {
|
|
122
|
+
const { value, index = 0, endType } = option;
|
|
123
|
+
if (!value) {
|
|
124
|
+
return undefined;
|
|
125
|
+
}
|
|
126
|
+
const parseType = typeof type === "string" ? type : type[index];
|
|
127
|
+
const parseFormat = typeof format === "string" ? format : format[index];
|
|
128
|
+
const parseEndType = Array.isArray(endType) ? endType[0] : endType;
|
|
129
|
+
if (parseType === "string") {
|
|
130
|
+
const dayjsValue = dayjs.isDayjs(value) ? value : dayjs(value);
|
|
131
|
+
if (parseEndType === "startOf") {
|
|
132
|
+
return dayjsValue
|
|
133
|
+
.startOf("day")
|
|
134
|
+
.format(
|
|
135
|
+
parseFormat.length === 10 ? `${parseFormat} HH:mm:ss` : parseFormat
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
if (parseEndType === "endOf") {
|
|
139
|
+
return dayjsValue
|
|
140
|
+
.endOf("day")
|
|
141
|
+
.format(
|
|
142
|
+
parseFormat.length === 10 ? `${parseFormat} HH:mm:ss` : parseFormat
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
return dayjsValue.format(parseFormat);
|
|
146
|
+
} else {
|
|
147
|
+
if (parseEndType === "startOf") {
|
|
148
|
+
return dayjs(value).startOf("day");
|
|
149
|
+
}
|
|
150
|
+
if (parseEndType === "endOf") {
|
|
151
|
+
return dayjs(value).endOf("day");
|
|
152
|
+
}
|
|
153
|
+
return dayjs(value);
|
|
154
|
+
}
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// 处理单字段
|
|
159
|
+
// 时间戳处理
|
|
160
|
+
// console.log(
|
|
161
|
+
// parseTime({
|
|
162
|
+
// params: Date.now(),
|
|
163
|
+
// })
|
|
164
|
+
// );
|
|
165
|
+
// 将日期格式字符串转换成dayjs
|
|
166
|
+
// console.log(
|
|
167
|
+
// parseTime({
|
|
168
|
+
// params: "2023-09-22",
|
|
169
|
+
// type: "object",
|
|
170
|
+
// })
|
|
171
|
+
// );
|
|
172
|
+
// 添加一天的开始 结束
|
|
173
|
+
// console.log(
|
|
174
|
+
// parseTime({
|
|
175
|
+
// params: "2023-09-22",
|
|
176
|
+
// endType: "startOf",
|
|
177
|
+
// })
|
|
178
|
+
// );
|
|
179
|
+
// console.log(
|
|
180
|
+
// parseTime({
|
|
181
|
+
// params: "2023-09-22",
|
|
182
|
+
// endType: "endOf",
|
|
183
|
+
// })
|
|
184
|
+
// );
|
|
185
|
+
|
|
186
|
+
// 多字段用例
|
|
187
|
+
console.log(
|
|
188
|
+
parseTime({
|
|
189
|
+
params: { time: "2023-08-11" },
|
|
190
|
+
fromKeys: ["time"],
|
|
191
|
+
toKeys: ["resultTime"],
|
|
192
|
+
endType: "endOf",
|
|
193
|
+
})
|
|
194
|
+
);
|
|
195
|
+
console.log(
|
|
196
|
+
parseTime({
|
|
197
|
+
params: { time: "2023-08-11", time1: "2023-08-12" },
|
|
198
|
+
fromKeys: ["time", "time1"],
|
|
199
|
+
toKeys: ["resultTime", "resultTime2"],
|
|
200
|
+
endType: ["startOf", "endOf"],
|
|
201
|
+
})
|
|
202
|
+
);
|
|
203
|
+
console.log(
|
|
204
|
+
parseTime({
|
|
205
|
+
params: { time: "2023-08-11", time1: "2023-08-12" },
|
|
206
|
+
fromKeys: [["time", "time1"]],
|
|
207
|
+
toKeys: ["resultTime"],
|
|
208
|
+
type: "object",
|
|
209
|
+
endType: ["startOf", "endOf"],
|
|
210
|
+
})
|
|
211
|
+
);
|
|
212
|
+
console.log(
|
|
213
|
+
parseTime({
|
|
214
|
+
params: {
|
|
215
|
+
time: "2023-08-11",
|
|
216
|
+
time1: "2023-08-12",
|
|
217
|
+
time2: "2023-08-08",
|
|
218
|
+
time3: "2023-08-01",
|
|
219
|
+
},
|
|
220
|
+
fromKeys: [["time", "time1"], "time2", "time3"],
|
|
221
|
+
toKeys: ["resultTime", ["time2Start", "time2End"], "time3Result"],
|
|
222
|
+
// type: "object",
|
|
223
|
+
endType: [
|
|
224
|
+
["endOf", "endOf"],
|
|
225
|
+
["startOf", "startOf"],
|
|
226
|
+
],
|
|
227
|
+
})
|
|
228
|
+
);
|
package/src/test.js
CHANGED
|
@@ -59,6 +59,15 @@ console.log(unescapeString("1、aa>80%\n2、><\n3、ss<80%"));
|
|
|
59
59
|
console.log(dangerouslySetXss("1、aa>80%\n2、><\n3、ss<80%"));
|
|
60
60
|
console.log(dangerouslySetXss1("1、aa>80%\n2、><\n3、ss<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
|