|
|
@@ -2,6 +2,71 @@
|
|
2
|
2
|
import moment from 'moment';
|
|
3
|
3
|
import os from 'os';
|
|
4
|
4
|
|
|
|
5
|
+/**
|
|
|
6
|
+ * 字符串工具类集合
|
|
|
7
|
+ *
|
|
|
8
|
+ * 提供各种字符串处理功能,包括:
|
|
|
9
|
+ * - 字符串补零、修剪空格
|
|
|
10
|
+ * - 引号转义与替换
|
|
|
11
|
+ * - 子字符串截取
|
|
|
12
|
+ * - 全量字符串替换
|
|
|
13
|
+ * - 数字/中文/英文验证
|
|
|
14
|
+ * - IP地址处理
|
|
|
15
|
+ * - 时间格式化
|
|
|
16
|
+ * - 年级计算
|
|
|
17
|
+ * - 哈希表实现
|
|
|
18
|
+ * - 金额格式化
|
|
|
19
|
+ * - 数组排序
|
|
|
20
|
+ * - 循环熔断
|
|
|
21
|
+ * - 英语单词提取
|
|
|
22
|
+ *
|
|
|
23
|
+ * 所有方法均为静态工具方法,可直接调用。
|
|
|
24
|
+ */
|
|
|
25
|
+/**
|
|
|
26
|
+ * 字符串工具类集合,提供多种字符串处理功能
|
|
|
27
|
+ *
|
|
|
28
|
+ * @namespace stringUtils
|
|
|
29
|
+ * @property {Function} AddZero - 给字符串左侧补零
|
|
|
30
|
+ * @property {Function} Trim - 删除字符串两端的空格
|
|
|
31
|
+ * @property {Function} LTrim - 删除字符串左侧的空格
|
|
|
32
|
+ * @property {Function} RTrim - 删除字符串右侧的空格
|
|
|
33
|
+ * @property {Function} AddSingleQuotes - 转义单引号
|
|
|
34
|
+ * @property {Function} SubStr - 截取子字符串
|
|
|
35
|
+ * @property {Function} ReplaceSingleQuotes - 替换单引号为双单引号
|
|
|
36
|
+ * @property {Function} ReplaceDoubleQuotes - 替换双引号为中文引号
|
|
|
37
|
+ * @property {Function} ReplaceAllString - 替换字符串中所有匹配项
|
|
|
38
|
+ * @property {Function} IsNumber - 判断字符串是否为纯数字
|
|
|
39
|
+ * @property {Function} GetServerIP - 获取服务器IP地址
|
|
|
40
|
+ * @property {Function} IsArray - 判断对象是否为数组
|
|
|
41
|
+ * @property {Function} ExportExcelRes - 设置Excel导出的响应头
|
|
|
42
|
+ * @property {Function} SetEncode - 字符编码转换
|
|
|
43
|
+ * @property {Function} SetDecode - 字符解码
|
|
|
44
|
+ * @property {Function} ChangeXML - 将JSON转换为XML格式
|
|
|
45
|
+ * @property {Function} Random - 生成指定范围内的随机整数
|
|
|
46
|
+ * @property {Function} RandomArray - 随机打乱数组顺序
|
|
|
47
|
+ * @property {Function} RemoveJSONNull - 移除JSON数组中的null值
|
|
|
48
|
+ * @property {Function} GetDateTimeFormat - 人性化时间格式化
|
|
|
49
|
+ * @property {Function} GetGrade - 根据生日计算年级
|
|
|
50
|
+ * @property {Function} GetGrade2 - 根据年级数字返回对应年级名称
|
|
|
51
|
+ * @property {Function} getMinuteSecond - 将秒数转换为分钟/秒格式
|
|
|
52
|
+ * @property {Function} FormatMoney - 将金额转换为格式化字符串
|
|
|
53
|
+ * @property {Function} FormatPercentageToFigure - 将百分比转换为数字
|
|
|
54
|
+ * @property {Function} SortArrayByStringLength - 根据字符串长度排序数组
|
|
|
55
|
+ * @property {Function} IsChineseSentence - 判断整句是否包含中文
|
|
|
56
|
+ * @property {Function} SentenceChinesePosition - 返回句中中文的位置
|
|
|
57
|
+ * @property {Function} IsChinese - 判断是否是中文
|
|
|
58
|
+ * @property {Function} IsEnglish - 判断是否是英文
|
|
|
59
|
+ * @property {Function} loopBreaker - 循环熔断函数
|
|
|
60
|
+ * @property {Function} getGroupName - 获取小组名称
|
|
|
61
|
+ * @property {Function} CheckIsArray - 检查是否为数组
|
|
|
62
|
+ * @property {Function} GetOrdinalPostfix - 获取序数后缀
|
|
|
63
|
+ * @property {Function} TrimEndZero - 去除数字末尾多余的0
|
|
|
64
|
+ * @property {Function} IsValidIP - 验证IP地址格式
|
|
|
65
|
+ * @property {Function} IsValidChineseNumberParentheses - 验证字符串是否只包含汉字、数字、字母和括号
|
|
|
66
|
+ * @property {Function} GetClientIP - 获取客户端真实IP地址
|
|
|
67
|
+ * @property {Function} cleanWord - 清理单词中的非字母字符
|
|
|
68
|
+ * @property {Function} extractEnglishWords - 从文本中提取英语单词
|
|
|
69
|
+ */
|
|
5
|
70
|
export const stringUtils = {
|
|
6
|
71
|
//给字符串左侧补零
|
|
7
|
72
|
AddZero: (str, length) => {
|
|
|
@@ -606,5 +671,58 @@ export const stringUtils = {
|
|
606
|
671
|
}
|
|
607
|
672
|
|
|
608
|
673
|
return clientIP || '0.0.0.0';
|
|
609
|
|
- }
|
|
|
674
|
+ },
|
|
|
675
|
+
|
|
|
676
|
+ cleanWord(word) {
|
|
|
677
|
+ if (!word) return '';
|
|
|
678
|
+
|
|
|
679
|
+ // 去除单词前后的非字母字符
|
|
|
680
|
+ const cleaned = word.replace(/^[^A-Za-z]+|[^A-Za-z]+$/g, '');
|
|
|
681
|
+
|
|
|
682
|
+ // 保留单词中间的撇号和连字符
|
|
|
683
|
+ return cleaned.replace(/[^A-Za-z''-]/g, '');
|
|
|
684
|
+ },
|
|
|
685
|
+ // 提取英语单词的函数 - 增强版
|
|
|
686
|
+ extractEnglishWords(texts) {
|
|
|
687
|
+ //console.group('英语单词提取');
|
|
|
688
|
+ const words = new Set();
|
|
|
689
|
+
|
|
|
690
|
+ texts.forEach(item => {
|
|
|
691
|
+ const text = item;
|
|
|
692
|
+ console.log('处理文本:', text);
|
|
|
693
|
+
|
|
|
694
|
+ // 使用多种分隔符分割文本(空格、逗号、句号、感叹号、中文字符等)
|
|
|
695
|
+ // 这个正则表达式会匹配任何非英文字母、撇号或连字符的字符作为分隔符
|
|
|
696
|
+ const parts = text.split(/[^A-Za-z''-]+/).filter(Boolean);
|
|
|
697
|
+
|
|
|
698
|
+ console.log('分割后的部分:', parts);
|
|
|
699
|
+
|
|
|
700
|
+ // 处理每个可能的单词
|
|
|
701
|
+ parts.forEach(part => {
|
|
|
702
|
+ // 清理并验证单词
|
|
|
703
|
+ const cleanWord = this.cleanWord(part);
|
|
|
704
|
+
|
|
|
705
|
+ // 特殊处理单词"I"
|
|
|
706
|
+ if (cleanWord === 'I' || cleanWord === 'a') {
|
|
|
707
|
+ words.add(cleanWord); // 添加小写的"i"
|
|
|
708
|
+ //console.log('添加单词: I (特殊处理)');
|
|
|
709
|
+ }
|
|
|
710
|
+ // 处理其他单词(长度>=2)
|
|
|
711
|
+ else if (cleanWord && cleanWord.length >= 2 && /^[A-Za-z''-]+$/.test(cleanWord)) {
|
|
|
712
|
+ let lowerWord = cleanWord.toLowerCase();
|
|
|
713
|
+ if (lowerWord=="i'm"){
|
|
|
714
|
+ lowerWord="I'm";
|
|
|
715
|
+ }
|
|
|
716
|
+ words.add(lowerWord);
|
|
|
717
|
+ //console.log('添加单词:', lowerWord);
|
|
|
718
|
+ }
|
|
|
719
|
+ });
|
|
|
720
|
+ });
|
|
|
721
|
+
|
|
|
722
|
+ //const result = Array.from(words).sort();
|
|
|
723
|
+ let result = Array.from(words);
|
|
|
724
|
+ //console.log('提取结果:', result);
|
|
|
725
|
+ //console.groupEnd();
|
|
|
726
|
+ return result;
|
|
|
727
|
+ },
|
|
610
|
728
|
}
|