Created
August 13, 2018 17:50
-
-
Save mubaidr/336e5bba75925c34ae1f52fe43621352 to your computer and use it in GitHub Desktop.
JavaScript-toProperCase
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
String.prototype.toProperCase = function (convertCamelCase) { | |
var words = this.split(' '); | |
var proper = []; | |
for (var index in words) { | |
if (convertCamelCase) { | |
proper.push(words[index].replace(/([A-Z])/g, ' $1') | |
.replace(/^./, function (str) { | |
return str.toUpperCase(); | |
})); | |
} else { | |
proper.push(words[index][0].toUpperCase() + words[index].slice(1)); | |
} | |
} | |
return proper.join(' '); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment