analyzeNTN function

NTNInfo? analyzeNTN(
  1. String ntn
)

Analyzes a Pakistani National Tax Number (NTN): a 13-digit CNIC for individuals, or 7 digits (optionally followed by a check digit) for businesses.

FBR does not publish a check-digit algorithm, so this checks the format only. Returns null if the format is invalid.

Implementation

NTNInfo? analyzeNTN(String ntn) {
  final trimmed = toAsciiDigits(jsTrim(ntn));
  if (validateCNIC(trimmed)) {
    final digits = stripSeparators(trimmed);
    return NTNInfo(
      type: NTNType.individual,
      digits: digits,
      formatted:
          '${digits.substring(0, 5)}-${digits.substring(5, 12)}-${digits[12]}',
    );
  }
  final match = _businessNtn.firstMatch(trimmed);
  if (match == null) return null;
  final base = match[1]!;
  final check = match[2];
  return NTNInfo(
    type: NTNType.business,
    digits: '$base${check ?? ''}',
    formatted: check == null ? base : '$base-$check',
  );
}