toTitleCase normalizes input text and converts it into title case.
It treats spaces, underscores, and hyphens as word boundaries, collapses repeated separators, and returns a clean, human-readable label.
When to use
- Rendering UI labels from machine-oriented keys.
- Normalizing user-entered text before display.
- Converting slug-like values to readable headings.
Behavior notes
- Empty input returns an empty string.
- Separator-only input returns an empty string.
- Numeric tokens are preserved.
- Punctuation attached to tokens is preserved.
Examples
toTitleCase('customer_success-manager');
// "Customer Success Manager"
toTitleCase(' q4-2026 product roadmap ');
// "Q4 2026 Product Roadmap"
Usage
import { toTitleCase } from '@branditdev/utils';
const rawLabel = 'customer_success-manager';
const rawHeading = ' q4-2026 product roadmap ';
const label = toTitleCase(rawLabel);
const heading = toTitleCase(rawHeading);
export const toTitleCaseDemo = { label, heading };
API
toTitleCase(
value):string
Defined in: string/toTitleCase/toTitleCase.ts:21
Converts text to title case using spaces, underscores, and hyphens as word separators.
The function normalizes separators into single spaces, removes extra separators, and transforms each token so the first character is uppercase while the rest are lowercase.
Parameters
| Name | Type | Optional | Default | Description |
|---|---|---|---|---|
| value | string | no | - | The input text to convert. |
Returns
string
A title-cased string with normalized spacing.
Source
export function toTitleCase(value: string): string {
if (value.length === 0) return value;
const tokens = value.split(/[\s_-]+/).filter(Boolean);
if (tokens.length === 0) return '';
return tokens
.map(token => token.charAt(0).toUpperCase() + token.slice(1).toLowerCase())
.join(' ');
}