Created
December 31, 2017 22:23
-
-
Save asmallteapot/3fb2b955927aa97fe3659205a2b5146d to your computer and use it in GitHub Desktop.
https://gist.github.com/asmallteapot/6fcd3c1484943e6800ae690e6ad35fef but in JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const sampleInputs = [ | |
"OAuth Token", | |
"Hello-World!", | |
]; | |
String.prototype.capitalized = function() { | |
const firstCharacter = this.charAt(0).toUpperCase(); | |
const remainder = this.substring(1); | |
return firstCharacter + remainder; | |
}; | |
String.prototype.camelCased = function() { | |
const words = this.split(/[- ]/); | |
let shouldCapitalizeNextWord = false; | |
let output = ""; | |
for (wordIndex in words) { | |
let word = words[wordIndex]; | |
let fixedWord = word.toLowerCase().replace(/[,.\/<>\?\!@#\$%\^&\*()\[\]\{\}]+/, ''); | |
if (shouldCapitalizeNextWord) { | |
output += fixedWord.capitalized(); | |
} else { | |
shouldCapitalizeNextWord = true; | |
output += fixedWord; | |
} | |
} | |
return output | |
}; | |
for (idx in sampleInputs) { | |
const sampleInput = sampleInputs[idx]; | |
console.log(sampleInput.camelCased()); | |
} | |
// oauthToken | |
// helloWorld |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment