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

21 lines
836 B
TypeScript
Raw Normal View History

2025-01-13 02:49:20 +00:00
/**
* camelCase PascalCase
* @param str
* @param isPascalCase PascalCase false
* @returns
*/
export function camelCase(str: string, isPascalCase: boolean = false): string {
// 将字符串分割成单词数组
let words: string[] = str.split(/[\s_-]+/);
// 将数组中的每个单词首字母大写(除了第一个单词)
let camelCased: string[] = words.map((word, index):string => {
if (index == 0 && !isPascalCase) {
return word.toLowerCase(); // 第一个单词全小写
}
return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();
});
// 将数组中的单词拼接成一个字符串
return camelCased.join('');
};