Created
April 22, 2014 20:58
-
-
Save adasq/11194077 to your computer and use it in GitHub Desktop.
PESEL
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
var Pesel = { | |
getDateByPesel: function (pesel) { | |
var l, c, birth = [ | |
pesel.substr(0, 2), | |
pesel.substr(2, 2), | |
pesel.substr(4, 2) | |
], | |
isOdd = function (c) { | |
return (c % 2 === 1); | |
}, years = { | |
"0": "19", | |
"2": "20", | |
"4": "21", | |
"6": "22", | |
"8": "18", | |
}; | |
l = +birth[1]; | |
c = Math.floor(l / 10); | |
isOdd(c) && --c; | |
return { | |
year: +(years[c] + birth[0]), | |
month: l - (c * 10), | |
day: +birth[2] | |
}; | |
}, | |
isValid: function (pesel) { | |
if (pesel.length !== 11) return false; | |
var multiples = [1, 3, 7, 9, 1, 3, 7, 9, 1, 3], | |
control, number = 0; | |
_.each(pesel, function (a, i) { | |
if (i === 10) return; | |
number += a * multiples[i]; | |
}); | |
control = (number % 10); | |
control = (control > 0) ? (10 - control) : control; | |
return (control === +pesel[10]); | |
} | |
}; | |
var pesel = "76041219615"; | |
console.log(Pesel.getDateByPesel(pesel)); | |
console.log(Pesel.isValid(pesel)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment