Skip to content

Commit 4c25f26

Browse files
authoredJun 26, 2023
refactor(isCreditCard): create allCards dynamically (#2117)
* 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
1 parent 3507d27 commit 4c25f26

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed
 

‎src/lib/isCreditCard.js

+12-4
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,17 @@ const cards = {
1010
unionpay: /^(6[27][0-9]{14}|^(81[0-9]{14,17}))$/,
1111
visa: /^(?:4[0-9]{12})(?:[0-9]{3,6})?$/,
1212
};
13-
/* eslint-disable max-len */
14-
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}))$/;
15-
/* eslint-enable max-len */
13+
14+
const allCards = (() => {
15+
const tmpCardsArray = [];
16+
for (const cardProvider in cards) {
17+
// istanbul ignore else
18+
if (cards.hasOwnProperty(cardProvider)) {
19+
tmpCardsArray.push(cards[cardProvider]);
20+
}
21+
}
22+
return tmpCardsArray;
23+
})();
1624

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

0 commit comments

Comments
 (0)
Please sign in to comment.