forked from KNXCloud/lowcode-engine-vue
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.ts
More file actions
46 lines (44 loc) · 1.04 KB
/
string.ts
File metadata and controls
46 lines (44 loc) · 1.04 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
/**
* 将驼峰命名法的字符串转化为中划线连接的字符串
*
* @example
*
* const res = kebabCase('userName'); // user-name
*
* @param str - 被转换的字符串
*/
export function kebabCase(str: string): string {
if (!str) return str;
return str.replace(/[A-Z]/g, (c, i) => {
const suf = i > 0 ? '-' : '';
return suf + c.toLocaleLowerCase();
});
}
/**
* 将中划线连接的字符串转化为小驼峰命名法
*
* @example
*
* const res = camelCase('user-name'); // userName
*
* @param str - 被转换的字符串
*/
export function camelCase(str: string): string {
if (!str) return str;
return str.replace(/-[a-zA-Z]/g, (c) => {
return c.charAt(1).toLocaleUpperCase();
});
}
/**
* 将中划线连接的字符串转化为大驼峰命名法
*
* @example
*
* const res = pascalCase('user-name'); // UserName
*
* @param str - 被转换的字符串
*/
export function pascalCase(str: string): string {
const res = camelCase(str);
return res && res.charAt(0).toLocaleUpperCase() + res.slice(1);
}