Title Case
VerifiedConverts a sentence to Title Case, keeping minor words like "of" and "the" lowercase.
- string
- case
- titlecase
Install
npx shadcn@latest add https://quicksnip.dev/r/title-case.jsonCode
const MINOR = new Set(["a", "an", "and", "as", "at", "but", "by", "for", "in", "of", "on", "or", "the", "to", "vs", "via"]);
/**
* Converts a sentence to Title Case.
* Minor words (a, of, the…) stay lowercase unless they are the first or last word.
*/
export function titleCase(input: string): string {
const parts = input.toLowerCase().trim().split(/\s+/).filter(Boolean);
return parts
.map((word, i) =>
i !== 0 && i !== parts.length - 1 && MINOR.has(word) ? word : word[0].toUpperCase() + word.slice(1),
)
.join(" ");
}Usage
titleCase("the lord of the rings"); // "The Lord of the Rings"