braintree / credit-card-type

A library for determining credit card type
MIT License
985 stars 154 forks source link

getTypeInfo does not return prefixPattern/exactPattern regular expressions #77

Closed jestanoff closed 6 years ago

jestanoff commented 6 years ago

General information

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

crookedneighbor commented 6 years ago

We also intentionally remove the properties: https://github.com/braintree/credit-card-type/blob/e8c0125969bf4e07b2c442137d5e1cbc7f10023a/index.js#L38-L39

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.

jestanoff commented 6 years ago

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

crookedneighbor commented 6 years ago

https://github.com/braintree/credit-card-type/pull/78