Last active
December 29, 2022 18:54
-
-
Save yarn-rp/8c407dbddbebfd90120af16312a08654 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum OtherCardType { bandec, bpa, metropolitan, unknown } | |
// First Group | |
const _cubanStart = r'^92'; | |
const _productName = r'[0-9]{2}'; | |
const _firstGroup = _cubanStart + _productName; | |
// Second Group | |
const _bandecSecondGroup = r'0699'; | |
const _metropolitanSecondGroup = r'9598'; | |
const _bpaSecondGroup = r'1299'; | |
const bandecCard = _firstGroup + _bandecSecondGroup; | |
const metropolitanCard = _firstGroup + _metropolitanSecondGroup; | |
const bpaCard = _firstGroup + _bpaSecondGroup; | |
const cubanCardRest = r'[0-9]{8}'; | |
final cubanCardRegex = | |
RegExp('($bandecCard|$metropolitanCard|$bpaCard)$cubanCardRest'); | |
final bandecRegex = RegExp(bandecCard); | |
final metropolitanRegex = RegExp(metropolitanCard); | |
final bpaRegex = RegExp(bpaCard); | |
final cardPatterns = <RegExp, OtherCardType>{ | |
bandecRegex: OtherCardType.bandec, | |
metropolitanRegex: OtherCardType.metropolitan, | |
bpaRegex: OtherCardType.bpa, | |
}; | |
/// This function determines the Credit Card type based on the cardPatterns | |
/// and returns it. | |
OtherCardType detectCCOtherType(String cardNumber) { | |
final ccPatternStr = cardNumber.replaceAll(RegExp(r'\\D|\s+\b|\b\s'), ''); | |
/// We set default type to unknown | |
const defaultType = OtherCardType.unknown; | |
return cardPatterns.entries.fold(defaultType, (previousValue, entry) { | |
final pattern = entry.key; | |
if (pattern.hasMatch(ccPatternStr)) { | |
// Found a match | |
return entry.value; | |
} | |
return previousValue; | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment