calculateCheckDigit function

String calculateCheckDigit(
  1. String input
)

Implementation

String calculateCheckDigit(String input) {
  const weights = [7, 3, 1];
  int sum = 0;

  String cleanInput = input
      .replaceAll('«', '<')
      .replaceAll('»', '<')
      .replaceAll('‹', '<')
      .replaceAll('›', '<')
      .replaceAll('〈', '<')
      .replaceAll('〉', '<')
      .replaceAll('<', '<')
      .replaceAll('>', '<');

  for (int i = 0; i < cleanInput.length; i++) {
    final char = cleanInput[i];
    int value;

    if (char == '<') {
      value = 0;
    } else if (RegExp(r'\d').hasMatch(char)) {
      value = int.parse(char);
    } else {
      value = char.codeUnitAt(0) - 'A'.codeUnitAt(0) + 10;
    }

    sum += value * weights[i % 3];
  }

  return (sum % 10).toString();
}