Text
Text manipulation utilities for truncation, pluralization, capitalization, and more.
truncate
Shortens text to a specified length with customizable suffix and word boundary handling.
import { truncate } from "@tilt-legal/cubitt-components/utilities/formatters";
truncate('This is a long text', 10); // "This is..."
truncate('Long text', 10, { suffix: '…' }); // "Long text…"
truncate('Hello world from Cubitt', 15, { wordBoundary: true }); // "Hello world..."| Property | Type | Default | Description |
|---|---|---|---|
suffix | string | '...' | String to append when truncated |
wordBoundary | boolean | false | Truncate at word boundaries |
truncateMiddle
Truncates text in the middle, preserving the start and end. Useful for filenames where the extension is important, similar to macOS Finder behavior.
import { truncateMiddle } from "@tilt-legal/cubitt-components/utilities/formatters";
truncateMiddle('Short.txt', 20); // "Short.txt"
truncateMiddle('VeryLongFilename.pdf', 15); // "VeryLo…me.pdf"
truncateMiddle('Document_Final_v2.docx', 18); // "Documen…v2.docx"
truncateMiddle('Quarterly_Report_2024_Final.xlsx', 20); // "Quarter…inal.xlsx"| Property | Type | Default | Description |
|---|---|---|---|
ellipsis | string | '…' | Character to use for ellipsis |
formatPlural
Intelligently handles singular and plural forms with custom plural rules and count display.
import { formatPlural } from "@tilt-legal/cubitt-components/utilities/formatters";
formatPlural(1, 'file'); // "1 file"
formatPlural(5, 'file'); // "5 files"
formatPlural(2, 'child', { plural: 'children' }); // "2 children"
formatPlural(0, 'item', { zero: 'no items' }); // "no items"
formatPlural(3, 'person', { includeCount: false }); // "people"| Property | Type | Default | Description |
|---|---|---|---|
plural | string | - | Custom plural form |
includeCount | boolean | true | Include count in output |
zero | string | - | Special form for count of 0 |
formatList
Formats arrays into grammatically correct lists with locale-aware conjunction or disjunction.
import { formatList } from "@tilt-legal/cubitt-components/utilities/formatters";
formatList(['apple', 'banana', 'cherry']); // "apple, banana, and cherry"
formatList(['red', 'green'], { type: 'disjunction' }); // "red or green"
formatList(['one', 'two', 'three'], { locale: 'en-US' }); // "one, two, and three"| Property | Type | Default | Description |
|---|---|---|---|
locale | string | 'en-AU' | Locale for formatting |
type | 'conjunction' | 'disjunction' | 'conjunction' | List type (and/or) |
formatInitials
Extracts initials from a full name, using first and last name only.
import { formatInitials } from "@tilt-legal/cubitt-components/utilities/formatters";
formatInitials('John Doe'); // "JD"
formatInitials('Mary Jane Smith'); // "MS"
formatInitials('Alice'); // "A"
formatInitials(''); // ""highlightText
Wraps matching text in HTML tags for search highlighting with customizable styling.
import { highlightText } from "@tilt-legal/cubitt-components/utilities/formatters";
highlightText('Hello World', 'world'); // "Hello <mark>World</mark>"
highlightText('Search result', 'result', {
tag: 'span',
className: 'bg-yellow-200'
}); // "Search <span class=\"bg-yellow-200\">result</span>"| Property | Type | Default | Description |
|---|---|---|---|
caseSensitive | boolean | false | Case-sensitive matching |
tag | string | 'mark' | HTML tag for highlighting |
className | string | - | CSS class for highlight tag |
capitalize
Capitalizes the first letter of a string and converts the rest to lowercase.
import { capitalize } from "@tilt-legal/cubitt-components/utilities/formatters";
capitalize('hello world'); // "Hello world"
capitalize('HELLO WORLD'); // "Hello world"
capitalize(''); // ""toTitleCase
Converts a string to title case by capitalizing the first letter of each word.
import { toTitleCase } from "@tilt-legal/cubitt-components/utilities/formatters";
toTitleCase('hello world'); // "Hello World"
toTitleCase('the quick brown fox'); // "The Quick Brown Fox"
toTitleCase('HELLO WORLD'); // "Hello World"toFieldLabel
Converts field paths to human-readable labels, extracting the last segment and handling camelCase, snake_case, and kebab-case.
import { toFieldLabel } from "@tilt-legal/cubitt-components/utilities/formatters";
toFieldLabel('firstName'); // "First name"
toFieldLabel('user_email'); // "User email"
toFieldLabel('kebab-case'); // "Kebab case"
toFieldLabel('camelCaseField'); // "Camel case field"
toFieldLabel('client.name'); // "Name"
toFieldLabel('addresses[0].street'); // "Street"