initials property

String initials

Returns initials from a name.

Implementation

String get initials {
  if (this == null || (this?.isEmpty ?? true)) return '';
  final List<String> nameParts = this!.split(RegExp(r'\W+'))..removeWhere((item) => item.isEmpty);
  if (nameParts.isEmpty) return '';
  return (nameParts.length > 1
          ? nameParts[0].substring(0, 1) + nameParts[nameParts.length - 1].substring(0, 1)
          : nameParts[0].length > 1
              ? nameParts[0].substring(0, 2)
              : nameParts[0])
      .toUpperCase();
}