Last active
June 28, 2020 11:57
-
-
Save hakanersu/3ef2c6cef2350a6b0f8a8b9291203e19 to your computer and use it in GitHub Desktop.
Validate turkish identity number with es6 functions.
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
/** | |
* Cogu dogrulama kosullari saglayip saglamadigina bakar. | |
* burada ise bize verilen girdi ile algoritmaya gore | |
* yeni bir deger uretilir ve son olarak bu deger ile | |
* girdi karsilastirilir. | |
* Acikcasi bu yontemin daha performansli oldugu kanaatinde degilim | |
* ama es6 fonksiyonlariyla biraz oynamak istedim. | |
*/ | |
const validateTcKimlik = (tc) => { | |
const input = tc.slice(0,9) | |
const result = [...input].reduce((acc, item, index) => { | |
acc[!(index % 2) ? 'even' : 'odd'].push(item); | |
return acc; | |
}, { even: [], odd: [] }); | |
const totals = Object.keys(result).map(item => { | |
return result[item].reduce((a, b) => Number(a) + Number(b)) | |
}) | |
const ten = input+ (totals[0] * 7 - totals[1]) % 10 | |
const eleven = ([...ten].reduce((a, b) => Number(a) + Number(b))) % 10 | |
return (ten + eleven) === tc | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment