Last active
May 15, 2022 15:32
-
-
Save kieranbarker/293b74f1b3b46272315d2e1719786b03 to your computer and use it in GitHub Desktop.
Convert a string to title case
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
/** | |
* Convert a string to title case. | |
* https://gist.github.com/kieranbarker/293b74f1b3b46272315d2e1719786b03 | |
* @param {string} str The string to convert. | |
* @returns {string} The converted string. | |
*/ | |
function toTitleCase(str) { | |
return str | |
.toLowerCase() | |
.split(" ") | |
.map(function (word) { | |
return word.charAt(0).toUpperCase() + word.slice(1); | |
}) | |
.join(" "); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Please note: this is my adaptation of a function written by Sonya Moisset. All I did was rewrite it in my preferred style using the array
map()
method instead of afor
loop.