The string to truncate.
The maximum length of the string, including the ellipsis.
Optionaloptions: { ellipsis?: string; preserveWords?: boolean }Optional configuration for truncation.
Optionalellipsis?: stringThe string to append to indicate truncation. Default is '...'.
OptionalpreserveWords?: booleanWhether to preserve whole words when truncating. When true, truncation
occurs at the last space within the allowed range. If no space exists in that range, it falls back to hard truncation
at maxLength to guarantee the output never exceeds maxLength. Default is false.
The truncated string with an ellipsis if necessary, or the original string if it's within the length limit.
truncate('This is a long string that should be truncated', 20); // "This is a long str..."
truncate('This is a long string that should be truncated', 20, { preserveWords: true }); // "This is a long..."
truncate('Supercalifragilistic', 10, { preserveWords: true }); // "Supercali..." (no space — hard truncation)
truncate('Short text', 20); // "Short text"
truncate('Short text', 6); // "Sho..."
Truncates a string to a specified maximum length, optionally adding an ellipsis and preserving whole words when truncating.