合同小程序前端代码仓库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

23 lines
1.0 KiB

  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. }