Created
July 4, 2018 04:58
-
-
Save hibuno/4706b30e71bfcfd63d832dc2248b65a2 to your computer and use it in GitHub Desktop.
[Just for wasting time] Add-on function for String to capitalize a words
This file contains hidden or 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
/* 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