Skip to content

Instantly share code, notes, and snippets.

@hakanersu
Last active June 28, 2020 11:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hakanersu/3ef2c6cef2350a6b0f8a8b9291203e19 to your computer and use it in GitHub Desktop.
Save hakanersu/3ef2c6cef2350a6b0f8a8b9291203e19 to your computer and use it in GitHub Desktop.
Validate turkish identity number with es6 functions.
/**
* 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