UNPKG

finnish-ssn

Version:

Small utility for validating and creating Finnish social security numbers. No more, no less, no dependencies.

15 lines 3.81 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.FinnishSSN=void 0;var Sex;(function(a){a.FEMALE="female",a.MALE="male"})(Sex||(Sex={}));class FinnishSSN{/** * Parse parameter given SSN string into Object representation. * @param ssn - {String} SSN to parse */static parse(a){// Sanity and format check, which allows to make safe assumptions on the format. if(!SSN_REGEX.test(a))throw new Error("Not valid SSN format");const b=parseInt(a.substring(0,2),10),c=a.substring(2,4),d=a.charAt(6),e=parseInt(a.substring(4,6),10)+centuryMap.get(d),f=a.substring(7,10),g=a.substring(10,11),h=parseInt(f,10)%2?this.MALE:this.FEMALE,i=daysInGivenMonth(e,c);if(!daysInMonthMap.get(c)||b>i)throw new Error("Not valid SSN");const j=parseInt(a.substring(0,6)+f,10),k=new Date(e,parseInt(c,10)-1,b,0,0,0,0),l=new Date;return{valid:g===checksumTable[j%31],sex:h,dateOfBirth:k,ageInYears:ageInYears(k,l)}}/** * Validates parameter given SSN. Returns true if SSN is valid, otherwise false. * @param ssn - {String} For example '010190-123A' */static validate(a){try{return this.parse(a).valid}catch(a){return!1}}/** * Creates a valid SSN using the given age (Integer). Creates randomly male and female SSN'n. * In case an invalid age is given, throws exception. * * @param age as Integer. Min valid age is 1, max valid age is 200 */static createWithAge(a){var b=Math.floor;if(a<MIN_AGE||a>MAX_AGE)throw new Error(`Given age (${a}) is not between sensible age range of ${MIN_AGE} and ${MAX_AGE}`);const c=new Date;let d=c.getFullYear()-a;const e=randomMonth();let f=randomDay(d,e);const g=randomNumber(800)+99;// No need for padding when rollingId >= 100 birthDayPassed(new Date(d,+e-1,+f),c)||(this.isLeapYear(d)&&"29"===f&&e===february&&(f="28"),d--);const h=[];centuryMap.forEach((a,c)=>{a===100*b(d/100)&&h.push(c)});const i=h[b(Math.random()*h.length)];d%=100;const j=yearToPaddedString(d),k=parseInt(f+e+j+g,10),l=checksumTable[k%31];return f+e+j+i+g+l}static isLeapYear(a){return 0==a%4&&0!=a%100||0==a%400}}exports.FinnishSSN=FinnishSSN,FinnishSSN.FEMALE=Sex.FEMALE,FinnishSSN.MALE=Sex.MALE;const centuryMap=new Map;centuryMap.set("F",2e3),centuryMap.set("E",2e3),centuryMap.set("D",2e3),centuryMap.set("C",2e3),centuryMap.set("B",2e3),centuryMap.set("A",2e3),centuryMap.set("U",1900),centuryMap.set("V",1900),centuryMap.set("W",1900),centuryMap.set("X",1900),centuryMap.set("Y",1900),centuryMap.set("-",1900),centuryMap.set("+",1800);const february="02",daysInMonthMap=new Map;daysInMonthMap.set("01",31),daysInMonthMap.set("02",28),daysInMonthMap.set("03",31),daysInMonthMap.set("04",30),daysInMonthMap.set("05",31),daysInMonthMap.set("06",30),daysInMonthMap.set("07",31),daysInMonthMap.set("08",31),daysInMonthMap.set("09",30),daysInMonthMap.set("10",31),daysInMonthMap.set("11",30),daysInMonthMap.set("12",31);const checksumTable=["0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F","H","J","K","L","M","N","P","R","S","T","U","V","W","X","Y"],MIN_AGE=1,MAX_AGE=200,SSN_REGEX=/^(0[1-9]|[12]\d|3[01])(0[1-9]|1[0-2])([5-9]\d\+|\d\d[-U-Y]|[012]\d[A-F])\d{3}[\dA-Z]$/;function randomMonth(){return`00${randomNumber(12)}`.substr(-2,2)}function yearToPaddedString(a){return 10>a%100?`0${a}`:a.toString()}function randomDay(a,b){const c=daysInGivenMonth(a,b);return`00${randomNumber(c)}`.substr(-2,2)}function daysInGivenMonth(a,b){const c=daysInMonthMap.get(b);return b===february&&FinnishSSN.isLeapYear(a)?c+1:c}function randomNumber(a){return Math.floor(Math.random()*a)+1;// no zero }function ageInYears(a,b){return b.getFullYear()-a.getFullYear()-(birthDayPassed(a,b)?0:1)}function birthDayPassed(a,b){return a.getMonth()<b.getMonth()||a.getMonth()===b.getMonth()&&a.getDate()<=b.getDate()}//# sourceMappingURL=finnish-ssn.js.map