initials property

String get initials

Extracts initials from words in this string (e.g. "John Doe" -> "JD").

Implementation

String get initials {
  if (isBlank) return '';
  final words = trim().split(RegExp(r'\s+'));
  if (words.isEmpty) return '';
  if (words.length == 1) {
    return words.first.substring(0, words.first.length.clamp(0, 2)).toUpperCase();
  }
  return '${words.first[0]}${words.last[0]}'.toUpperCase();
}