Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply more functional concepts in this library #79

Closed
carlosrberto opened this issue Jun 13, 2018 · 3 comments
Closed

Apply more functional concepts in this library #79

carlosrberto opened this issue Jun 13, 2018 · 3 comments

Comments

@carlosrberto
Copy link
Contributor

As you suggest @fernahh I'm replicating my coments from #78 here.

I think #78 is a good start point for turning this library more functional.

As an improvement, but not really required I also suggest applying more funcional concepts like decomposing some functions in smaller ones with less responsibilities.

For example isSecurityCodeValid:

export function isSecurityCodeValid(number, code) {
  let brand = getCreditCardNameByNumber(number);
  let numberLength;

  numberLength = (brand === 'Amex') ? 4 : 3;
  let regex = new RegExp(`^[0-9]{${numberLength}}$`);

  return regex.test(code);
}

could be refactored to something like:

const isSecurityCodeValid = (number, code) =>
  isCodeLengthValid(code, getBrandCodeLength(getBrand(number)));

or using some simple functional helpers like compose and curry for better readability:

const isSecurityCodeValid = (number, code) =>
  compose(curry(isCodeLengthValid)(code), getBrandCodeLength, getBrand)(number);

Another example isExpirationDateValid:

export function isExpirationDateValid(paramMonth, paramYearn) {
  const month = parseInt(paramMonth, 10);
  const year = parseInt(paramYearn, 10);

  const currentYear = new Date().getFullYear();
  const currentMonth = new Date().getMonth() + 1;

  if (isNaN(month) || isNaN(year))
    return false;

  if (year === currentYear && month < currentMonth)
    return false;

  if (month < 1 || month > 12)
    return false;

  return !(year < 1000 || year >= 3000);
}

could be:

export const isExpirationDateValid = (month, year) => 
  isValidMonth(month) && isValidYear(year) && !isPastDate(new Date(year, month));
@fernahh
Copy link
Contributor

fernahh commented Jun 18, 2018

@carlosrberto can you get this issue for V3?

@carlosrberto
Copy link
Contributor Author

Yes, I can. I'll start working on this issue soon @fernahh!

@dedicio
Copy link
Contributor

dedicio commented Jan 20, 2021

HI @carlosrberto

Thanks for your contribution!

That suggestions was implemented in PR #120

See more details: https://contaazul.github.io/creditcard.js/

@dedicio dedicio closed this as completed Jan 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants