Skip to content

Commit

Permalink
refactor(isCreditCard): create allCards dynamically (#2117)
Browse files Browse the repository at this point in the history
* refactor(isCreditCard): create allCards dynamically

get rid of the hardcoded allCards variable, which was a manual copy of the existing regExp for card provider.
Replace it with a dynamically created array instead, which will make it easier to maintain, when new providers are added.

* chore: code coverage improvement

add "istanbull ignore else", similarly to how it is was done in:
9ee09a7
  • Loading branch information
pano9000 committed Jun 26, 2023
1 parent 3507d27 commit 4c25f26
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/lib/isCreditCard.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,17 @@ const cards = {
unionpay: /^(6[27][0-9]{14}|^(81[0-9]{14,17}))$/,
visa: /^(?:4[0-9]{12})(?:[0-9]{3,6})?$/,
};
/* eslint-disable max-len */
const allCards = /^(?:4[0-9]{12}(?:[0-9]{3,6})?|5[1-5][0-9]{14}|(222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12}|6(?:011|5[0-9][0-9])[0-9]{12,15}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11}|6[27][0-9]{14}|^(81[0-9]{14,17}))$/;
/* eslint-enable max-len */

const allCards = (() => {
const tmpCardsArray = [];
for (const cardProvider in cards) {
// istanbul ignore else
if (cards.hasOwnProperty(cardProvider)) {
tmpCardsArray.push(cards[cardProvider]);
}
}
return tmpCardsArray;
})();

export default function isCreditCard(card, options = {}) {
assertString(card);
Expand All @@ -26,7 +34,7 @@ export default function isCreditCard(card, options = {}) {
} else if (provider && !(provider.toLowerCase() in cards)) {
/* specific provider not in the list */
throw new Error(`${provider} is not a valid credit card provider.`);
} else if (!(allCards.test(sanitized))) {
} else if (!allCards.some(cardProvider => cardProvider.test(sanitized))) {
// no specific provider
return false;
}
Expand Down

0 comments on commit 4c25f26

Please sign in to comment.