Skip to content

titleCase

Edit

Converts text from common word boundaries into readable title case.

titleCase.ts
const words = (value: string): string[] => {
return value
.replace(/([\p{Ll}\d])(\p{Lu}(?=\p{Ll}))/gu, "$1 $2")
.replace(/(\p{Lu}+)(\p{Lu}\p{Ll})/gu, "$1 $2")
.match(/[\p{L}\p{N}]+/gu) ?? [];
};
/** Converts text from common word boundaries into title case. */
export const titleCase = (value: string): string => {
return words(value)
.map((word) => {
const lower = word.toLocaleLowerCase();
return lower.charAt(0).toLocaleUpperCase() + lower.slice(1);
})
.join(" ");
};
Terminal
wget -O src/lib/titleCase.ts https://raw.githubusercontent.com/jrTilak/lazykit/HEAD/registry/functions/titleCase.ts
titleCase.example.ts
import { titleCase } from "./titleCase";
const heading = titleCase("HTTPResponse_code");
// "Http Response Code"
  • value (string) — Text containing common word boundaries.
  • Returns capitalized words separated by spaces.