Created
March 31, 2024 01:29
-
-
Save tangentlin/59adfa8fa7cb83ccefbc83d9638a005f 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
function validateAndFormatCard(number: string, type: string) { | |
const cardType = creditCardTypes[type]; | |
if (!cardType) { | |
throw new Error('Unsupported card type'); | |
} | |
const isValid = cardType.validate(number); | |
const formattedNumber = cardType.format(number); | |
return { | |
isValid, | |
formattedNumber, | |
cardName: cardType.name, | |
logoUri: cardType.logoUri | |
}; | |
} | |
// Example usage | |
const cardInfo = validateAndFormatCard("4111111111111111", "visa"); | |
console.log(cardInfo); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment