Last active
October 18, 2022 09:08
-
-
Save FOLLGAD/d58be1d5876a486bd5940b3a80112b2f to your computer and use it in GitHub Desktop.
10-modulmetoden i Javascript för att validera svenska personnummer
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
| function getCheckSum(ch) { | |
| let ps = ch.slice(0, -1).split('').reverse() | |
| let sums = ps.map((s, i) => { | |
| let m = (i % 2 == 0) ? 2 : 1; | |
| let rem = (parseInt(s) * m).toString().split('') | |
| return rem.reduce((d,a) => Number(d) + Number(a), 0) | |
| }) | |
| let res = sums.reduce((d,a) => Number(d) + Number(a), 0) | |
| return (Math.ceil(res / 10) * 10 - res) | |
| } | |
| function isValidIdNumber(ch) { | |
| if (ch.length !== 10) throw new Error("Length must be exactly 10") | |
| return ch.slice(-1) == getCheckSum(ch.slice(0, -1)) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment