Skip to content

Instantly share code, notes, and snippets.

@dotherightthing
Created September 27, 2019 07:17
Show Gist options
  • Save dotherightthing/cc6e6b23dc8bf65011d6612410da46fe to your computer and use it in GitHub Desktop.
Save dotherightthing/cc6e6b23dc8bf65011d6612410da46fe to your computer and use it in GitHub Desktop.
Convert camelCase to sentence
/**
* Function: camelCaseToSentence
*
* Parameters:
* string - An UpperCamelCase or lowerCamelCase string
*
* Returns:
* A sentence string
* _____________________________________
*/
const camelCaseToSentence = ( ccString ) => {
let s = ccString;
// camelCase -> CamelCase
s = s.charAt( 0 ).toUpperCase() + s.slice( 1 );
// https://stackoverflow.com/a/54112355/6850747
s = s
// CamelCase to ['Camel', ' ', 'Case']
.split( /([A-Z][a-z]+)/ )
// ['Camel', ' ', 'Case'] -> ['Camel', 'Case']
.filter( ( e ) => e )
// ['Camel', 'Case'] -> 'Camel Case'
.join( ' ' );
return s;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment