format method

String? format(
  1. String? dlNumber, {
  2. bool withHyphen = true,
})

Driving license format is HR-0619850034761 or HR06 19850034761.

by default it will return with hyphen.

Implementation

String? format(String? dlNumber, {bool withHyphen = true}) {
  if (dlNumber == null) return null;

  // remove the space and hyphen (-) from [dlNumber]
  // because the driving license number can have these
  dlNumber = dlNumber.replaceAll(RegExp(r"-|\s+"), "");

  if (withHyphen) {
    if (dlNumber.length > 2) {
      return (dlNumber.substring(0, 2) + '-' + dlNumber.substring(2))
          .toUpperCase();
    }

    return dlNumber.toUpperCase();
  } else {
    if (dlNumber.length > 4) {
      return (dlNumber.substring(0, 4) + ' ' + dlNumber.substring(4))
          .toUpperCase();
    } else {
      return dlNumber.toUpperCase();
    }
  }
}