-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
3,615 additions
and
11,121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
/** | ||
* Validid is a Javascript library to validate ID Card numbers of China, Taiwan, Hong Kong and South Korea. | ||
* Validid is a Javascript library to validate ID card number of Hong Kong, Taiwan, South Korea and China. | ||
* | ||
* Validid is open source in: | ||
* https://github.com/Edditoria/validid | ||
* | ||
* @author Edditoria | ||
* @license MIT | ||
* | ||
* Code released under the MIT license: | ||
* https://github.com/Edditoria/validid/blob/master/LICENSE.txt | ||
* | ||
* https://github.com/Edditoria/validid/blob/main/LICENSE.txt | ||
* | ||
* @author Edditoria <edditoria@pm.me> | ||
* @license MIT | ||
*/ | ||
|
||
function normalize(id){var re;re=/[-\/\s]/g;id=id.toUpperCase().replace(re,"");re=/\([A-Z0-9]\)$/;if(re.test(id)){id=id.replace(/[\(\)]/g,"")}return id}function isDateValid(idDate,minDate,maxDate){if(minDate===void 0)minDate="default";if(maxDate===void 0)maxDate="today";var isFormatValid,parseDate;if(minDate==="default"||minDate===""){minDate="18991129"}isFormatValid=function(date){return typeof date==="string"&&/^[0-9]{8}$/.test(date)};if(!isFormatValid(idDate)){return false}if(!isFormatValid(minDate)){return false}parseDate=function(input){var date,day,isDayValid,isFutureDate,isLeapYear,isMonthValid,maxDay,month,startIndex,year;startIndex=0;year=+input.substring(startIndex,startIndex+=4);month=input.substring(startIndex,startIndex+=2);day=+input.substring(startIndex,startIndex+=2);date=new Date(year,+month-1,day);maxDay="01,03,05,07,08,10,12".indexOf(month)>=0?31:"04,06,09,11".indexOf(month)>=0?30:(isLeapYear=year%4===0&&year%100!==0||year%400===0,isLeapYear?29:28);isDayValid=day>0&&day<=maxDay;if(!isDayValid){return false}isMonthValid=+month>0&&+month<=12;if(!isMonthValid){return false}isFutureDate=new Date<date;if(isFutureDate){return false}return date};idDate=parseDate(idDate);if(idDate===false){return false}minDate=parseDate(minDate);if(minDate===false){return false}maxDate=maxDate==="today"?new Date:typeof maxDate==="string"?parseDate(maxDate):maxDate;if(maxDate===false){return false}return idDate>=minDate&&idDate<=maxDate}function cnid(id){var isChecksumValid,isFormatValid,isThisDateValid;isFormatValid=function(id){return/^[0-9]{17}[0-9X]$/.test(id)};isThisDateValid=function(id){return isDateValid(id.substring(6,14),"18860625")};isChecksumValid=function(id){var char,checkDigit,getWeight,i,identifier,index,len,remainder,weightedSum;identifier=id.slice(0,-1);checkDigit=id.slice(-1)==="X"?10:+id.slice(-1);getWeight=function(n){return Math.pow(2,n-1)%11};weightedSum=0;index=id.length;for(i=0,len=identifier.length;i<len;i++){char=identifier[i];weightedSum+=+char*getWeight(index);index--}remainder=(12-weightedSum%11)%11-checkDigit;return remainder===0};id=normalize(id);return isFormatValid(id)&&isThisDateValid(id)&&isChecksumValid(id)}function isTwidChecksumValid(id,letterNum){var char,i,idLen,idLetters,idNumbers,len,letterIndex,letters,remainder,weight,weightedSum;idLetters=id.slice(0,letterNum);idNumbers=id.slice(letterNum);idLen=10;letters="ABCDEFGHJKLMNPQRSTUVXYWZIO";letterIndex=letters.indexOf(idLetters[0]);weightedSum=Math.floor(letterIndex/10+1)+letterIndex*(idLen-1);if(letterNum===2){weightedSum+=letters.indexOf(idLetters[1])*(idLen-2)}weight=idLen-idLetters.length-1;for(i=0,len=idNumbers.length;i<len;i++){char=idNumbers[i];weightedSum+=+char*weight;weight--}remainder=(weightedSum+ +idNumbers.slice(-1))%10;return remainder===0}function twid(id){var isFormatValid;isFormatValid=function(id){return/^[A-Z][12][0-9]{8}$/.test(id)};id=normalize(id);return isFormatValid(id)&&isTwidChecksumValid(id,1)}function getTwrcFormat(id){if(/^[A-Z][A-D][0-9]{8}$/.test(id)){return"old"}if(/^[A-Z][89][0-9]{8}$/.test(id)){return"new"}return false}function twrc(id){var idFormat;id=normalize(id);idFormat=getTwrcFormat(id);if(idFormat==="old"){return isTwidChecksumValid(id,2)}if(idFormat==="new"){return isTwidChecksumValid(id,1)}return false}function twrcLegacy(id){var isFormatValid;isFormatValid=function(id){return/^[A-Z][A-D][0-9]{8}$/.test(id)};id=normalize(id);return isFormatValid(id)&&isTwidChecksumValid(id,2)}function hkid(id){var getLetterValue,isChecksumValid,isFormatValid,isLetter;getLetterValue=function(letter){return letter.charCodeAt(0)-55};isLetter=function(char){return/[a-zA-Z]/.test(char)};isFormatValid=function(id){return/^[A-NP-Z]{1,2}[0-9]{6}[0-9A]$/.test(id)};isChecksumValid=function(id){var char,charValue,checkDigit,i,identifier,len,remainder,weight,weightedSum;weight=id.length;weightedSum=weight===8?324:0;identifier=id.slice(0,-1);checkDigit=id.slice(-1)==="A"?10:+id.slice(-1);for(i=0,len=identifier.length;i<len;i++){char=identifier[i];charValue=isLetter(char)?getLetterValue(char):+char;weightedSum+=charValue*weight;weight--}remainder=(weightedSum+checkDigit)%11;return remainder===0};id=normalize(id);return isFormatValid(id)&&isChecksumValid(id)}function getMaxDate(yearsOld){var now,year;now=new Date;year=now.getFullYear()-yearsOld;return new Date(year,now.getMonth(),now.getDate())}function krid(id){var isChecksumValid,isFormatValid,isThisDateValid;isFormatValid=function(id){return/^[0-9]{13}$/.test(id)};isThisDateValid=function(id){var date,maxDate,sDigit,yearPrefix;sDigit=id.substring(6,7);yearPrefix=function(){switch(sDigit){case"1":case"2":case"5":case"6":return"19";case"3":case"4":case"7":case"8":return"20";default:return"18"}}();date=yearPrefix+id.substring(0,6);maxDate=getMaxDate(17);return isDateValid(date,"default",maxDate)};isChecksumValid=function(id){var char,i,index,len,remainder,weight,weightedSum;weight=[2,3,4,5,6,7,8,9,2,3,4,5,0];weightedSum=0;index=0;for(i=0,len=id.length;i<len;i++){char=id[i];weightedSum+=+char*weight[index];index++}remainder=(11-weightedSum%11)%10-+id.slice(-1);return remainder===0};id=normalize(id);return isFormatValid(id)&&isThisDateValid(id)&&isChecksumValid(id)}var depreciatedError,validid;depreciatedError=function(){throw new Error("validid.tools is depreciated. Please use validid.utils instead")};validid=function(){return null};validid.utils={normalize:normalize,isDateValid:isDateValid,getMaxDate:getMaxDate,isTwidChecksumValid:isTwidChecksumValid,getTwrcFormat:getTwrcFormat};validid.tools={normalize:depreciatedError,isDateValid:depreciatedError,getMaxDate:depreciatedError,isTwidChecksumValid:depreciatedError,getTwrcFormat:depreciatedError};validid.cnid=cnid;validid.twid=twid;validid.twrc=twrc;validid.hkid=hkid;validid.krid=krid;validid.twrcLegacy=twrcLegacy;var validid$1=validid;export{validid$1 as default}; | ||
function t(t){let e=/[-/\s]/g;return t=t.toUpperCase().replace(e,""),e=/\([A-Z0-9]\)$/,e.test(t)&&(t=t.replace(/[()]/g,"")),t}function e(t){return/^[A-Z]$/.test(t)}function n(t,e){const n=+t.substring(0,4),r=+t.substring(4,6),o=+t.substring(6,8),s=+e.substring(0,4),c=+e.substring(4,6),i=+e.substring(6,8);let a=s-n;return(c<r||c===r&&i<o)&&a--,a}function r(t){if("string"!=typeof t)return!1;if(8!==t.length)return!1;const e=t.substring(0,4),n=t.substring(4,6),r=t.substring(6,8),o=new Date(`${e}-${n}-${r}`);return!Number.isNaN(o.valueOf())}const o={},s={},c={code:0,text:"OK",desc:""},i={code:1,text:"UNKNOWN_ERROR",desc:""},a={code:2,text:"INVALID_LENGTH",desc:""},u={code:3,text:"INVALID_FORMAT",desc:""},d={code:4,text:"INVALID_DATE",desc:""},I={code:5,text:"INVALID_CHECKSUM",desc:""},l={code:6,text:"DUMMY_ID",desc:""},N="^[A-NP-Z]{1,2}[0-9]{6}[0-9A]$",f={OK:c,UNKNOWN_ERROR:i,INVALID_LENGTH:a,INVALID_FORMAT:u,INVALID_CHECKSUM:I};function A(t){return 8===t.length||9===t.length}function D(t){let n=t.length,r=8===n?324:0;const o=t.slice(0,-1);for(const t of o){r+=(e(t)?t.charCodeAt(0)-55:+t)*n,n--}const s=(11-r%11)%11;return 10===s?"A":""+s}function p(e){const n=t(e),r="HKID";return A(n)?new RegExp(N).test(n)?n.slice(-1)!==D(n)?{id:n,type:r,ok:!1,status:f.INVALID_CHECKSUM}:{id:n,type:r,ok:!0,status:f.OK}:{id:n,type:r,ok:!1,status:f.INVALID_FORMAT}:{id:n,type:r,ok:!1,status:f.INVALID_LENGTH}}const g=10,L="ABCDEFGHJKLMNPQRSTUVXYWZIO",C=Object.freeze({NIC:"NIC",RC:"RC",INVALID:"INVALID"}),R=Object.freeze({NIC:"^[A-Z][12][0-9]{8}$",RC:"^[A-Z][89A-D][0-9]{8}$"}),_=Object.freeze({RC_2021:"RC_2021",RC_LEGACY:"RC_LEGACY",NOT_RC:"NOT_RC"}),E=Object.freeze({RC_2021:"^[A-Z][89][0-9]{8}$",RC_LEGACY:"^[A-Z][A-D][0-9]{8}$"}),w={OK:c,UNKNOWN_ERROR:i,INVALID_LENGTH:a,INVALID_FORMAT:u,INVALID_CHECKSUM:I};function O(t){return new RegExp(R.NIC).test(t)?C.NIC:new RegExp(R.RC).test(t)?C.RC:C.INVALID}function V(t){return new RegExp(E.RC_LEGACY).test(t)?_.RC_LEGACY:new RegExp(E.RC_2021).test(t)?_.RC_2021:_.NOT_RC}function h(t){let n=function(t,e=9){const n=L.indexOf(t[0]);return Math.floor(n/10+1)+n*e}(t)+function(t,n=8){const r=t[1];return(e(r)?+L.indexOf(r):+r)*n}(t);const r=t.slice(2,-1);let o=r.length;for(const t of r)n+=+t*o,o--;return""+(10-n%10)%10}function T(e){const n=t(e),r="TWID",o=O(n);return 10!==n.length?{id:n,type:r,ok:!1,status:w.INVALID_LENGTH}:o===C.INVALID?{id:n,type:r,ok:!1,status:w.INVALID_FORMAT}:h(n)!==n.slice(-1)?{id:n,type:r,ok:!1,status:w.INVALID_CHECKSUM}:{id:n,type:r,ok:!0,status:w.OK}}const k=13,y="^[0-9]{13}$",M=17,b={OK:c,UNKNOWN_ERROR:i,INVALID_LENGTH:a,INVALID_FORMAT:u,INVALID_DATE:d,INVALID_CHECKSUM:I};function K(t){let e;switch(t.substring(6,7)){case"1":case"2":case"5":case"6":e="19";break;case"3":case"4":case"7":case"8":e="20";break;default:e="18"}const n=e+t.substring(0,6);if(!r(n))throw new Error("Invalid date");return n}function v(t){const e=[2,3,4,5,6,7,8,9,2,3,4,5,1];let n=0,r=0;const o=t.slice(0,-1);for(const t of o)n+=+t*e[r],r++;return((11-n%11)%10).toString()}function H(e){const r=t(e),o="KRID";if(13!==r.length)return{id:r,type:o,ok:!1,status:b.INVALID_LENGTH};if(!new RegExp(y).test(r))return{id:r,type:o,ok:!1,status:b.INVALID_FORMAT};let s;try{s=K(r)}catch(t){return{id:r,type:o,ok:!1,status:b.INVALID_DATE}}return n(s,(new Date).toLocaleDateString("sv").replace(/-/g,""))<17?{id:r,type:o,ok:!1,status:b.INVALID_DATE}:{id:r,type:o,ok:!0,status:b.OK}}const x=18,G="^[0-9]{17}[0-9X]$",U="18860625",$={OK:c,UNKNOWN_ERROR:i,INVALID_LENGTH:a,INVALID_FORMAT:u,INVALID_DATE:d,INVALID_CHECKSUM:I};function m(t){const e=t.substring(6,14);if(!r(e))throw new Error("Invalid date");return e}function Y(t){const e=t.slice(0,-1);let n=0,r=t.length;for(const t of e){n+=+t*(Math.pow(2,r-1)%11),r--}const o=(12-n%11)%11;return 10===o?"X":""+o}function F(e){const n=t(e),r="CNID";if(18!==n.length)return{id:n,type:r,ok:!1,status:$.INVALID_LENGTH};if(!new RegExp(G).test(n))return{id:n,type:r,ok:!1,status:$.INVALID_FORMAT};let o;try{o=m(n)}catch(t){return{id:n,type:r,ok:!1,status:$.INVALID_DATE}}return+o>=+(new Date).toLocaleDateString("sv").replace(/-/g,"")?{id:n,type:r,ok:!1,status:$.INVALID_DATE}:Y(n)!==n.slice(-1)?{id:n,type:r,ok:!1,status:$.INVALID_CHECKSUM}:{id:n,type:r,ok:!0,status:$.OK}}function S(t){if("string"!=typeof t)throw new TypeError("Input has to be string.");if(8!==t.length)throw new RangeError('Input has to be in format of "YYYYMMDD".');const e=t.substring(0,4),n=t.substring(4,6),r=t.substring(6,8),o=new Date(`${e}-${n}-${r}`);if(Number.isNaN(o.valueOf()))throw new Error("Invalid date.");return o}const P={hkid:function(t){return console.warn("validid.hkid() is deprecated. Please contact developer to update the program."),p(t).ok},twid:function(t){console.warn("validid.twid() is deprecated. Please contact developer to update the program.");const e=T(t);return e.ok&&O(e.id)===C.NIC},twrc:function(t){console.warn("validid.twrc() is deprecated. Please contact developer to update the program.");const e=T(t);return e.ok&&O(e.id)===C.RC},twrcLegacy:function(t){console.warn("validid.twrcLegacy() is deprecated. Please contact developer to update the program.");const e=T(t),n=V(e.id);return e.ok&&n===_.RC_LEGACY},krid:function(t){return console.warn("validid.krid() is deprecated. Please contact developer to update the program."),H(t).ok},cnid:function(t){return console.warn("validid.cnid() is deprecated. Please contact developer to update the program."),F(t).ok},utils:{normalize:function(e){return t(e)},isDateValid:function(t,e="default",n="today"){let r,o,s;"default"!==e&&""!==e||(e="18991129");try{r=S(t),o=S(e)}catch(t){return!1}const c=new Date;if(r>c||o>c)return!1;if("today"===n)s=c;else if("string"==typeof n){try{s=S(n)}catch(t){return!1}if(s<c)return!1}else{if(!(n instanceof Date&&isFinite(n.getTime())))return!1;s=n}return r>=o&&r<=s},getMaxDate:function(t){const e=new Date,n=e.getFullYear()-t;return new Date(n,e.getMonth(),e.getDate())},isTwidChecksumValid:function(t,e){return h(t)===t.slice(-1)},getTwrcFormat:function(t){const e=V(t);return e===_.RC_2021?"new":e===_.RC_LEGACY&&"old"}}};export{U as BIRTH_DATE_OF_OLDEST_CHINESE,x as CNID_LENGTH,G as CNID_PATTERN,$ as CnidStatus,N as HKID_PATTERN,f as HkidStatus,k as KRID_LENGTH,M as KRID_MIN_AGE,y as KRID_PATTERN,b as KridStatus,g as TWID_LENGTH,R as TwidPattern,w as TwidStatus,C as TwidType,E as TwrcPattern,_ as TwrcVersion,o as ValididResponse,s as ValididStatus,m as captureBirthDateFromCnid,K as captureBirthDateFromKrid,P as default,n as getAge,Y as getCnidDigit,D as getHkidDigit,v as getKridDigit,h as getTwidDigit,O as identifyTwidType,V as identifyTwrcVersion,e as isCaptialLetter,t as normalize,l as statusDummyId,I as statusInvalidChecksum,d as statusInvalidDate,u as statusInvalidFormat,a as statusInvalidLength,c as statusOk,i as statusUnknownError,F as validateCnid,r as validateDateString,p as validateHkid,A as validateHkidLength,H as validateKrid,T as validateTwid}; |
Oops, something went wrong.