detectCCType function
This function determines the CC type based on the cardPatterns
Implementation
CreditCardType detectCCType(String ccNumStr) {
CreditCardType cardType = CreditCardType.unknown;
if (ccNumStr.isEmpty) {
return cardType;
}
//TODO error checking for strings with non-numerical chars
cardNumPatterns.forEach(
(CreditCardType type, Set<List<String>> patterns) {
for (List<String> patternRange in patterns) {
// Remove any spaces
String ccPatternStr = ccNumStr.replaceAll(RegExp(r'\s+\b|\b\s'), '');
int rangeLen = patternRange[0].length;
// Trim the CC number str to match the pattern prefix length
if (rangeLen < ccNumStr.length) {
ccPatternStr = ccPatternStr.substring(0, rangeLen);
}
if (patternRange.length > 1) {
// Convert the prefix range into numbers then make sure the
// CC num is in the pattern range.
// Because Strings don't have '>=' type operators
int ccPrefixAsInt = int.parse(ccPatternStr);
int startPatternPrefixAsInt = int.parse(patternRange[0]);
int endPatternPrefixAsInt = int.parse(patternRange[1]);
if (ccPrefixAsInt >= startPatternPrefixAsInt &&
ccPrefixAsInt <= endPatternPrefixAsInt) {
// Found a match
cardType = type;
break;
}
} else {
// Just compare the single pattern prefix with the CC prefix
if (ccPatternStr == patternRange[0]) {
// Found a match
cardType = type;
break;
}
}
}
},
);
var listCCTypeBand = cardType.toString().replaceAll('CreditCardType.', '');
if (listBand.contains(listCCTypeBand)) {
cardType = cardType;
} else {
cardType = CreditCardType.unknown;
}
return cardType;
}