Skip to content

Instantly share code, notes, and snippets.

@FOLLGAD
Last active October 18, 2022 09:08
Show Gist options
  • Select an option

  • Save FOLLGAD/d58be1d5876a486bd5940b3a80112b2f to your computer and use it in GitHub Desktop.

Select an option

Save FOLLGAD/d58be1d5876a486bd5940b3a80112b2f to your computer and use it in GitHub Desktop.
10-modulmetoden i Javascript för att validera svenska personnummer
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