Catalog
  1. 1. Ada
  2. 2. Beck
  3. 3. Helen
  4. 4. Irene
  5. 5. Mia
  6. 6. Jack
写一个函数把下划线命名转化为小驼峰命名

如:输入字符串a_bc_de,返回字符串aBcDe

Ada

1
2
3
4
5
function format (num) {
return num.replace(/_(\w)/g, function(all, letter){
return letter.toUpperCase();
});
}

Beck

1
2
3
4
5
6
7
function underscore2upper(str) {
let words = str.split('_');
let newWords = words.map((element, index) => {
return index === 0 ? element : element[0].toUpperCase() + element.substring(1, element.length)
});
return newWords.join('');
}

Helen

1
2
3
4
5
6
7
8
9
// a.找到_位置,如果_后一位为字母,大写
// b.将多余的_去除
function camelCase(str) {
let outStr = str.replace(/_([a-zA-Z])/g, function (regValue, groupValue) {
return groupValue.toUpperCase();
});
return outStr.replace(/_/g, '');
}
console.log(camelCase('_____a_bc_de__________w___'));

Irene

1
2
3
function test(item){
return item.replace(/_([a-zA-Z])/g, (a, b) => b.toUpperCase())
}

Mia

1
2
3
4
5
6
7
8
9
10
11
function f2(str){
return str.split("_")
.map(function(item, i){
if(i>0){
return item.replace(item[0],item[0].toUpperCase());
}else{
return item;
}
}).join("");
}
f2("a_bc_de");

Jack

1
2
3
4
5
6
7
function toString(foo){
var arr = foo.split('_');
for(let i=1; i<arr.length; i++){
arr[i]=arr[i].charAt(0).toUpperCase()+arr[i].substr(1);
}
return arr.join('')
}
Author: Erealm
Link: http://erealmsoft.github.io/2019/09/28/purefunc/purefunc-small-hump/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.

Comment