Transform a string into title case following English rules
npx @tessl/cli install tessl/npm-title-case@4.3.0Title Case transforms strings into proper title case following English grammar rules. It handles complex scenarios like hyphenated words, small words, acronyms, URLs, and preserves manual casing (e.g., "iPhone", "iOS") while providing extensive configuration options for different capitalization needs.
npm install title-caseThis is a pure ESM package and must be imported using ES module syntax:
import {
titleCase,
Options,
WORD_SEPARATORS,
SENTENCE_TERMINATORS,
TITLE_TERMINATORS,
SMALL_WORDS
} from "title-case";Note: This package cannot be used with require() or CommonJS module resolution. It is ESM-only.
import { titleCase } from "title-case";
// Simple transformation
titleCase("hello world"); // "Hello World"
titleCase("follow step-by-step instructions"); // "Follow Step-by-Step Instructions"
// With locale support
titleCase("piña colada", "en-US"); // "Piña Colada"
// Sentence case mode
titleCase("one sentence. two sentences.", { sentenceCase: true });
// "One sentence. Two sentences."Transforms input strings into proper title case following English grammar rules with intelligent handling of special cases.
/**
* Transform a string into title case following English rules
* @param input - The string to transform
* @param options - Configuration options or locale string/array
* @returns The transformed title case string
*/
function titleCase(
input: string,
options: Options | string[] | string = {}
): string;
interface Options {
/** Locale used for toLocaleUpperCase during case transformation */
locale?: string | string[];
/** Only capitalize the first word of each sentence (default: false) */
sentenceCase?: boolean;
/** Set of characters to consider a new sentence under sentence case behavior */
sentenceTerminators?: Set<string>;
/** Set of words to keep lower-case when sentenceCase === false */
smallWords?: Set<string>;
/** Set of characters to consider a new sentence under title case behavior */
titleTerminators?: Set<string>;
/** Set of characters to consider a new word for capitalization, such as hyphenation */
wordSeparators?: Set<string>;
}Usage Examples:
import { titleCase, SMALL_WORDS } from "title-case";
// Basic title case
titleCase("the quick brown fox"); // "The Quick Brown Fox"
// Preserves special cases
titleCase("visit example.com for more info"); // "Visit example.com for More Info"
titleCase("iPhone and iOS development"); // "iPhone and iOS Development"
// Handles hyphenated words
titleCase("state-of-the-art technology"); // "State-of-the-Art Technology"
// Custom small words
const customSmallWords = new Set([...SMALL_WORDS, "via", "per"]);
titleCase("send via email per instructions", {
smallWords: customSmallWords
}); // "Send via Email per Instructions"
// Sentence case mode
titleCase("first sentence. second sentence! third sentence?", {
sentenceCase: true
}); // "First sentence. Second sentence! Third sentence?"
// Locale-specific capitalization
titleCase("straße in münchen", "de-DE"); // "Straße in München"Pre-defined sets of characters and words for common title case scenarios.
/** Default word separator characters: "—", "–", "-", "―", "/" */
const WORD_SEPARATORS: Set<string>;
/** Default sentence terminator characters: ".", "!", "?" */
const SENTENCE_TERMINATORS: Set<string>;
/** Default title terminator characters: includes sentence terminators plus ":", '"', "'", """ */
const TITLE_TERMINATORS: Set<string>;
/** Default small words that remain lowercase in title case */
const SMALL_WORDS: Set<string>;The SMALL_WORDS set includes common English articles, prepositions, and conjunctions:
Usage Examples:
import {
titleCase,
SMALL_WORDS,
WORD_SEPARATORS,
TITLE_TERMINATORS
} from "title-case";
// Extend default small words
const extendedSmallWords = new Set([...SMALL_WORDS, "via", "per"]);
// Custom word separators
const customSeparators = new Set([...WORD_SEPARATORS, "|", "\\"]);
// Custom configuration
titleCase("connect via ethernet|wifi per requirements", {
smallWords: extendedSmallWords,
wordSeparators: customSeparators
}); // "Connect via Ethernet|Wifi per Requirements"Title Case intelligently preserves existing casing in special scenarios:
example.com, user@domain.com remain unchangediPhone, iOS, jQuery preserve their specific capitalizationU.S.A., N.A.S.A. are handled appropriatelystate-of-the-arttoLocaleUpperCase() for proper locale-specific capitalizationThe function handles edge cases gracefully: