1
0

index.ts 1.0 KB

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