truncate()
truncate(
str,options?):string
Truncates string if it's longer than the given maximum length.
Parametersβ
str: stringβ
The string to truncate.
options?: TruncateOptions = {}β
The options object.
Returns: stringβ
The truncated string.
Sinceβ
2.0.0
Also known asβ
truncate (Lodash, es-toolkit, Remeda, Modern Dash) Β· β (Radashi, Ramda, Effect, Antfu)
Exampleβ
truncate('hi-diddly-ho there, neighborino');
// => 'hi-diddly-ho there, neighbo...'
truncate('hi-diddly-ho there, neighborino', { length: 24 });
// => 'hi-diddly-ho there, n...'
truncate('hi-diddly-ho there, neighborino', {
length: 24,
separator: ' '
});
// => 'hi-diddly-ho there,...'
truncate('hi-diddly-ho there, neighborino', {
length: 24,
separator: /,? +/
});
// => 'hi-diddly-ho there...'
truncate('hi-diddly-ho there, neighborino', {
omission: ' [...]'
});
// => 'hi-diddly-ho there, neig [...]'
How it works?β
Truncates string to a maximum length with customizable omission.
Optionsβ
| Option | Default | Description |
|---|---|---|
length | 30 | Max string length |
omission | '...' | Truncation indicator |
separator | β | Break at word boundary |
Word Boundaryβ
Without separator, cuts mid-word. With separator, breaks at last occurrence.
Custom Omissionβ
Use Casesβ
Preview text for content cards πβ
Display short previews of long content in cards or lists. Essential for article listings, product descriptions, or comment previews.
const preview = truncate(article.content, { length: 80 });
// => "TypeScript generics provide a way to create reusable components..."
Word-boundary truncation for readable excerpts πβ
Truncate at word boundaries for more natural reading. Avoids cutting words in half.
truncate(description, { length: 30, separator: ' ' });
// => "The quick brown fox jumps..."
Custom omission for branded experiencesβ
Use custom omission strings for branded "read more" indicators.
truncate(post, { length: 50, omission: 'β¦ [voir plus]' });
// => "This is a very long social⦠[voir plus]"
Truncate file names in a file explorerβ
Shorten long file names in a file tree or upload list while keeping the extension visible.
const truncateFileName = (name: string, maxLen: number) => {
const ext = name.slice(name.lastIndexOf('.'));
if (name.length <= maxLen) return name;
return truncate(name.slice(0, -ext.length), { length: maxLen - ext.length }) + ext;
};
truncateFileName('my-very-long-document-name-final-v2.pdf', 25);
// => "my-very-long-docum....pdf"
Truncate notification messages for mobileβ
Shorten notification text for mobile push notifications with character limits. Essential for PWAs and mobile apps where push notification length is constrained.
const pushTitle = truncate(notification.title, { length: 50 });
const pushBody = truncate(notification.body, { length: 120, separator: " " });
sendPushNotification({ title: pushTitle, body: pushBody });
Truncate breadcrumb labels in navigationβ
Shorten long breadcrumb segments to prevent layout overflow. Perfect for responsive navigation in content-heavy applications.
const breadcrumbs = ["Home", "Documentation", "API Reference", "Very Long Category Name Here"];
const displayCrumbs = breadcrumbs.map((label) =>
truncate(label, { length: 20, separator: " " })
);
// => ["Home", "Documentation", "API Reference", "Very Long Category..."]
TruncateOptionsβ
Options for the truncate function.
Sinceβ
2.0.0
Propertiesβ
length?: numberβ
Maximum string length (default: 30).
omission?: stringβ
The string to indicate text is omitted (default: '...').
separator?: string | RegExpβ
The separator pattern to truncate to.