function toCamel(str, type) {
var arr = [];
arr = str.split("");
var res = arr.map((item, index) => {
if (item === '_') {
item = "";
if (arr[index + 1]) arr[index + 1] = arr[index + 1].toLocaleUpperCase();
}
return item;
});
var resStr = res.join("");
switch (type) {
case 'small':
resStr = resStr.replace(resStr[0], resStr[0].toLocaleLowerCase());//首字母转小写
break;
case 'big':
resStr = resStr.replace(resStr[0], resStr[0].toLocaleUpperCase());//首字母转大写
break;
default:
throw "转换类型错误 仅限 small big"
}
return resStr;
}
console.log("小驼峰", toCamel("user_name", "small"));//小驼峰
console.log("大驼峰", toCamel("user_name", "big"));//大驼峰
demo演示地址:http://demo.crazyming.com/?link=topic/d4/下划线命名转驼峰.html
1.如需转载本站原创文章,请务必注明文章出处并附上链接,非常感谢。
2.本站用于记录个人 工作、学习、生活,非商业网站,更多信息请 点击这里
下一篇: 去除字符串中最后一个指定的字符