UNPKG

finnish-ssn-validator

Version:

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

15 lines 3.37 kB
'use strict';Object.defineProperty(exports,'__esModule',{value:!0}),exports.FinnishSSN=void 0;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){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 b=new Date;let c=b.getFullYear()-a;const d=randomMonth(),e=randomDay(c,d);let f;const g=randomNumber(800)+99;// No need for padding when rollingId >= 100 centuryMap.forEach((a,b)=>{a===100*Math.floor(c/100)&&(f=b)}),birthDayPassed(new Date(c,+d-1,+e),b)||c--,c%=100;const h=yearToPaddedString(c),i=parseInt(e+d+h+g,10),j=checksumTable[i%31];return e+d+h+f+g+j}static isLeapYear(a){return 0==a%4&&0!=a%100||0==a%400}}exports.FinnishSSN=FinnishSSN,FinnishSSN.FEMALE='female',FinnishSSN.MALE='male';const centuryMap=new Map;centuryMap.set('A',2e3),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-|[012]\dA)\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