formatUIC method

String formatUIC(
  1. String uic, {
  2. UICType? type = UICType.twelveDigits,
  3. UICCategory? category = UICCategory.passengerCoach,
})

Formats uic to match the common format of type (defaults to UICType.twelveDigits) and category (defaults to UICCategory.passengerCoach, ignored with 7-digit uic).

Implementation

String formatUIC(
  String uic, {
  UICType? type = UICType.twelveDigits,
  UICCategory? category = UICCategory.passengerCoach,
}) {
  final onlyDigits = uic.replaceAll(_everythingExceptDigits, '');

  if (type == null && category == null) return onlyDigits;

  // 000 000-0
  if (type == UICType.sevenDigits) {
    return onlyDigits.substring(0, 3) + ' ' + onlyDigits.substring(3, 6) + '-' + onlyDigits[6];
  }

  switch (category!) {
    // 00 00 0 000 000-0
    case UICCategory.tractionUnit:
      return onlyDigits.substring(0, 2) +
          ' ' +
          onlyDigits.substring(2, 4) +
          ' ' +
          onlyDigits[4] +
          ' ' +
          onlyDigits.substring(5, 8) +
          ' ' +
          onlyDigits.substring(8, 11) +
          '-' +
          onlyDigits[11];

    // 00 00 00-00 000-0
    case UICCategory.passengerCoach:
      return onlyDigits.substring(0, 2) +
          ' ' +
          onlyDigits.substring(2, 4) +
          ' ' +
          onlyDigits.substring(4, 6) +
          '-' +
          onlyDigits.substring(6, 8) +
          ' ' +
          onlyDigits.substring(8, 11) +
          '-' +
          onlyDigits[11];

    // 00 00 000 0 000-0
    case UICCategory.freightWagon:
      return onlyDigits.substring(0, 2) +
          ' ' +
          onlyDigits.substring(2, 4) +
          ' ' +
          onlyDigits.substring(4, 7) +
          ' ' +
          onlyDigits[7] +
          ' ' +
          onlyDigits.substring(8, 11) +
          '-' +
          onlyDigits[11];
  }
}