braintree / credit-card-type

A library for determining credit card type

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

getTypeInfo does not return prefixPattern/exactPattern regular expressions

jestanoff opened this issue · comments

General information

  • Library version: 7.00
  • Browser and OS: Chrome 68.0.3440.106 (64-bit) on MacOS 10.12.6

Issue description

My intended use case is to modify existing card type VISA, add additional length of 11 but change nothing else:

  const visa = creditCardType.getTypeInfo(creditCardType.types.VISA);
  creditCardType.addCard({
    ...visa,
    lengths: [11, ...visa.lengths],
  });

with the above code I'm getting the following error:

undefined is not an object (evaluating 'value.exactPattern.test')
          creditCardType@webpack:///node_modules/credit-card-type/index.js:186

If I include prefixPattern/exactPattern e.g.

  const visa = creditCardType.getTypeInfo(creditCardType.types.VISA);
  creditCardType.addCard({
    ...visa,
    prefixPattern: /^4$/,
    exactPattern: /^4\d*$/,
    lengths: [11, ...visa.lengths],
  });

That issue is solved, still these properties shouldn't be overrided manually as they might change in feature releases of the package.

I narrowed down the issue in the clone() method that is using JSON.stringify to clone the types. RegEx object cannot be cloned with this method.
https://github.com/braintree/credit-card-type/blob/master/index.js#L37

We also intentionally remove the properties:

delete dupe.prefixPattern;
delete dupe.exactPattern;

Thanks for bringing this up. I think it'd probably best to include an updateCard method that allows you to modify the existing one, rather than trying to merge it.

Fair point, it is a feature request then. Thank you!