Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save tangentlin/59adfa8fa7cb83ccefbc83d9638a005f to your computer and use it in GitHub Desktop.
Save tangentlin/59adfa8fa7cb83ccefbc83d9638a005f to your computer and use it in GitHub Desktop.
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