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

IBAN validation multiple issues #9

Open
tshemsedinov opened this issue Mar 16, 2024 · 2 comments
Open

IBAN validation multiple issues #9

tshemsedinov opened this issue Mar 16, 2024 · 2 comments

Comments

@tshemsedinov
Copy link

static isIbanNumberValid(iban: string): boolean {
const CODE_LENGTHS: Record<string, number> = {
UA: 29,
}
const code = iban.match(/^([A-Z]{2})(\d{2})([A-Z\d]+)$/)
if (!code || iban.length !== CODE_LENGTHS[code[1]]) {
return false
}
const digits: string = (code[3] + code[1] + code[2]).replace(/[A-Z]/g, (letter: string) => (letter.charCodeAt(0) - 55).toString())
return this.mod97(digits) === 1
}

  1. Magic numbers: 3, 2, 2, 55...
  2. Regular expressions are not needed here
  3. Supports only UA ibans so rename to validateIbanUA or better move country to arguments and get universal solution
@Amice13
Copy link

Amice13 commented Mar 18, 2024

According to ISO 7064 each letter in the IBAN string must be replaced with two digits, where A = 10, B = 11, ..., Z = 35.

Is there any way to replace charCodeAt and 55 to avoid magic number, but keep is short?

I've added other countries to the validator as well:

#30

@tshemsedinov
Copy link
Author

Is there any way to replace charCodeAt and 55 to avoid magic number, but keep is short?

To make code understandable all constants should have names and if there are series of constants (for example certain places in BLOB or code, like we have in this place) it should be represented as collection and offsets (see implementation of IP packet parsing or any other binary data structure representation parser).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants