formatListWithAnd function
Format a list of items with proper "and" conjunction.
Implementation
String formatListWithAnd(List<String> items, {int limit = 0}) {
if (items.isEmpty) return '';
final effectiveLimit = limit == 0 ? null : limit;
// No limit or within limit: normal formatting
if (effectiveLimit == null || items.length <= effectiveLimit) {
if (items.length == 1) return items[0];
if (items.length == 2) return '${items[0]} and ${items[1]}';
final lastItem = items.last;
final allButLast = items.sublist(0, items.length - 1);
return '${allButLast.join(', ')}, and $lastItem';
}
// More items than limit: show first few + count
final shown = items.sublist(0, effectiveLimit);
final remaining = items.length - effectiveLimit;
if (shown.length == 1) {
return '${shown[0]} and $remaining more';
}
return '${shown.join(', ')}, and $remaining more';
}