Last active
December 4, 2020 12:42
-
-
Save defiant/18387fb096a8581e1b2e9968ea016e50 to your computer and use it in GitHub Desktop.
Vergi Kimlik Numarası (VKN) doğrulama.
This file contains 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
function verifyVkn(vkn) { | |
if (typeof vkn !== 'string') throw new TypeError('vkn should be a string'); | |
if (vkn.length !== 10) throw new TypeError('invalid length'); | |
const digits = vkn.split(''); | |
const control = digits.pop(); // eslint-disable-line | |
const r = []; | |
digits.forEach((el, i) => { | |
const x = Number(el) + 10 - (i + 1); | |
const mod = x % 10; | |
if (mod === 9) { | |
r.push(mod); | |
} else { | |
const pow = 2 ** (10 - (i + 1)); | |
r.push((mod * pow) % 9); | |
} | |
}); | |
const total = r.reduce((acc, cur) => acc + cur); | |
const c = (10 - ((total % 10) % 10)) % 10; | |
return `${digits.join('')}${c}` === vkn; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment