web
20 小时以前 49fa0d82a40345342966e810b44429aec0480ef3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// @ts-nocheck
// export function toLowercaseSeparator(key: string) {
//   return key.replace(/([A-Z])/g, '-$1').toLowerCase();
// }
 
/**
 * 将字符串转换为指定连接符的命名约定
 * @param str 要转换的字符串
 * @param separator 指定的连接符,默认为 "-"
 * @returns 转换后的字符串
 */
export function kebabCase(str : string, separator : string = "-") : string {
    return str
        
        // #ifdef UNI-APP-X && APP
        .replace(/[A-Z]/g, (match : string, _ : number, _ : string) : string => `${separator}${match.toLowerCase()}`) // 将大写字母替换为连接符加小写字母
        // #endif
        // #ifndef UNI-APP-X && APP
        .replace(/[A-Z]/g, (match : string) : string => `${separator}${match.toLowerCase()}`) // 将大写字母替换为连接符加小写字母
        // #endif
        .replace(/[\s_-]+/g, separator) // 将空格、下划线和短横线替换为指定连接符
        .replace(new RegExp(`^${separator}|${separator}$`, "g"), "") // 删除开头和结尾的连接符
        .toLowerCase(); // 将结果转换为全小写
}