UNPKG

@konfirm/iso13616

Version:

ISO 13616-1:2007 - International Bank Account Number

3 lines (2 loc) 5.53 kB
function t(t){return null===t?"null":Array.isArray(t)?"array":typeof t}let e=class extends Error{constructor(e){super(`Alphabets requires a string(able), got (${t(e)}) ${e}`);const{constructor:r,constructor:{name:n}}=this;this.name=n,Error.captureStackTrace(this,r)}},r=class extends Error{constructor(t,e){super(`Alphabets cannot contain duplicate characters, found "${e}" in "${t}"`);const{constructor:r,constructor:{name:n}}=this;this.name=n,Error.captureStackTrace(this,r)}};const n=new WeakMap;class a{constructor(a="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"){const s=/string|object/.test(t(a))?String(a):"";const o=Array.from(s),i=o.filter(((t,e,r)=>r.indexOf(t)!==e)).join("");if(!s.length)throw new e(a);if(i.length)throw new r(s,i);n.set(this,{characters:s,alphabet:o})}get characters(){const{characters:t}=n.get(this);return t}get byteLength(){return this.characters.length}get length(){const{alphabet:{length:t}}=n.get(this);return t}slice(t,e){const{alphabet:r}=n.get(this),{constructor:a}=Object.getPrototypeOf(this);return a.from(r.slice(t,e).join(""))}charAt(t){const{alphabet:e}=n.get(this);return e[t]}charCodeAt(t){const{characters:e,length:r}=this;return t>=0&&t<r?e.charCodeAt(t):void 0}codePointAt(t){const e=this.charAt(t);return e?e.codePointAt(0):void 0}indexOf(t){const{alphabet:e}=n.get(this);return e.indexOf(t)}map(...t){const{length:e}=this,r=t=>t<0?r(e+t):t;return t.map((t=>this.charAt(r(t)%e)))}toString(){return this.characters}toJSON(){return String(this)}static from(t){n.has(this)||n.set(this,new Map);const e=n.get(this);return e.has(t)||e.set(t,new this(t)),e.get(t)}}const s=new WeakMap;class o{constructor(t={}){s.set(this,t)}get algorithm(){const{algorithm:t}=s.get(this);return t||"Custom"}get specification(){const{algorithm:t}=this;return`ISO 7064, ${t}`}get designation(){const{designation:t}=s.get(this);return t||0}get indices(){const{indices:t,alphabet:e}=s.get(this);return t||e}get alphabet(){const{alphabet:t}=s.get(this);return t}get modulus(){const{modulus:t,alphabet:e}=s.get(this);return t||e&&e.length}get radix(){const{radix:t}=s.get(this);return t}get double(){const{double:t}=s.get(this);return t||!1}normalize(t){const{indices:e="a-zA-Z0-9"}=this,r=new RegExp(`[^${e}]+`,"g");return String(t).toUpperCase().replace(r,"")}checksum(t){throw new Error("Checksum method not implemented")}validate(t){const{indices:e,alphabet:r,double:n}=this,a=new RegExp(`([${e}]+)([${r}]{${Number(n)+1}})`),s=this.normalize(t).match(a);if(s){const[,t,e]=s;return this.checksum(t)===e}return!1}generate(t){return`${this.normalize(t)}${this.checksum(t)}`}factory(t={}){const{indices:e,alphabet:r,modulus:n,radix:a,double:s}=this,{constructor:o}=Object.getPrototypeOf(this);return new o(Object.assign({indices:e,alphabet:r,modulus:n,radix:a,double:s},t))}}"function"==typeof SuppressedError&&SuppressedError;class i extends o{constructor(t={}){const{alphabet:e,radix:r=2,indices:n=(null==e?void 0:e.slice(0,-1))}=t,a=function(t,e){var r={};for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&e.indexOf(n)<0&&(r[n]=t[n]);if(null!=t&&"function"==typeof Object.getOwnPropertySymbols){var a=0;for(n=Object.getOwnPropertySymbols(t);a<n.length;a++)e.indexOf(n[a])<0&&Object.prototype.propertyIsEnumerable.call(t,n[a])&&(r[n[a]]=t[n[a]])}return r}(t,["alphabet","radix","indices"]);super(Object.assign({alphabet:e,indices:n,radix:r},a))}checksum(t){const{modulus:e,radix:r,double:n,indices:a,alphabet:s}=this,o=s.charAt(0).repeat(Number(n)+1),i=this.normalize(t)+o,c=Array.from(i).map((t=>a.indexOf(t))).reduce(((t,n)=>(t*r+n)%e),0),h=(e+1-c%e)%e;return(n?[h/r|0,h%r]:[h]).map((t=>s.charAt(t))).join("")}}class c extends o{checksum(t){const{modulus:e,indices:r,alphabet:n}=this,a=Array.from(this.normalize(t)).map((t=>r.indexOf(t))).reduce(((t,r)=>2*((t%(e+1)+r)%e||e)),e)%(e+1);return n.charAt((e+1-a)%e)}}const h={num:a.from("0123456789"),numX:a.from("0123456789X"),alpha:a.from("ABCDEFGHIJKLMNOPQRSTUVWXYZ"),alphanum:a.from("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"),alphanumA:a.from("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ*")};new i({algorithm:"MOD 11-2",designation:1,alphabet:h.numX}),new i({algorithm:"MOD 37-2",designation:2,alphabet:h.alphanumA});const u=new i({algorithm:"MOD 97-10",designation:3,modulus:97,radix:10,alphabet:h.num,indices:h.num,double:!0});new i({algorithm:"MOD 661-26",designation:4,modulus:661,radix:26,alphabet:h.alpha,indices:h.alpha,double:!0}),new i({algorithm:"MOD 1271-36",designation:5,modulus:1271,radix:36,alphabet:h.alphanumA,double:!0}),new c({algorithm:"MOD 11,10",designation:6,alphabet:h.num}),new c({algorithm:"MOD 27,26",designation:7,alphabet:h.alpha}),new c({algorithm:"MOD 37,36",designation:8,alphabet:h.alphanum});const l=u.factory({algorithm:"MOD 97-10 (Custom)",designation:3,indices:a.from("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ")});function m(t){const{country:e,checksum:r,account:n}=b(String(t));return d(n,e)===r}function d(t,e){const{modulus:r,indices:n}=l,a=Array.from(`${t}${e}`).map((t=>n.indexOf(t))).filter((t=>t>=0)).join(""),s=l.checksum(a),o=Number(s);return o<=98-r?String(o+r):s}function g(t,e,r=!1){const n=`${e}${d(t,e)}${t}`;return r?p(n):l.normalize(n)}function p(t){const e=l.normalize(String(t));return Array.from(e).reduce(((t,e,r)=>t+(r&&r%4==0?" ":"")+e),"")}function b(t){const[,e,r,n]=l.normalize(t).match(/^([A-Z]{2})([0-9]{2})(\w{1,30})$/)||[];return{country:e,checksum:r,account:n}}export{d as checksum,p as format,g as generate,b as match,m as validate}; //# sourceMappingURL=main.min.mjs.map