extractNIN static method

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

Implementation

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

  // NIN SLIP LOGIC
  // ID Number
  for (final line in lines) {
    final text = line.toUpperCase();
    if (text.startsWith('NIN')) {
      final parts = line.split(':');
      String candidate =
      (parts.length > 1) ? parts[1].replaceAll(RegExp(r'\D'), '') : '';
      if (candidate.isEmpty) {
        final nextIdx = lines.indexOf(line) + 1;
        if (nextIdx < lines.length) {
          candidate = lines[nextIdx].replaceAll(RegExp(r'\D'), '').trim();
        }
      }
      if (candidate.isNotEmpty) {
        idNumber = candidate;
        break;
      }
    }
  }
  if (idNumber == null) {
    for (int i = 0; i < lines.length - 1; i++) {
      final line1Raw = lines[i];
      var line2Raw = lines[i + 1];
      if (line2Raw.length > 4) {
        line2Raw = line2Raw.substring(4);
      } else {
        line2Raw = '';
      }
      if (RegExp(r'^[0-9 ]+$').hasMatch(line1Raw) &&
          RegExp(r'^[0-9 ]+$').hasMatch(line2Raw)) {
        final combined = (line1Raw + line2Raw).replaceAll(' ', '');
        if (combined.length >= 16) {
          idNumber = combined;
          break;
        }
      }
    }
    String digitsOnly = lines.join(' ').replaceAll(RegExp(r'[^0-9]'), ' ');
    final idCandidates =
    digitsOnly.split(' ').where((s) => s.length >= 10).toList();
    idCandidates.sort((a, b) => b.length.compareTo(a.length));
    if (idCandidates.isNotEmpty) {
      idNumber = idCandidates.first;
    }
  }
  // Name
  for (final line in lines) {
    final upper = line.toUpperCase();
    if (upper.startsWith('SURNAME')) {
      lastName = line.split(' ').last.trim();
    } else if (upper.startsWith('FIRST NAME')) {
      firstName = line.split(' ').last.trim();
    } else if (upper.startsWith('MIDDLE NAME')) {
      middleName = line.split(' ').last.trim();
    }
  }
  // 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,
  );
}