UNPKG

verifiera

Version:

A JavaScript library to perform common operations on Personal Identification Numbers (Social Security Numbers), like: Validation, censoring the individual digits, calculating age and detecting gender.

3 lines (2 loc) 8.05 kB
!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports):"function"==typeof define&&define.amd?define(["exports"],r):r((e=e||self).verifiera={})}(this,function(e){"use strict";function c(e){return e.replace(/[^\d]/g,"").trim()}function f(e,r){var n=c(e),t=0;return r.forEach(function(e,r){t+=e*parseInt(n[r],10)}),t}function d(e,r,n){return void 0===n&&(n=11),f(e,r)%n==0}function r(){return 0}function n(){return""}function t(){return""}function a(){return""}function u(){return""}function i(){return 0}function v(){return!1}function g(){return!1}function o(){return!1}function s(e){return function(){return parseInt(e.gender,10)%2==0?"f":"m"}}function y(e,r){return function(){return r()+"-"+e.month+"-"+e.day}}function l(r){return function(e){return void 0===e&&(e=""),function(e,r){var n=new Date(e),t=r?new Date(r):new Date,a=t.getFullYear()-n.getFullYear();n.setFullYear(n.getFullYear()+a),t<n&&(--a,n.setFullYear(n.getFullYear()-1));var u,i,o=a+(t.getTime()-n.getTime())/864e5/(u=t.getFullYear(),(i=new Date(u,1,28)).setDate(i.getDate()+1),1===i.getMonth()?366:365);return 0<o?Math.floor(o):0}(r(),e)}}function p(a,u,i,o,c){return function(){return 0<a.length&&u.test(a)&&(e=parseInt(i.day,10),r=parseInt(i.month,10),n=o(),(t=new Date).setFullYear(n,r-1,e),t.getFullYear()===n&&t.getMonth()+1===r&&t.getDate()===e)&&c();var e,r,n,t}}function h(e){return void 0===e&&(e={}),N(N({getAge:r,getBirthday:n,getCensored:t,getCountry:a,getGender:u,getYear:i},e),{validate:o})}function m(u){var e=new RegExp("^([0-9]{2})([0-9]{2})([0-9]{2})/([0-9]{3})([0-9])?$"),n=e.exec(u);if(!n)return h();function r(){var e="19",r=parseInt(t.year,10);return 10===u.length&&54<=r?e="18":11===u.length&&r<54&&(e="20"),parseInt(e+t.year,10)}var t={day:n[3],month:function(){var e=parseInt(n[2],10);if(51<=e&&e<=62){var r=e-50;return(r<10?"0":"")+r.toString()}return n[2]}(),year:n[1],individualNumber:n[4]},a=p(u,e,t,r,function(){if(10===u.length)return!0;for(var e=c(u),r=0,n=0,t=0;t<e.length;t++){var a=parseInt(e[t],10);(t+1)%2==0?n+=a:r+=a}return(r-n)%11==0});if(!a())return h();var i=y(t,r);return{getAge:l(i),getBirthday:i,getCensored:function(){return""+t.year+n[2]+t.day+"/****"},getCountry:function(){return"czech_slovakia"},getGender:function(){var e=parseInt(n[2],10);return 51<=e&&e<=62?"f":"m"},getYear:r,validate:a}}function I(e,r){void 0===r&&(r=!1);var n=new RegExp("^([0-9]{2})([0-9]{2})([0-9]{2})-([0-9])([0-9]{2})([0-9])$"),t=n.exec(e);if(!t)return h();function a(){var e=parseInt(u.year,10),r=["4","9"].includes(u.centuryIndicator),n=["5","6","7","8"].includes(u.centuryIndicator),t="19";return r&&e<=36||n&&e<=57?t="20":n&&58<=e&&(t="18"),parseInt(""+t+u.year,10)}var u={day:t[1],month:t[2],year:t[3],centuryIndicator:t[4],individualNumber:t[5],gender:t[6]},i=p(e,n,u,a,function(){return!r||d(c(e),[4,3,2,7,6,5,4,3,2,1])});if(!i())return h();var o=y(u,a);return{getAge:l(o),getBirthday:o,getCensored:function(){return""+u.day+u.month+u.year+"-****"},getCountry:function(){return"denmark"},getGender:s(u),getYear:a,validate:i}}function C(e){var r=new RegExp("^([0-9]{2})([0-9]{2})([0-9]{2})([-+A])([0-9]{3})([0-9A-Y])$"),n=r.exec(e);if(!n)return h();function t(){var e="19";switch(a.centuryIndicator){case"-":e="19";break;case"+":e="18";break;case"A":e="20"}return parseInt(""+e+a.year,10)}var a={day:n[1],month:n[2],year:n[3],centuryIndicator:n[4],individualNumber:n[5],checksum:n[6],gender:n[5]},u=p(e,r,a,t,function(){return"0123456789ABCDEFHJKLMNPRSTUVWXY"[parseInt(""+a.day+a.month+a.year+a.individualNumber,10)%31]===a.checksum.toUpperCase()});if(!u())return h();var i=y(a,t);return{getAge:l(i),getBirthday:i,getCensored:function(){return""+a.day+a.month+a.year+a.centuryIndicator+"****"},getCountry:function(){return"finland"},getGender:s(a),getYear:t,validate:u}}function w(e){function r(){return d(e,[9,8,7,6,5,4,3,2,-1])}var n=new RegExp("^([0-9]{9})$").exec(e),t=h();return n&&r()&&(t.validate=r,t.getCensored=function(){return"*********"},t.getCountry=function(){return"netherlands"}),t}function b(e){function r(){return/^[4-7][0-9]{10}$/.test(e)}var n=new RegExp("^([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{3})([0-9])([0-9])$"),t=n.exec(e);if(!t)return h({isDNumber:g});function a(){var e=parseInt(u.individualNumber,10),r=parseInt(u.year,10),n=19;return 499<e&&(e<750&&54<=r?n=18:r<40?n=20:900<=e&&40<=r&&(n=19)),100*n+r}var u={day:function(){if(r()){var e=parseInt(t[1],10)-40;return(e<10?"0":"")+e.toString()}return t[1]}(),month:t[2],year:t[3],individualNumber:t[4],gender:t[4]},i=p(e,n,u,a,function(){return d(e,[3,7,6,1,8,9,4,5,2,1])&&d(e,[5,4,3,2,7,6,5,4,3,2,1])});if(!i())return h({isDNumber:g});var o=y(u,a);return{getAge:l(o),getBirthday:o,getCensored:function(){return""+t[1]+u.month+u.year+"*****"},getCountry:function(){return"norway"},getGender:s(u),getYear:a,isDNumber:r,validate:i}}function x(n){var e=new RegExp("^([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{3})([0-9])([0-9])$"),t=e.exec(n);if(!t)return h();function r(){var e=parseInt(t[2],10),r="19";return e<20?r="19":20<e&&e<33?r="20":40<e&&e<53?r="21":60<e&&e<73&&(r="22"),parseInt(r+u.year,10)}var a,u={day:t[3],month:((a=parseInt(t[2],10)%20)<10?"0":"")+a.toString(),year:t[1],individualNumber:t[4],gender:t[5],checksum:t[6]},i=p(n,e,u,r,function(){var e=f(n,[1,3,7,9,1,3,7,9,1,3])%10,r=parseInt(n[10],10);return 0==e&&0===r||r===10-e});if(!i())return h();var o=y(u,r);return{getAge:l(o),getBirthday:o,getCensored:function(){return""+u.year+t[2]+u.day+"*****"},getCountry:function(){return"poland"},getGender:s(u),getYear:r,validate:i}}function Y(e){return 0===function(e){for(var r=c(e).split("").reverse().join(""),n=0,t=0;t<=r.length-1;t+=1){var a=parseInt(r[t],10);9<(a=t%2==1?2*a:a)&&(a-=9),n+=a}return n%10}(c(e))}function D(e,t){void 0===t&&(t=!0);var n=t?3:4,r="^([0-9]{2})([0-9]{2})([0-9]{2})([-+])([0-9]{3})([0-9])$";t||(r="^(18|19|20)?([0-9]{2})([0-9]{2})([0-9]{2})([-+])?([0-9]{3})([0-9])$");var a=new RegExp(r),u=a.exec(e);if(!u)return h({isCoordinationNo:v});function i(){var e=parseInt(u[n],10);if(61<=e){var r=e-60;return(r<=9?"0":"")+r.toString()}return u[n]}var o={centuryIndicator:"",day:i(),month:u[2],year:u[1],separator:u[4],individualNumber:u[5],gender:u[5],checksum:u[6]};function c(){if(!t&&o.centuryIndicator)return parseInt(""+o.centuryIndicator+o.year,10);var e=o.year+"-"+o.month+"-"+o.day,r=new Date,n="19";return r<new Date("20"+e)?n="19":r>=new Date("20"+e)&&"-"===o.separator?n="20":r>=new Date("19"+e)&&"+"===o.separator&&(n="19"),parseInt(""+n+o.year,10)}t||(o={centuryIndicator:u[1]||"",day:i(),month:u[3],year:u[2],separator:u[5]||"",individualNumber:u[6],gender:u[6],checksum:u[7]});var f=function(){return Y(e)};!t&&o.centuryIndicator&&(f=function(){return Y(e.replace(new RegExp("^"+o.centuryIndicator),""))});var d=p(e,a,o,c,f);if(!d())return h({isCoordinationNo:v});var g=y(o,c);return{getAge:l(g),getBirthday:g,getCensored:function(){return t?""+o.year+o.month+u[3]+o.separator+"****":""+o.centuryIndicator+o.year+o.month+u[4]+o.separator+"****"},getCountry:function(){return"sweden"},getGender:s(o),getYear:c,isCoordinationNo:function(){return["6","7","8","9"].includes(u[n][0])},validate:d}}var N=function(){return(N=Object.assign||function(e){for(var r,n=1,t=arguments.length;n<t;n++)for(var a in r=arguments[n])Object.prototype.hasOwnProperty.call(r,a)&&(e[a]=r[a]);return e}).apply(this,arguments)},k=[m,I,C,w,b,x,D];e.czechSlovakia=m,e.denmark=I,e.detectCountry=function(e){for(var r=0,n=k;r<n.length;r++){var t=n[r];if(1===t.length){var a=t(e);if(a.validate())return a.getCountry()}else if(2===t.length){var u=t(e,!0),i=t(e,!1);if(u.validate())return u.getCountry();if(i.validate())return i.getCountry()}}return""},e.finland=C,e.getCountryTools=function(e){for(var r=0,n=k;r<n.length;r++){var t=n[r];if(1===t.length){var a=t(e);if(a.validate())return a}else if(2===t.length){var u=t(e,!0),i=t(e,!1);if(u.validate())return u;if(i.validate())return i}}return h()},e.netherlands=w,e.norway=b,e.poland=x,e.sweden=D,Object.defineProperty(e,"__esModule",{value:!0})}); //# sourceMappingURL=verifiera.js.map