convertToMercosulPlate static method

String convertToMercosulPlate(
  1. String? oldPlate
)

Converts an old plate String to the Mercosul standard.

The old card must be in "AAA9999" format.

Returns the new plate in the Mercosur pattern.

Implementation

static String convertToMercosulPlate(String? oldPlate) {
  if (oldPlate == null) {
    throw ArgumentError.notNull('oldPlate');
  }

  String cleanPlate = oldPlate.replaceAll(RegExp(r'[^a-zA-Z0-9]'), '');

  RegExp oldPlateRegex = RegExp(r'^([A-Z]{3})(\d{4})$');
  if (!oldPlateRegex.hasMatch(cleanPlate)) {
    throw const FormatException('Invalid old plate format.');
  }

  String newPlate = cleanPlate.replaceAllMapped(oldPlateRegex, (match) => '${match[1]}${match[2]![0]}${match[1]![2]}${match[2]!.substring(1)}');

  return newPlate.toUpperCase();
}