Skip to content

Instantly share code, notes, and snippets.

@yefremov
Created March 10, 2017 22:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yefremov/bc142c5a623dd745f6a8b4003bba25fb to your computer and use it in GitHub Desktop.
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
/**
* 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