Created
June 2, 2023 04:35
-
-
Save ardislu/babe8e0a5b2a00b1e507c7178dd212b6 to your computer and use it in GitHub Desktop.
Simple utility function to convert a camelCase string into Title Case With Spaces for human readability.
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
/** | |
* Converts a camelCase string into Title Case With Spaces for human readability. | |
* @param {string} str - A string in camelCase. | |
* @returns {string} The input string converted into Title Case With Spaces. | |
*/ | |
function camelCaseToTitleCase(str) { | |
str = str[0].toLocaleUpperCase() + str.slice(1); | |
return str.replaceAll(/([A-Z]+)/g, ' $1').slice(1); | |
} | |
// camelCaseToTitleCase('exampleCamelCaseString'); | |
// 'Example Camel Case String' | |
// camelCaseToTitleCase('acronymExampleJSON'); | |
// 'Acronym Example JSON' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment