Skip to content

Instantly share code, notes, and snippets.

@fauxsaurus
Created January 24, 2020 01:27
Show Gist options
  • Save fauxsaurus/a502c09f2e087654bb4f3363d74683e2 to your computer and use it in GitHub Desktop.
Save fauxsaurus/a502c09f2e087654bb4f3363d74683e2 to your computer and use it in GitHub Desktop.
Convert PascalCase, camelCase, snake_case, & kebab-case into "space case".
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