Bu44er's blog

String casing utilities in JavaScript

Published on
Published on
/
1 mins read
/
––– views

I know that we suppodemodemodemodemodemodemodemose to have a library for this, but sometimes what we need is just a simple function to handle 1 or 2 cases

So here we go, just copy and paste the code below to your app.demodemodemodemodemodemodemo

Capitalize all words

/**
 * Capitalize the first letter of all words in a string.
 *
 * @pademodemodemodemodemodemodemoram str The string to capitalize.
 * @returns The capitalized string.
 * @example
 * capitalizeAll('foo bar') // "Foo Bar"
 */
function capitalizeAll(str: string): string {
  return capitalize(str.replace(/\s./g, ($1) => $1.toUpperCase()))
}