extractNINslip static method

Future<IDCardInfo> extractNINslip(
  1. List<String> lines
)

Implementation

static Future<IDCardInfo> extractNINslip(List<String> lines) async {
  var idNumber;
  var firstName;
  var lastName;
  var middleName;
  var dob;
  var fullName;
  String? tempSurname, tempFirstName, tempMiddleName, tempDob, tempIdNumber;

  // DOB
  final dobRegex = RegExp(
    r'(\d{2})[ /-]([A-Z]{3})[ /-](\d{2,4})|(\d{2})[ /-](\d{2})[ /-](\d{2,4})',
  );
  for (int i = 0; i < lines.length; i++) {
    final upper = lines[i].toUpperCase().trim();

    final match = dobRegex.firstMatch(upper);

    // Surname
    if ((upper.contains('SURNAME') && !upper.contains('GIVEN')) ||
        upper.contains('SURNAME / NOM')) {
      if (i + 1 < lines.length) tempSurname = lines[i + 3].trim();
    }
    // Given Names
    else if (upper.contains('GIVEN NAMES') ||
        upper.contains('GIVEN NAMES / PRÉNOMS') ||
        upper.contains('PRÉNOMS') ||
        upper.contains('Glven Names/ Prénoms')) {
      if (i + 1 < lines.length) {
        var names = lines[i + 1].trim().split(RegExp(r'\s+'));
        if (names.isNotEmpty) tempFirstName = names[0];
        if (names.length > 1) tempMiddleName = names.sublist(1).join(' ');
      }
    }
    // Date of Birth
    else if (match != null) {
      if (match.group(2) != null) {
        final day = match.group(1);
        final monStr = match.group(2);
        final year = match.group(3);
        final months = {
          'JAN': '01',
          'FEB': '02',
          'MAR': '03',
          'APR': '04',
          'MAY': '05',
          'JUN': '06',
          'JUL': '07',
          'AUG': '08',
          'SEP': '09',
          'OCT': '10',
          'NOV': '11',
          'DEC': '12',
        };
        final mon = months[monStr] ?? monStr;
        dob = '$day-$mon-$year';
      } else if (match.group(4) != null &&
          match.group(5) != null &&
          match.group(6) != null) {
        dob = '${match.group(4)}-${match.group(5)}-${match.group(6)}';
      }
    }
    // Passport No.
    else if (upper.contains('NGA')) {
      // dev.log('PASSPORT NO');
      // dev.log(lines[i]);
      // dev.log(lines[i + 1]);
      if (i + 1 < lines.length) tempIdNumber = lines[i + 1].trim();
    }
  }

  if (tempSurname != null) lastName = tempSurname;
  if (tempFirstName != null) firstName = tempFirstName;
  if (tempMiddleName != null) middleName = tempMiddleName;
  if (tempIdNumber != null) idNumber = tempIdNumber;
  return IDCardInfo(
    firstName: firstName,
    lastName: lastName,
    middleName: middleName,
    dateOfBirth: dob,
    idNumber: idNumber,
  );
}