· 7 years ago · Jan 07, 2018, 03:28 AM
1const sampleInputs = [
2 "OAuth Token",
3 "Hello-World!",
4];
5
6String.prototype.capitalized = function() {
7 const firstCharacter = this.charAt(0).toUpperCase();
8 const remainder = this.substring(1);
9 return firstCharacter + remainder;
10};
11
12String.prototype.camelCased = function() {
13 const words = this.split(/[- ]/);
14 let shouldCapitalizeNextWord = false;
15 let output = "";
16 for (wordIndex in words) {
17 let word = words[wordIndex];
18 let fixedWord = word.toLowerCase().replace(/[,.\/<>\?\!@#\$%\^&\*()\[\]\{\}]+/, '');
19 if (shouldCapitalizeNextWord) {
20 output += fixedWord.capitalized();
21 } else {
22 shouldCapitalizeNextWord = true;
23 output += fixedWord;
24 }
25 }
26 return output
27};
28
29for (idx in sampleInputs) {
30 const sampleInput = sampleInputs[idx];
31 console.log(sampleInput.camelCased());
32}
33// oauthToken
34// helloWorld