Created
September 27, 2019 07:17
-
-
Save dotherightthing/cc6e6b23dc8bf65011d6612410da46fe to your computer and use it in GitHub Desktop.
Convert camelCase to sentence
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: 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