Files
30-seconds-of-code/snippets/toTitleCase.md

925 B

toTitleCase

Converts a string to title case.

The algorithm featured here breaks down as follows:

  1. Break the string into words, using a Regular Expression
  2. Capitalize each of them
  3. Stich them back together, using the whitespace ' ' character as a separator
const toTitleCase = str => str
  .match(/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g)
  .map(x => x.charAt(0).toUpperCase() + x.slice(1).toLowerCase())
  .join(' ');
toTitleCase('some_database_field_name'); // 'Some Database Field Name'
toTitleCase('Some label that needs to be title-cased'); // 'Some Label That Needs To Be Title Cased'
toTitleCase('some-package-name'); // 'Some Package Name'
toTitleCase('some-mixed_string with spaces_underscores-and-hyphens'); // 'Some Mixed String With Spaces Underscores And Hyphens'