-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
convert-string-to-camel-case.js
47 lines (38 loc) · 1.66 KB
/
convert-string-to-camel-case.js
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// toCamelCase("the-stealth-warrior") // returns "theStealthWarrior"
// toCamelCase("The_Stealth_Warrior") // returns "TheStealthWarrior"
function toCamelCase(str) {
// split the string by dash or underscore
const words = str.split(/-|_/g);
let camelCase = words[0];
// a place to store the camelCase string
// skip capitalizing the first word
// iterate over the split string
for (let i = 1; i < words.length; i++) {
const word = words[i];
// append the current word Capitalized to the camelCase string
camelCase += word[0].toUpperCase() + word.slice(1);
}
// return the camelCase string
return camelCase;
}
function toCamelCase(str) {
return str.split(/-|_/g).reduce((camelCase, word, index) => {
if (index === 0) {
return word;
}
return camelCase + word[0].toUpperCase() + word.slice(1);
}, '');
}
const capitalize = word => word[0].toUpperCase() + word.slice(1);
function toCamelCase(str) {
return str.split(/-|_/g).reduce((camelCase, word, index) => {
return index === 0 ? word : camelCase + capitalize(word);
}, '');
}
function toCamelCase(str) {
return str.split(/-|_/g).reduce((camelCase, word, index) => index === 0 ? word : camelCase + capitalize(word), '');
}
console.log(toCamelCase(''), '', 'An empty string was provided but not returned');
console.log(toCamelCase('the_stealth_warrior'), 'theStealthWarrior', 'toCamelCase(\'the_stealth_warrior\') did not return correct value');
console.log(toCamelCase('The-Stealth-Warrior'), 'TheStealthWarrior', 'toCamelCase(\'The-Stealth-Warrior\') did not return correct value');
console.log(toCamelCase('A-B-C'), 'ABC', 'toCamelCase(\'A-B-C\') did not return correct value');