Created
January 24, 2020 01:27
-
-
Save fauxsaurus/a502c09f2e087654bb4f3363d74683e2 to your computer and use it in GitHub Desktop.
Convert PascalCase, camelCase, snake_case, & kebab-case into "space case".
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
export const spaceCase = txtWithNoSpaces => | |
txtWithNoSpaces | |
.split(/_|-/g) // snake_case & kebab-case | |
.join(' ') | |
.replace(/([A-Z])/g, ' $1') // PascalCase & camelCase | |
.split(' ') | |
.map(word => word.charAt(0).toUpperCase() + word.slice(1)) | |
.join(' ') | |
.replace(/\s+/, ' ') // dashed entries create two spaces, bump that down to one | |
.replace(/([A-Z])\s(?=[A-Z]\b)/g, '$1') // e.g. "U U I D" => "UUID" | |
.trim() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment