Created
February 8, 2019 06:07
-
-
Save davidroyer/156c94076732f3ae87ce4be1b61fcad7 to your computer and use it in GitHub Desktop.
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
/** | |
* Replaces hyphens with spaces. (only hyphens between word chars) | |
*/ | |
function unhyphenate(str){ | |
return str.replace(/(\w)(-)(\w)/g, '$1 $3'); | |
} | |
export const toTitleCase = function (str) { | |
str = str.toLowerCase().split(' '); | |
for (var i = 0; i < str.length; i++) { | |
str[i] = str[i].charAt(0).toUpperCase() + str[i].slice(1); | |
} | |
return unhyphenate(str.join(' ')); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment