truncateWithEllipsis method

String truncateWithEllipsis(
  1. int? cutoff
)

Truncates the string to cutoff graphemes and appends an ellipsis '…'.

Uses grapheme clusters for proper Unicode support, including emojis. Returns the original string if it's shorter than cutoff.

Implementation

String truncateWithEllipsis(int? cutoff) {
  // Return original string if it's empty or cutoff is invalid
  if (isEmpty || cutoff == null || cutoff <= 0) {
    return this;
  }

  // Return original string if it's already shorter than the cutoff
  // Use grapheme length for proper emoji support
  if (characters.length <= cutoff) {
    return this;
  }

  // Simple truncation if not keeping words intact
  return '${substringSafe(0, cutoff)}$ellipsis';
}