Skip to content

Instantly share code, notes, and snippets.

@mubaidr
Created August 13, 2018 17:50
Show Gist options
  • Save mubaidr/336e5bba75925c34ae1f52fe43621352 to your computer and use it in GitHub Desktop.
Save mubaidr/336e5bba75925c34ae1f52fe43621352 to your computer and use it in GitHub Desktop.
JavaScript-toProperCase
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