Skip to content

Instantly share code, notes, and snippets.

@muhibbudins
Created July 4, 2018 04:58
Show Gist options
  • Save muhibbudins/4706b30e71bfcfd63d832dc2248b65a2 to your computer and use it in GitHub Desktop.
Save muhibbudins/4706b30e71bfcfd63d832dc2248b65a2 to your computer and use it in GitHub Desktop.
[Just for wasting time] Add-on function for String to capitalize a words
/* Add-on prototype function on String */
String.prototype.capitalize = function() {
var string = this.toString();
if (string.length === 0) return;
var split = string.split(' ');
return split.map(function(item) {
return item.charAt(0).toUpperCase() + item.slice(1, item.length);
}).join(' ');
}
/* Usage */
'lorem'.capitalize() // Lorem
'lorem ipsum dolor sit amet'.capitalize() // "Lorem Ipsum Dolor Sit Amet"
''.capitalize() // undefined
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment