formatID method

String? formatID({
  1. IdFormatType formatType = IdFormatType.proper,
})

formatID

Returns string in the requested format. @throws LocalRegexException if the national id is invalid.

Implementation

String? formatID({IdFormatType formatType = IdFormatType.proper}) {
  late String id;
  if (LocalRegex.isZimID(this)) {
    final idWithNoSpaces = clean.replaceAll(RegExp('[^A-Z0-9]'), '');
    final idPrefix = idWithNoSpaces.substring(0, 2);
    final idSuffix = idWithNoSpaces.lastCharacters(3);
    final properIdSuffix = "${idSuffix.split("")[0]} ${idSuffix.split("")[1]}"
        "${idSuffix.split("")[2]}";
    final idBody = idWithNoSpaces
        .replaceFirst(RegExp('[0-9]{2}'), '')
        .replaceFirst(RegExp('[A-Za-z][0-9]{2}'), '')
        .trim();

    switch (formatType) {
      case IdFormatType.proper:
        id = '$idPrefix-$idBody $properIdSuffix';
        break;
      case IdFormatType.noSpace:
        id = '$idPrefix$idBody$properIdSuffix'.clean;
        break;
    }
  } else {
    throw LocalRegexException('National ID is not valid');
  }

  return id.toUpperCase();
}