maskCardNumber function
Mask Card Number
Type: String
Use in the masking process of card number. It requires the card number and mask type and uses these two params to mask the card number.
Implementation
String maskCardNumber({required String number, required FMMaskType maskType}) {
// getting mask type condition added by the developer
bool full = maskType == FMMaskType.full;
bool first4Last4 = maskType == FMMaskType.first4Last4;
bool first6last2 = maskType == FMMaskType.first6last2;
bool first2last6 = maskType == FMMaskType.first2last6;
bool none = maskType == FMMaskType.none;
String mask = "0000 0000 0000 0000";
var bufferString = StringBuffer();
// return the number input without validation to avoid length error
if (number.length < 8) return number;
// remove the "/" from number passed buffer
number = number.replaceAll(" ", "");
// card number length less than two are not validated to avoid [Out Of Range].
if (number.length > 2) {
if (full) {
mask = "*" * number.length;
} else if (first4Last4) {
String first4 = number.substring(0, 4);
String last4 = number.substring(number.length - 4);
String maskCount = "*" * (number.length - 8);
mask = "$first4$maskCount$last4";
} else if (first6last2) {
String first6 = number.substring(0, 6);
String last2 = number.substring(number.length - 2);
String maskCount = "*" * (number.length - 8);
mask = "$first6$maskCount$last2";
} else if (first2last6) {
String first2 = number.substring(0, 2);
String last6 = number.substring(number.length - 6);
String maskCount = "*" * (number.length - 8);
mask = "$first2$maskCount$last6";
} else if (none) {
mask = number;
}
}
// this is to buff the number using space after every 4 characters
for (int i = 0; i < mask.length; i++) {
bufferString.write(mask[i]);
var nonZeroIndexValue = i + 1;
if (nonZeroIndexValue % 4 == 0 && nonZeroIndexValue != mask.length) {
bufferString.write(" ");
}
}
// finally return the buffed number
return bufferString.toString();
}