refactor(isCreditCard): create allCards dynamically and get rid of hard-to-maintain hardcoded version#2117
Conversation
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.
Codecov ReportBase: 100.00% // Head: 100.00% // No change to project coverage 👍
Additional details and impacted files@@ Coverage Diff @@
## master #2117 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 104 104
Lines 2308 2313 +5
Branches 578 579 +1
=========================================
+ Hits 2308 2313 +5
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. ☔ View full report at Codecov. |
add "istanbull ignore else", similarly to how it is was done in: 9ee09a7
| const tmpCardsArray = []; | ||
| for (const cardProvider in cards) { | ||
| // istanbul ignore else | ||
| if (cards.hasOwnProperty(cardProvider)) { |
There was a problem hiding this comment.
Why is this if-statement needed?
There was a problem hiding this comment.
I originally did not have it in there either, but there is an linting rule that forced me to add it :-)
It otherwise threw me guard-for-in error
https://eslint.org/docs/latest/rules/guard-for-in
|
Interesting that we are doing this... i would have to look back, but i think that i had a similar solution to this in the first refactor I did. Glad it is coming back, makes maintenance much easier. Any card provider can be added very easily now. |
Hello,
I've refactored the way the
allCardsvariable is created/handled in isCreditCard:It currently is manually hardcoding all of the previously already defined RegExp into one huge RegExp, that it later then checks against.
This is problematic, because:
allCardsI've replace it with a dynamically created array with the RegExps from the
cardsobject instead, which will make it a lot easier to maintain and update, when new providers are added:cardsobjectSince we are now working with an array instead of one huge RegExp, I had to also change the last "else if":
There I am using the Array some() method, which checks if any of the items inside the array return true for a given function.
Checklist
[ ] README updated (where applicable)[ ] Tests written (where applicable)