refactor(isCreditCard): create allCards dynamically and get rid of hard-to-maintain hardcoded version by pano9000 · Pull Request #2117 · validatorjs/validator.js
Hello,
I've refactored the way the allCards variable 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:
- it adds unnecessary code duplication (you need to store the provider RegExps in two places: 1x on its own and 1x inside
allCards - (at least to my eyes) it is unreadable
- therefore it makes it a nightmare to update RegExps or add new providers
I've replace it with a dynamically created array with the RegExps from the cards object instead, which will make it a lot easier to maintain and update, when new providers are added:
- you only need to add the RegExp inside the
cardsobject - no manual copying of the RegExp necessary
Since 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
- PR contains only changes related; no stray files, etc.
[ ] README updated (where applicable)[ ] Tests written (where applicable)