Skip to content

Instantly share code, notes, and snippets.

@gkucmierz
Created May 5, 2020 17:13
Show Gist options
  • Select an option

  • Save gkucmierz/3e7ec2a65f96def84f0125ad6049e7fd to your computer and use it in GitHub Desktop.

Select an option

Save gkucmierz/3e7ec2a65f96def84f0125ad6049e7fd to your computer and use it in GitHub Desktop.
MortgageRegister calculate and verify checksum
// 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