truncate static method

String truncate(
  1. String text,
  2. int maxLength
)

Truncate text with ellipsis and bounds checking

Throws AssertionError if maxLength is negative. Returns empty string if maxLength is 0.

Example:

FSUtils.truncate('Very long text here', 10); // 'Very long...'

Implementation

static String truncate(String text, int maxLength) {
  assert(maxLength >= 0, 'maxLength must be non-negative');
  if (maxLength == 0) return '';
  if (text.length <= maxLength) return text;
  return '${text.substring(0, maxLength)}...';
}