extractnimc static method

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

Implementation

static Future<IDCardInfo> extractnimc(List<String> lines) async {
  var idNumber;
  var firstName;
  var lastName;
  var middleName;
  var dob;
  var fullName;

  // NIMC card number extraction with duplicate group handling
  List<String> digitLines =
  lines
      .where((l) => RegExp(r'^[0-9 ]{6,}$').hasMatch(l.trim()))
      .toList();
  if (digitLines.length >= 2) {
    var firstLine = digitLines[0].replaceAll(' ', '');
    var secondLine = digitLines[1].replaceAll(' ', '');
    var firstGroups = digitLines[0].trim().split(' ');
    var lastGroup = firstGroups.isNotEmpty ? firstGroups.last : '';
    if (lastGroup.isNotEmpty &&
        digitLines[1].trim().startsWith(lastGroup)) {
      // Remove the duplicate group from the start of second line
      var dedupedSecond =
      digitLines[1].trim().substring(lastGroup.length).trim();
      idNumber = (digitLines[0] + ' ' + dedupedSecond).replaceAll(' ', '');
    } else {
      idNumber = (digitLines[0] + digitLines[1]).replaceAll(' ', '');
    }
  }

  // Name extraction for NIMC: always use the value after the label
  String? tempSurname, tempFirstName, tempMiddleName;
  for (int i = 0; i < lines.length; i++) {
    final upper = lines[i].toUpperCase().trim();
    if (upper == 'SURNAME' && i + 1 < lines.length) {
      tempSurname = lines[i + 1].trim();
    } else if (upper == 'FIRST NAME' && i + 1 < lines.length) {
      tempFirstName = lines[i + 1].trim();
    } else if (upper == 'MIDDLE NAME' && i + 1 < lines.length) {
      tempMiddleName = lines[i + 1].trim();
    }
  }
  if (tempSurname != null) lastName = tempSurname;
  if (tempFirstName != null) firstName = tempFirstName;
  if (tempMiddleName != null) middleName = tempMiddleName;

  // DOB
  final dobRegex = RegExp(
    r'(\d{2})[ /-]([A-Z]{3})[ /-](\d{2,4})|(\d{2})[ /-](\d{2})[ /-](\d{2,4})',
  );
  for (final line in lines) {
    final match = dobRegex.firstMatch(line.toUpperCase());
    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)}';
      }
      break;
    }
  }
  return IDCardInfo(
    firstName: firstName,
    lastName: lastName,
    middleName: middleName,
    dateOfBirth: dob,
    idNumber: idNumber,
  );
}