Last active
July 21, 2021 18:23
-
-
Save mbruno-kr/0edc472e337058dddcae635b31baca57 to your computer and use it in GitHub Desktop.
To NameCase
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
function capitalizeFirstLetter(str) { | |
return str.charAt(0).toUpperCase() + str.slice(1); | |
} | |
function toNameCase(str) { | |
return str.split(" ").map(capitalizeFirstLetter).join(" ") | |
} | |
console.log(toNameCase("banana man")) | |
console.log(toNameCase("ole McDonald")) |
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
/** | |
* | |
* @param {string} str | |
* @returns {*} | |
*/ | |
function toNameCase(str) { | |
return str.replace(/(^|\s)(.)/g, (m) => m.toUpperCase()) | |
} | |
console.log(toNameCase("banana man")) | |
console.log(toNameCase("ole McDonald")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Outputs: