convertToThreeLettersPlate static method

String convertToThreeLettersPlate(
  1. String? newPlate
)

Converts a String from Mercosul license plate to the old standard.

The Mercosul license plate must be in the "AAA1A11" format.

Returns the new plate in the old pattern.

Implementation

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

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

  RegExp newPlateRegex = RegExp(r'^([A-Z]{3})(\d)([A-Z]{1})(\d{2})$');
  if (!newPlateRegex.hasMatch(cleanPlate)) {
    throw const FormatException('Invalid mercosul format.');
  }

  String oldPlate = cleanPlate.replaceAllMapped(newPlateRegex, (match) => '${match[1]}${match[3]}${match[4]}');

  return oldPlate.toUpperCase();
}