@cc-heart/utils
Version:
🔧 javascript common tools collection
31 lines (30 loc) • 770 B
text/typescript
/**
* Converts an underline-separated string to camel case.
* e.g. underlineToHump('hello_word') => 'helloWord'
*
* @param {string} target - The underline-separated string to convert.
* @return {string} The camel case version of the input string.
*/
export function underlineToHump(target: string) {
let isStartUnderline = true
let prefixStr = null
let str = ''
for (let i = 0; i < target.length; i++) {
if (
target[i] === '_' &&
/[a-z]/.test(target[i + 1]) &&
!isStartUnderline &&
prefixStr !== '_'
) {
i++
str += target[i].toUpperCase()
continue
}
prefixStr = target[i]
if (isStartUnderline && target[i] !== '_') {
isStartUnderline = false
}
str += target[i]
}
return str
}