Posted By

Pjotor on 08/02/10


Tagged

personnummer checksum EAN


Versions (?)

Pnr (swe) and EAN checksum prototype


 / Published in: JavaScript
 

URL: http://pjotor.com

EAN and Personnummer checksum/digit calculator.

Usage:

"123456789".ean() ==> "1234567895"

"123456789".pnr() ==> "1234567897"

Note: This does not validate checksum/digit, only calculates it.

  1. String.prototype.ean = function(){var n=0;var d=(this+'0').split('').reverse();for(var i in d){n+=(i%2)?d[i]*3:+d[i]}return this+(10-(n%10))%10}
  2.  
  3.  
  4. String.prototype.pnr = function(){var n=[];var d=(this).split(''); for(var i in d){n[i]=((+i+1)%2)?d[i]*2:+d[i]} d=n.join('').split(''); n=0; for(var i in d){n+=+d[i]} n=(10-(n%10)); return this + String(n).substr(String(n).length-1) }
  5.  
  6. //Map/reduce prn check
  7. String.prototype.pnr_check = function(){ return 10-(this.slice(-10,-1).split('').map(function(n,i){ return ((+i+1)%2)?n*2:+n }).join('').split('').reduce(function(a,b){ return +a + +b; }) % 10) === +this.substr(-1); }

Report this snippet  

You need to login to post a comment.