Skip to content

titleCase

Capitalizes each significant word of a phrase, normalizing case in both directions: the locale’s minor words (articles, short conjunctions/ prepositions) are forced lowercase in the middle and known acronyms are forced uppercase — so “SOUTH BY SOUTHWEST” becomes “South by Southwest” and “jack smith, ceo” becomes “Jack Smith, CEO”. The first and last words are always capitalized. The locale supplies the minor-word and acronym lists (e.g. “le”/“de”/“par” for French); pass an explicit rules object for full control, including {style: "sentence"} to capitalize only the first word.

Recognized acronyms are forced to their canonical casing no matter how they were typed — ceo→CEO, gdp→GDP, sql→SQL — including mixed-case forms like iOS and SaaS.

import {titleCase} from "@d3plus/text";

titleCase("the new ceo and cfo");
titleCase("r&d drives gdp growth");
titleCase("the ios app and saas platform");
titleCase("html, css, and sql basics");

Capitalizes each word of a plain phrase; the minor word of stays lowercase in the middle, while the and a are capitalized here because they lead the string.

import {titleCase} from "@d3plus/text";

titleCase("hello world");
titleCase("the quick brown fox");
titleCase("a tale of two cities");

Case is fixed in both directions: the all-caps SOUTH BY SOUTHWEST is lowered to South by Southwest, the lowercase ceo is raised to CEO, and a trailing minor word like for is capitalized because it ends the phrase.

import {titleCase} from "@d3plus/text";

titleCase("SOUTH BY SOUTHWEST");
titleCase("of mice and men");
titleCase("jack smith, ceo");
titleCase("what are you looking for");

Passing a locale (es-ES, fr-FR, de-DE, it-IT) swaps in that language’s minor-word list, so words such as y, de, la, über, and di stay lowercase mid-title.

import {titleCase} from "@d3plus/text";

titleCase("el rápido zorro y el perro", "es-ES");
titleCase("le pdg de la société", "fr-FR");
titleCase("der bericht über das bip", "de-DE");
titleCase("la storia di roma", "it-IT");

Parameters

import {titleCase} from "@d3plus/text";
strrequiredstring | undefined

The string to capitalize.

localestring | TitleCaseRulesDefault en-US

A locale code (e.g. "en-US", "fr-FR") or an explicit rules object. Defaults to "en-US".