Skip to content

Instantly share code, notes, and snippets.

@ardislu
Created June 2, 2023 04:35
Show Gist options
  • Save ardislu/babe8e0a5b2a00b1e507c7178dd212b6 to your computer and use it in GitHub Desktop.
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.
/**
* 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