truncateWithEllipsis method

String truncateWithEllipsis(
  1. int? cutoff
)

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

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
  if (length <= cutoff) {
    return this;
  }

  // Simple truncation if not keeping words intact
  return '${substring(0, cutoff)}$ellipsis';
  // maxLength <= 0 || length <= maxLength ? this : '${substring(0, maxLength)}…';
}