Created
March 10, 2017 22:26
-
-
Save yefremov/bc142c5a623dd745f6a8b4003bba25fb to your computer and use it in GitHub Desktop.
Capitalize the first letter of a string, or all words in a string
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
/** | |
* Capitalize the first letter of a string, or all words in a string. | |
* | |
* @param {string} | |
* @returns {string} | |
* @example | |
* | |
* capitalize('foo bar baz'); | |
* => "Foo bar baz" | |
* | |
* capitalize.words('foo bar baz'); | |
* => "Foo Bar Baz" | |
*/ | |
function capitalize(str) { | |
return str.charAt(0).toUpperCase() + str.substring(1); | |
} | |
capitalize.words = function words(str) { | |
return str.split(/\s+/).map(capitalize).join(' '); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment