expandColumns static method

List<Column> expandColumns(
  1. List<String> values
)

Expands an list of columns defined as per Column.parse into a list of Columns.

Implementation

static List<Column> expandColumns(List<String> values) {
  final columns = <Column>[];

  for (final value in values) {
    final parts = value.split('-');

    if (parts.length == 1) {
      columns.add(Column.parse(parts[0]));
    } else if (parts.length == 2) {
      // We have been passed a column range 1-4
      // The type and sort direction MUST ONLY be present on the end ordinal
      // e.g. 1-4Sa

      final end = Column.parse(parts[1]);

      final comparator = end._comparator;
      final sortDirection = end._sortDirection;

      final start = Column.parse(parts[0], ordinalOnly: true)
        .._comparator = comparator
        .._sortDirection = sortDirection;

      int index;
      if (end.ordinal! > start.ordinal!) {
        index = 1;
      } else {
        index = -1;
      }
      columns.add(start);

      for (var i = start.ordinal! + index; i != end.ordinal; i += index) {
        final column = Column(i, comparator, sortDirection);
        columns.add(column);
      }

      columns.add(end);
    } else {
      throw InvalidArgumentException('The column format is invalid: $value');
    }
  }

  return columns;
}