Skip to content

Instantly share code, notes, and snippets.

@asmallteapot
Created December 31, 2017 22:23
Show Gist options
  • Save asmallteapot/3fb2b955927aa97fe3659205a2b5146d to your computer and use it in GitHub Desktop.
Save asmallteapot/3fb2b955927aa97fe3659205a2b5146d to your computer and use it in GitHub Desktop.
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