-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconvertToCamelCase.js
More file actions
25 lines (22 loc) · 861 Bytes
/
Copy pathconvertToCamelCase.js
File metadata and controls
25 lines (22 loc) · 861 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/*
Complete the method/function so that it converts dash/underscore delimited words into camel casing. The first word within the output should be capitalized only if the original word was capitalized.
Examples:
// returns "theStealthWarrior"
toCamelCase("the-stealth-warrior")
// returns "TheStealthWarrior"
toCamelCase("The_Stealth_Warrior")
*/
function toCamelCase(str){
var strArray;
if (str.indexOf('-') !== -1){ //if delineated by -
strArray = str.split('-');
} else {
strArray = str.split('_'); //if delineated by _
}
var camelCase = strArray[0]; //keeps first word value as is
for (var i=1, len=strArray.length; i < len; i++){
var capitalized = strArray[i].substr(0, 1).toUpperCase() + strArray[i].slice(1); //redundant but clearer
camelCase += capitalized;
}
return camelCase;
}