-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtoCamelCase.js
More file actions
26 lines (21 loc) · 755 Bytes
/
toCamelCase.js
File metadata and controls
26 lines (21 loc) · 755 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
26
/**
Description:
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) {
if (!str) return ''
return str
.replace(/\-/g, '_')
.split('_')
.reduce((res, word, i) => {
let firstChar = (i === 0 && word[0] === word[0].toLowerCase())
? word[0]
: word[0].toUpperCase()
res.push(firstChar + word.substr(1))
return res}, []).join('')
}