maskValidThru function

String maskValidThru({
  1. required String validThru,
  2. required FMMaskType maskType,
})

Mask Valid Thru

Type: String

Use in the masking process of valid thru. It requires the card valid thru and mask type and uses these two params to mask the card valid thru.

Implementation

String maskValidThru({
  required String validThru,
  required FMMaskType maskType,
}) {
  // getting mask type condition added by the developer
  bool full = maskType == FMMaskType.full;
  bool none = maskType == FMMaskType.none;
  String mask = "0000";
  var bufferString = StringBuffer();

  // return the valid thru input without validation to avoid length error
  if (validThru.length != 5) return validThru;

  // remove the "/" from valid thru passed buffer
  validThru = validThru.replaceAll("/", "");

  if (full) {
    mask = "*" * validThru.length;
  } else if (none) {
    mask = validThru;
  }

  // this is to buff the valid thru using / after first 2 characters
  for (int i = 0; i < mask.length; i++) {
    bufferString.write(mask[i]);
    var nonZeroIndexValue = i + 1;
    if (nonZeroIndexValue % 2 == 0 && nonZeroIndexValue != mask.length) {
      bufferString.write("/");
    }
  }

  // finally return the buffed valid thru
  return bufferString.toString();
}