@cc-heart/utils
Version:
🔧 javascript common tools collection
31 lines (29 loc) • 887 B
JavaScript
/**
* 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.
*/
function underlineToHump(target) {
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;
}
export { underlineToHump };