Created
May 5, 2020 17:13
-
-
Save gkucmierz/3e7ec2a65f96def84f0125ad6049e7fd to your computer and use it in GitHub Desktop.
MortgageRegister calculate and verify checksum
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // https://ekw.ms.gov.pl/ | |
| class MortgageRegister { | |
| static check(str) { | |
| const m = str.match(/([A-Z0-9]{4}\/[0-9]{8})\/([0-9])/); | |
| if (m) { | |
| return this.checksum(m[1]) === +m[2]; | |
| } | |
| return false; | |
| } | |
| static checksum(str) { | |
| const a = '0123456789XABCDEFGHIJKLMNOPRSTUWYZ'; | |
| return ( | |
| (str.toUpperCase().match(/[a-z0-9]/gi) || []) | |
| .map((c, i) => a.indexOf(c) * [1, 3, 7][i%3]) | |
| .reduce((a, n) => a + n, 0) | |
| ) % 10; | |
| } | |
| }; | |
| console.log([ | |
| MortgageRegister.check('WL1A/00272852/9'), | |
| MortgageRegister.checksum('WL1A/00272852') | |
| ]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment