Skip to content

Instantly share code, notes, and snippets.

@davidroyer
Created February 8, 2019 06:07
Show Gist options
  • Save davidroyer/156c94076732f3ae87ce4be1b61fcad7 to your computer and use it in GitHub Desktop.
Save davidroyer/156c94076732f3ae87ce4be1b61fcad7 to your computer and use it in GitHub Desktop.
/**
* 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