-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelper.ts
More file actions
232 lines (207 loc) · 6.47 KB
/
helper.ts
File metadata and controls
232 lines (207 loc) · 6.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import { DateTime } from 'luxon';
import { isObject } from 'lodash';
export const findAsync = async (arr, callback) => {
for (let i = 0; i < arr.length; i++) {
const result = await callback(arr[i], i, arr);
if (result) {
return arr[i];
}
}
return undefined;
};
export const mapAsync = async (arr, callback) => {
const result: any[] = [];
for (let i = 0; i < arr.length; i++) {
const mappedValue = await callback(arr[i], i, arr);
result.push(mappedValue);
}
return result;
};
export const filterAsync = async (arr, callback) => {
const result: any[] = [];
for (let i = 0; i < arr.length; i++) {
const filteredValue = await callback(arr[i], i, arr);
if (filteredValue) {
result.push(arr[i]);
}
}
return result;
};
export const findIndexAsync = async (arr, callback) => {
for (let i = 0; i < arr.length; i++) {
const result = await callback(arr[i], i, arr);
if (result) {
return i;
}
}
return -1;
};
export const sortRule = (valueA, valueB, sort) => {
if (
Number.isNaN(valueA) ||
Number.isNaN(valueB) ||
typeof valueA === 'undefined' ||
typeof valueB === 'undefined' ||
valueA === null ||
valueB === null
) {
return 1;
} else {
if (valueA >= valueB) {
if (sort) {
return 1;
}
return -1;
} else {
if (sort) {
return -1;
}
return 1;
}
}
};
/**
* 获取应用的时区
* 逻辑:
* 1. 如果输入的时区是 'global',则使用应用配置的时区(window.appInfo.appTimeZone)
* 2. 如果输入的时区是其他非 'user' 的值,则使用该值作为时区
* 3. 如果输入的时区是 'user' 或者没有输入,则使用用户本地的时区(Intl.DateTimeFormat().resolvedOptions().timeZone)
*/
export const getAppTimezone = (tz?: string) => {
const userTimeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
const globalTimeZone = window?.appInfo?.appTimeZone === 'user' ? userTimeZone : window?.appInfo?.appTimeZone;
tz = tz ?? 'user';
switch (tz) {
case 'global':
return globalTimeZone;
case 'user':
return userTimeZone;
default:
return tz;
}
};
const validIANATimezoneCache = {};
// 判断是否是有效的时区字符
export function isValidTimezoneIANAString(timezoneString) {
if (validIANATimezoneCache[timezoneString]) return true;
try {
new Intl.DateTimeFormat(undefined, { timeZone: timezoneString });
validIANATimezoneCache[timezoneString] = true;
return true;
} catch (error) {
return false;
}
}
export function naslDateToLocalDate(date) {
const localTZ = Intl.DateTimeFormat().resolvedOptions().timeZone;
if (date instanceof Date) {
const localDate = DateTime.fromJSDate(date).setZone(localTZ);
return safeNewDate(localDate.toFormat('yyyy-MM-dd HH:mm:ss'));
}
if (date?.includes('T')) {
const localDate = DateTime.fromISO(date, { zone: localTZ });
return safeNewDate(localDate.toFormat('yyyy-MM-dd HH:mm:ss'));
}
// 仅日期部分
if (/^\d{4}-\d{2}-\d{2}$/.test(date)) {
const localDate = DateTime.fromFormat(date, 'yyyy-MM-dd', { zone: localTZ });
return safeNewDate(localDate.toFormat('yyyy-MM-dd HH:mm:ss'));
}
// 包含时间部分
const localDate = DateTime.fromFormat(date, 'yyyy-MM-dd HH:mm:ss', { zone: localTZ });
return safeNewDate(localDate.toFormat('yyyy-MM-dd HH:mm:ss'));
}
export function convertJSDateInTargetTimeZone(date, tz) {
return safeNewDate(
DateTime.fromJSDate(safeNewDate(date)).setZone(getAppTimezone(tz)).toFormat('yyyy-MM-dd HH:mm:ss'),
);
}
export const safeNewDate = (dateStr) => {
// 如果输入是字符串形式的时间戳,则先转换为时间戳
if (typeof dateStr === 'string' && /^\d+$/.test(dateStr)) {
const date = new Date(parseInt(dateStr, 10));
if (!isNaN(date.getTime())) {
return date;
}
}
const isValidDate = (date) => {
return !['Invalid Date', 'Invalid time value', 'invalid date'].includes(date.toString());
};
const fallback = new Date(null);
try {
// 尝试第一次转日期
let res = new Date(dateStr);
// 如果是无效日期,则尝试替换 '-' 为 '/' 再转
if (!isValidDate(res)) {
res = new Date(dateStr.replaceAll('-', '/'));
// 如果还是无效日期,则返回 fallback
if (!isValidDate(res)) {
return fallback;
}
}
return res;
} catch (err) {
return fallback;
}
};
// 类型定义是否属于基础类型
export const isDefPrimitive = (typeKey) =>
[
'nasl.core.Boolean',
'nasl.core.Long',
'nasl.core.Decimal',
'nasl.core.String',
'nasl.core.Text',
'nasl.core.Binary',
'nasl.core.Date',
'nasl.core.Time',
'nasl.core.DateTime',
'nasl.core.Email',
].includes(typeKey);
// 类型定义是否属于字符串大类
export const isDefString = (typeKey) =>
[
'nasl.core.String',
'nasl.core.Text',
'nasl.core.Binary',
'nasl.core.Date',
'nasl.core.Time',
'nasl.core.DateTime',
'nasl.core.Email',
].includes(typeKey);
export type RegExpLike = {
pattern: string;
flags?: string;
$type: 'nasl.util.Regex';
};
export const isDefRegExp: (obj: unknown) => boolean = (obj: unknown) => {
// @ts-expect-error
return isObject(obj) && obj.$type === 'nasl.util.Regex';
};
// 类型定义是否属于数字大类
export const isDefNumber = (typeKey) => ['nasl.core.Long', 'nasl.core.Decimal'].includes(typeKey);
// 类型定义是否属于数组
export const isDefList = (typeDefinition) => {
const { typeKind, typeNamespace, typeName } = typeDefinition || {};
return typeKind === 'generic' && typeNamespace === 'nasl.collection' && typeName === 'List';
};
// 类型定义是否属于Map
export const isDefMap = (typeDefinition) => {
const { typeKind, typeNamespace, typeName } = typeDefinition || {};
return typeKind === 'generic' && typeNamespace === 'nasl.collection' && typeName === 'Map';
};
export function isInputValidNaslDateTime(input) {
return (
input instanceof Date ||
(typeof input === 'string' && /^(\d{4}-\d{2}-\d{2})T(\d{2}:\d{2}:\d{2})/.test(input)) ||
(typeof input === 'string' && /^(\d{4}-\d{2}-\d{2}) (\d{2}:\d{2}:\d{2})/.test(input))
);
}
export function toValue(date, typeKey) {
if (!date) return date;
if (typeKey === 'format')
return DateTime.fromJSDate(date).toFormat('yyyy-MM-dd'); // value 的真实格式
else if (typeKey === 'json') return this.JsonSerialize(date);
else if (typeKey === 'timestamp') return date.getTime();
else return date;
}