defaultList static method

String defaultList(
  1. List<String> items, {
  2. required String two,
  3. String start = ', ',
  4. String middle = ', ',
  5. required String end,
})

Generates a formatted list from items similar to the Unicode-Proposal.

Implementation

static String defaultList(
  List<String> items, {
  required String two,
  String start = ', ',
  String middle = ', ',
  required String end,
}) {
  switch (items.length) {
    case 0:
      throw ArgumentError('items must not be empty.');
    case 1:
      return items.first;
    case 2:
      return '${items.first}$two${items[1]}';
    default:
      final output = StringBuffer(items.first);
      output
        ..write(start)
        ..write(items[1]);

      for (final entry in items.sublist(2, items.length - 1)) {
        output
          ..write(middle)
          ..write(entry);
      }

      output
        ..write(end)
        ..write(items.last);
      return output.toString();
  }
}