cxc-szcx-uniapp/uni_modules/lime-shared/kebabCase/index.ts

24 lines
1.0 KiB
TypeScript
Raw Normal View History

2025-01-13 02:49:20 +00:00
// @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 APP-IOS || APP-ANDROID
.replace(/[A-Z]/g, (match : string, _ : number, _ : string) : string => `${separator}${match.toLowerCase()}`) // 将大写字母替换为连接符加小写字母
// #endif
// #ifndef APP-IOS || APP-ANDROID
.replace(/[A-Z]/g, (match : string) : string => `${separator}${match.toLowerCase()}`) // 将大写字母替换为连接符加小写字母
// #endif
.replace(/[\s_-]+/g, separator) // 将空格、下划线和短横线替换为指定连接符
.replace(new RegExp(`^${separator}|${separator}$`, "g"), "") // 删除开头和结尾的连接符
.toLowerCase(); // 将结果转换为全小写
}