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. Default is false.
The truncated string with an ellipsis if necessary, or the original string if it's within the length limit.
truncateString('This is a long string that should be truncated', 20); // "This is a long..."
truncateString('This is a long string that should be truncated', 20, { preserveWords: true }); // "This is a long..."
truncateString('Short text', 20); // "Short text"
truncateString('Short text', 5); // "Short..."
Truncates a string to a specified maximum length, optionally adding an ellipsis and preserving whole words when truncating.