-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathString.ts
159 lines (135 loc) · 4.92 KB
/
String.ts
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
/**
* 处理单个单词的大小写
*/
export function capitalizeWord(str: string, { lowerRest = true, preserveCase = false }): string {
if (!str) return '';
// 如果保留原有大小写
if (preserveCase && str.length > 0 && /[A-Z]/.test(str[0])) {
return str;
}
const first = str.charAt(0).toUpperCase();
const rest = str.slice(1);
return first + (lowerRest ? rest.toLowerCase() : rest);
}
export interface CapitalizeOptions {
/** 是否将剩余部分转为小写 */
lowerRest?: boolean;
/** 是否保留原有的大小写 */
preserveCase?: boolean;
/** 是否对每个单词都进行首字母大写 */
words?: boolean;
}
/**
* 增强版首字母大写函数
* @param str - 输入字符串
* @param options - 配置选项
* @returns 转换后的字符串
*
* @example
console.log(capitalize('hello')); // 'Hello'
console.log(capitalize('HELLO')); // 'Hello'
console.log(capitalize('hello world')); // 'Hello world'
console.log(capitalize('')); // ''
console.log(capitalize('中文测试')); // '中文测试'
console.log(capitalize('hello world')); // 'Hello world'
console.log(capitalize('hello world', { words: true })); // 'Hello World'
console.log(capitalize('HELLO WORLD', { preserveCase: true })); // 'HELLO WORLD'
console.log(capitalize('hello WORLD', { lowerRest: false })); // 'Hello WORLD'
*/
export function capitalize(str: string, options: CapitalizeOptions = {}): string {
// 处理空值和非字符串
if (!str) return '';
if (typeof str !== 'string') return '';
const { lowerRest = true, preserveCase = false, words = false } = options;
// 如果需要处理每个单词
if (words) {
return str
.split(/\s+/)
.map((word) => capitalizeWord(word, { lowerRest, preserveCase }))
.join(' ');
}
return capitalizeWord(str, { lowerRest, preserveCase });
}
export interface CamelCaseOptions {
/** 是否使用大驼峰(首字母大写) */
pascalCase?: boolean;
/** 是否保留数字之间的分隔符 */
preserveNumbers?: boolean;
/** 自定义单词分隔符 */
separator?: RegExp;
}
/**
* 增强版驼峰命名转换函数
* @param str - 输入字符串
* @param options - 配置选项
* @returns 驼峰命名格式的字符串
*
* @example
console.log(camelCase('hello world')); // 'helloWorld'
console.log(camelCase('hello-world')); // 'helloWorld'
console.log(camelCase('hello_world')); // 'helloWorld'
console.log(camelCase('HelloWorld')); // 'helloWorld'
console.log(camelCase(' hello world ')); // 'helloWorld'
// 增强的使用示例:
console.log(camelCase('hello world', { pascalCase: true })); // 'HelloWorld'
console.log(camelCase('hello123world', { preserveNumbers: true })); // 'hello123World'
console.log(camelCase('hello@#$world')); // 'helloWorld'
console.log(camelCase('hello.world', { separator: /\.+/g })); // 'helloWorld'
*/
export function camelCase(str: string, options: CamelCaseOptions = {}): string {
// 处理空值和非字符串
if (!str) return '';
if (typeof str !== 'string') return '';
const { pascalCase = false, preserveNumbers = false, separator = /[^a-zA-Z0-9]+/g } = options;
// 1. 清理字符串
let cleanStr = str.toLowerCase();
// 2. 处理数字
if (preserveNumbers) {
// 在数字前后添加空格,但保留连续的数字
cleanStr = cleanStr.replace(/([0-9]+)/g, ' $1 ');
}
// 3. 替换分隔符
cleanStr = cleanStr.replace(separator, ' ').trim();
// 4. 转换为驼峰格式
const words = cleanStr.split(' ').filter(Boolean);
if (!words.length) return '';
const camelCased = words.reduce((result, word, index) => {
if (index === 0 && !pascalCase) {
return word;
}
return result + capitalize(word);
}, '');
return pascalCase ? capitalize(camelCased) : camelCased;
}
export async function copy(txt: string) {
await navigator.clipboard.writeText(txt);
}
export async function paste(): Promise<string> {
return await navigator.clipboard.readText();
}
export function generatePassword(length = 10, includeSpecialCharacters = true) {
const lowerCaseLetters = 'abcdefghijklmnopqrstuvwxyz';
const upperCaseLetters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
const specialCharacters = '!@#$%^&*()-_=+[]{}|;:,.<>?';
const allCharacters = lowerCaseLetters + upperCaseLetters + (includeSpecialCharacters ? specialCharacters : '');
let password = '';
let hasLowerCase = false;
let hasUpperCase = false;
let hasSpecialCharacter = false;
while (!hasLowerCase || !hasUpperCase || (includeSpecialCharacters && !hasSpecialCharacter)) {
password = '';
for (let i = 0; i < length; i++) {
const randomIndex = Math.floor(Math.random() * allCharacters.length);
const char = allCharacters[randomIndex];
password += char;
if (lowerCaseLetters.includes(char)) {
hasLowerCase = true;
} else if (upperCaseLetters.includes(char)) {
hasUpperCase = true;
} else if (specialCharacters.includes(char)) {
hasSpecialCharacter = true;
}
}
}
return password;
}