From 5fa41082cd34669b92ed1e2bc84af2daf96089f4 Mon Sep 17 00:00:00 2001 From: Panagiotis Papadopoulos Date: Tue, 6 Dec 2022 10:31:18 +0100 Subject: [PATCH 1/2] 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. --- src/lib/isCreditCard.js | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/lib/isCreditCard.js b/src/lib/isCreditCard.js index 0a874c9cf..d0c5ae1a6 100644 --- a/src/lib/isCreditCard.js +++ b/src/lib/isCreditCard.js @@ -10,9 +10,16 @@ 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) { + if (cards.hasOwnProperty(cardProvider)) { + tmpCardsArray.push(cards[cardProvider]); + } + } + return tmpCardsArray; +})(); export default function isCreditCard(card, options = {}) { assertString(card); @@ -26,7 +33,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; } From 1ecdd21d5c9d35b7b27e5b9db1423c5c4925dbad Mon Sep 17 00:00:00 2001 From: Panagiotis Papadopoulos Date: Tue, 6 Dec 2022 11:42:29 +0100 Subject: [PATCH 2/2] chore: code coverage improvement add "istanbull ignore else", similarly to how it is was done in: 9ee09a7c6227dba4d8953d8de2346090a742eeaa --- src/lib/isCreditCard.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/lib/isCreditCard.js b/src/lib/isCreditCard.js index d0c5ae1a6..6a9080c6d 100644 --- a/src/lib/isCreditCard.js +++ b/src/lib/isCreditCard.js @@ -14,6 +14,7 @@ const cards = { const allCards = (() => { const tmpCardsArray = []; for (const cardProvider in cards) { + // istanbul ignore else if (cards.hasOwnProperty(cardProvider)) { tmpCardsArray.push(cards[cardProvider]); }