formatText (slugify, capitalize)
String transforms and slug generation.
6.6 KB
•Updated on September 3, 2026
Installation
Bash
pnpm dlx shadcn@latest add @spaceui/hooks-utils-format-textUsage
Code
import { truncateText, randomWord, capitalizeText } from '@/utils/format-text'
// Sentence case (capitalizes the first letter of each sentence)
capitalizeText('SPACE UI IS THE BEST UI LIBRAY. TRY IT TODAY!')
// => "Space ui is the best ui libray. Try it today!"
// Words case (capitalizes the first letter of every word)
capitalizeText('SPACE UI IS THE BEST UI LIBRAY', 'words')
// => "Space Ui Is The Best Ui Libray"
// Truncate text
truncateText('Space UI is the best UI library!', { maxLength: 8 }) // => "Space..."API Reference
capitalizeText(text, options?)
Capitalizes text by sentence, words, or first letter while optionally lowercasing the rest.
| Parameter | Type | Default | Description |
|---|---|---|---|
text | string | required | The input string to capitalize. |
options.mode | sentence|words|first | sentence | Capitalization strategy. Can also be passed directly as a string (e.g. 'words'). |
options.lowerRest | boolean | true | When true, lowercases all non-capitalized characters to ensure only the target letters are uppercase. |
Shorthand helpers:
capitalizeWords(text, options?)- Shorthand forcapitalizeText(text, { mode: 'words' }).capitalizeFirst(text, options?)- Shorthand forcapitalizeText(text, { mode: 'first' }).capitalize(text, options?)- Alias forcapitalizeText.
truncateText(text, options?)
Truncates a string by character or word count and adds an ellipsis.
| Parameter | Type | Default | Description |
|---|---|---|---|
text | string | required | The input string to truncate. |
options.type | char|word | char | Truncation mode. |
options.maxLength | number | 40 | Maximum length before truncation. |
randomWord(options?)
Generates pseudo-Latin random words without external dictionaries.
| Parameter | Type | Default | Description |
|---|---|---|---|
options.count | number | 1 | Number of words to generate. |
options.length | number | random (3-8) | Length of each word. |
options.casing | lowercase|uppercase|capitalize | lowercase | Output casing style. |
options.separator | string | Separator between words. | |
options.alphanumeric | boolean | false | Includes numeric digits in words. |