Skip to content

Instantly share code, notes, and snippets.

@faustobdls
Last active July 13, 2023 17:04
Show Gist options
  • Save faustobdls/3d98d42355ab423ddd2dec0c887f0c5a to your computer and use it in GitHub Desktop.
Save faustobdls/3d98d42355ab423ddd2dec0c887f0c5a to your computer and use it in GitHub Desktop.
joyful-spray-8340
void main() {
print("_initialsFormatter ${_initialsFormatter('ana')}");
print("_initialsFormatter ${_initialsFormatter('Nonato De Sousa ')}");
print("_initialsFormatter ${_initialsFormatter('ana beatriz')}");
print("_initialsFormatter ${_initialsFormatter('ana beatriz')}");
print("_initialsFormatter ${_initialsFormatter('ana beatriz')}");
print("_initialsFormatter ${_initialsFormatter('Tabs Tabs Tabs Brenda Tabs')}");
print("_initialsFormatter ${_initialsFormatter('Débora beatriz Laís Taissa')}");
print("_initialsFormatter ${_initialsFormatter('ana beatriz')}");
print("_initialsFormatter ${_initialsFormatter('ana beatriz')}");
print("_initialsFormatter ${_initialsFormatter('ana beatriz')}");
print("_initialsFormatter ${_initialsFormatter('ana beatriz')}");
print("faustoInitialsFormatter ${faustoInitialsFormatter('ana')}");
print("faustoInitialsFormatter ${faustoInitialsFormatter('Nonato De Sousa ')}");
print("faustoInitialsFormatter ${faustoInitialsFormatter('ana beatriz')}");
print("faustoInitialsFormatter ${faustoInitialsFormatter('ana beatriz')}");
print("faustoInitialsFormatter ${faustoInitialsFormatter('ana beatriz')}");
print("faustoInitialsFormatter ${faustoInitialsFormatter('Tabs Tabs Tabs Brenda Tabs')}");
print("faustoInitialsFormatter ${faustoInitialsFormatter('Tabs Tabs Tabs Brenda Tabs Brenda')}");
print("faustoInitialsFormatter ${faustoInitialsFormatter('Débora beatriz Laís Taissa')}");
print("faustoInitialsFormatter ${faustoInitialsFormatter('ana beatriz')}");
print("faustoInitialsFormatter ${faustoInitialsFormatter('ana beatriz')}");
print("faustoInitialsFormatter ${faustoInitialsFormatter('ana beatriz')}");
print("faustoInitialsFormatter ${faustoInitialsFormatter('ana beatriz')}");
print("faustoInitialsFormatter ${faustoInitialsFormatter('ana paula arruda assunção')}");
print("faustoInitialsFormatter ${faustoInitialsFormatter(' ')}");
print("faustoInitialsFormatter ${faustoInitialsFormatter('')}");
}
String _initialsFormatter(String name) {
if(name.trim().isEmpty) return "";
if (name.isNotEmpty) {
final List<String> names = name.trim().split(' ');
String initials = '';
for (String name in names) {
final bool first = names.indexOf(name) == 0;
final bool last = names.indexOf(name) == names.length - 1;
if (first || last) {
initials += name.substring(0, 1).toUpperCase();
}
}
if (initials.length == 1) {
return initials;
} else {
return initials.substring(0, 2);
}
} else {
return '';
}
}
String faustoInitialsFormatter(String name) {
if (name.trim().isEmpty) {
return '';
}
final List<String> initials = name
.split(' ').where((String element) => element.isNotEmpty)
.map((String e) => e.substring(0, 1).toUpperCase())
.toList();
final List<String> finalInitials = initials
.where((String element) =>
initials.first == element || initials.last == element)
.toList();
return '${finalInitials.first}${finalInitials.length > 1 ? finalInitials.last : ''}';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment