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
export default class utils {
// 格式化为json字符串
static formatJson(jsonString: string) {
try {
return JSON.stringify(JSON.parse(jsonString), null, 2); // 对JSON字符串进行格式化处理
} catch (error) {
return 'Invalid JSON'
}
}
// 格式化为json
static formatJsonObj(jsonString: string) {
try {
return JSON.parse(jsonString) // 对JSON字符串进行格式化处理
} catch (error) {
return JSON.parse('{"error": "Invalid JSON"}')
}
}
// 生成年月日时分秒毫秒字符串
static genDateTimeStr() {
const now = new Date();
const year = now.getFullYear();
const month = (now.getMonth() + 1).toString().padStart(2, '0');
const day = now.getDate().toString().padStart(2, '0');
const hours = now.getHours().toString().padStart(2, '0');
const minutes = now.getMinutes().toString().padStart(2, '0');
const seconds = now.getSeconds().toString().padStart(2, '0');
const milliseconds = now.getMilliseconds().toString().padStart(3, '0');
const formattedDateTime = `${year}${month}${day}${hours}${minutes}${seconds}${milliseconds}`;
// console.log(formattedDateTime); // 输出类似:20221231120530123
return formattedDateTime
}
// 拆分文本
static splitText(str: string) {
str = str.replaceAll('“','').replaceAll('”','')
// 使用正则表达式拆分文本
let sentences = str.split(/[!|?|。|"|!]/);
// 过滤掉长度为 0 的句子
sentences = sentences.filter(s => s.length > 0);
// console.log(sentences)
return sentences
}
// 拆分英文文本
static splitTextEn(str: string) {
str = str.replaceAll('"','').replaceAll('"','')
// 使用正则表达式拆分文本
let sentences = str.split(/[!|?|.]/);
// 过滤掉长度为 0 的句子
sentences = sentences.filter(s => s.length > 0);
// console.log(sentences)
return sentences
}
// 过滤掉中文字符
static filterChineseAndPunctuation(inputString: string) {
return inputString.replace(/[\u4E00-\u9FA5\u3000-\u303F\uff00-\uffef]/g, '') // 过滤中文字符
.replace(/[^\w\s]|_/g, '') // 过滤标点符号
.replace(/\s+/g, ' '); // 连续多个空格替换为一个空格
}
}