UNPKG

vgscode

Version:

Validate Generate Sanitize Code

2 lines (1 loc) 2.48 kB
var c=Object.defineProperty;var i=(r,t)=>c(r,"name",{value:t,configurable:!0});function a(r){let t=0,n=0;for(let o=0;o<11;o++){const s=parseInt(r.charAt(o),10);t+=s*(o+1),n+=s*(o<9?o+3:o===9?1:2)}let e=t%11;return e===10&&(e=n%11),e===10?0:e}i(a,"calculateControlDigit");function g(r=""){let t=typeof r=="string"?r:String(r);if(!/^\d*$/.test(t)||t.length>11)throw new Error("Input value must be a 'string'/number of 0 to 11 digits");for(;t.length<4;)t+=Math.floor(Math.random()*10).toString();if(t.length===4&&(t+=Math.floor(Math.random()*4).toString()),t.length>=5){const e=parseInt(t.charAt(4),10);if(isNaN(e)||e<0||e>3)throw new Error("The 5th digit must be 0, 1, 2, or 3")}for(;t.length<11;)t+=Math.floor(Math.random()*10).toString();const n=a(t);return t+n.toString()}i(g,"gIIN");function l(r){const t=typeof r=="string"?r:String(r);if(t.length!==12)return!1;const n=parseInt(t.charAt(4),10);return isNaN(n)||n<0||n>3?!1:a(t)===parseInt(t.charAt(11),10)}i(l,"vIIN");function d(r=""){let t=typeof r=="string"?r:String(r);if(t.length>11||!/^\d*$/.test(t))throw new Error("Input value must be a 'string'/number of 0 to 11 digits");if(t.length>=4){const n=(t.charCodeAt(2)-48)*10+(t.charCodeAt(3)-48);if(n<1||n>12)throw new Error("The 3rd and 4th digits must represent a valid month (01-12)")}if(t.length>=5&&![4,5,6].includes(t.charCodeAt(4)-48))throw new Error("The 5th digit must be 4, 5, or 6");if(t.length>=6&&![0,1,2,3].includes(t.charCodeAt(5)-48))throw new Error("The 6th digit must be 0, 1, 2, or 3");for(;t.length<11;)t+=t.length===2?("0"+(Math.floor(Math.random()*12)+1)).slice(-2):t.length===4?(4+Math.floor(Math.random()*3)).toString():t.length===5?Math.floor(Math.random()*4).toString():Math.floor(Math.random()*10).toString();return t+a(t)}i(d,"gBIN");function u(r){const t=typeof r=="string"?r:String(r);if(!/^\d{12}$/.test(t))return!1;const n=(t.charCodeAt(2)-48)*10+(t.charCodeAt(3)-48);return n<1||n>12||![4,5,6].includes(t.charCodeAt(4)-48)||![0,1,2,3].includes(t.charCodeAt(5)-48)?!1:a(t)===t.charCodeAt(11)-48}i(u,"vBIN");function m(r){return r.replace(/\s/g,"")}i(m,"sTrimAll");function p(r){return r.replace(/\D/g,"")}i(p,"sRemoveNonDigits");function w(r,t,n){const e=new Array,o=r.match(/\d+/g)||[];for(const s of o)if(s.length>=t)for(let h=0;h<=s.length-t;h++){const f=s.slice(h,h+t);n(f)&&e.push(f)}return Array.from(e)}i(w,"sExtractCodes");const A={gIIN:g,vIIN:l,gBIN:d,vBIN:u,sTrimAll:m,sRemoveNonDigits:p,sExtractCodes:w};export{A as default};