UNPKG

6.48 kBJavaScriptView Raw
1!function(t,e){if("function"==typeof define&&define.amd)define("Hashids",["exports"],e);else if("undefined"!=typeof exports)e(exports);else{var n={exports:{}};e(n.exports),t.Hashids=n.exports}}("undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:this,(function(t){"use strict";function e(t){return function(t){if(Array.isArray(t)){for(var e=0,n=new Array(t.length);e<t.length;e++)n[e]=t[e];return n}}(t)||function(t){if(Symbol.iterator in Object(t)||"[object Arguments]"===Object.prototype.toString.call(t))return Array.from(t)}(t)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance")}()}t.__esModule=!0,t.onlyChars=t.withoutChars=t.keepUnique=t.default=void 0;var n=function(){function t(t,n,l,u){if(void 0===t&&(t=""),void 0===n&&(n=0),void 0===l&&(l="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"),void 0===u&&(u="cfhistuCFHISTU"),this.minLength=n,"number"!=typeof n)throw new TypeError("Hashids: Provided 'minLength' has to be a number (is "+typeof n+")");if("string"!=typeof t)throw new TypeError("Hashids: Provided 'salt' has to be a string (is "+typeof t+")");if("string"!=typeof l)throw new TypeError("Hashids: Provided alphabet has to be a string (is "+typeof l+")");var p=Array.from(t),c=Array.from(l),g=Array.from(u);this.salt=p;var d=o(c);if(d.length<r)throw new Error("Hashids: alphabet must contain at least "+r+" unique characters, provided: "+d);this.alphabet=a(d,g);var b,m,w,I=h(g,d);(this.seps=f(I,p),0===this.seps.length||this.alphabet.length/this.seps.length>i)&&((b=Math.ceil(this.alphabet.length/i))>this.seps.length&&(m=b-this.seps.length,(w=this.seps).push.apply(w,e(this.alphabet.slice(0,m))),this.alphabet=this.alphabet.slice(m)));this.alphabet=f(this.alphabet,p);var x=Math.ceil(this.alphabet.length/s);this.alphabet.length<3?(this.guards=this.seps.slice(0,x),this.seps=this.seps.slice(x)):(this.guards=this.alphabet.slice(0,x),this.alphabet=this.alphabet.slice(x)),this.guardsRegExp=v(this.guards),this.sepsRegExp=v(this.seps),this.allowedCharsRegExp=y([].concat(e(this.alphabet),e(this.guards),e(this.seps)))}var n=t.prototype;return n.encode=function(t){for(var n=arguments.length,r=new Array(n>1?n-1:0),i=1;i<n;i++)r[i-1]=arguments[i];var s="";return(r=Array.isArray(t)?t:[].concat(e(null!=t?[t]:[]),e(r))).length?(r.every(l)||(r=r.map((function(t){return"bigint"==typeof t||"number"==typeof t?t:d(String(t))}))),r.every(u)?this._encode(r).join(""):s):s},n.decode=function(t){return t&&"string"==typeof t&&0!==t.length?this._decode(t):[]},n.encodeHex=function(t){switch(typeof t){case"bigint":t=t.toString(16);break;case"string":if(!/^[0-9a-fA-F]+$/.test(t))return"";break;default:throw new Error("Hashids: The provided value is neither a string, nor a BigInt (got: "+typeof t+")")}var e=b(t,12,(function(t){return parseInt("1"+t,16)}));return this.encode(e)},n.decodeHex=function(t){return this.decode(t).map((function(t){return t.toString(16).slice(1)})).join("")},n._encode=function(t){var n=this,r=this.alphabet,i=t.reduce((function(t,e,n){return t+("bigint"==typeof e?Number(e%BigInt(n+100)):e%(n+100))}),0),s=[r[i%r.length]],o=s.slice(),a=this.seps,h=this.guards;if(t.forEach((function(i,h){var l,u=o.concat(n.salt,r);r=f(r,u);var c=p(i,r);if((l=s).push.apply(l,e(c)),h+1<t.length){var g=c[0].codePointAt(0)+h,d="bigint"==typeof i?Number(i%BigInt(g)):i%g;s.push(a[d%a.length])}})),s.length<this.minLength){var l=(i+s[0].codePointAt(0))%h.length;if(s.unshift(h[l]),s.length<this.minLength){var u=(i+s[2].codePointAt(0))%h.length;s.push(h[u])}}for(var c=Math.floor(r.length/2);s.length<this.minLength;){var g,d;r=f(r,r),(g=s).unshift.apply(g,e(r.slice(c))),(d=s).push.apply(d,e(r.slice(0,c)));var b=s.length-this.minLength;if(b>0){var v=b/2;s=s.slice(v,v+this.minLength)}}return s},n.isValidId=function(t){return this.allowedCharsRegExp.test(t)},n._decode=function(t){if(!this.isValidId(t))throw new Error("The provided ID ("+t+") is invalid, as it contains characters that do not exist in the alphabet ("+this.guards.join("")+this.seps.join("")+this.alphabet.join("")+")");var n=t.split(this.guardsRegExp),r=n[3===n.length||2===n.length?1:0];if(0===r.length)return[];var i=r[Symbol.iterator]().next().value,s=r.slice(i.length).split(this.sepsRegExp),o=this.alphabet,a=[],h=s,l=Array.isArray(h),u=0;for(h=l?h:h[Symbol.iterator]();;){var p;if(l){if(u>=h.length)break;p=h[u++]}else{if((u=h.next()).done)break;p=u.value}var g=p,d=f(o,[i].concat(e(this.salt),e(o)).slice(0,o.length));a.push(c(Array.from(g),d)),o=d}return this._encode(a).join("")!==t?[]:a},t}();t.default=n;var r=16,i=3.5,s=12,o=function(t){return Array.from(new Set(t))};t.keepUnique=o;var a=function(t,e){return t.filter((function(t){return!e.includes(t)}))};t.withoutChars=a;var h=function(t,e){return t.filter((function(t){return e.includes(t)}))};t.onlyChars=h;var l=function(t){return"bigint"==typeof t||!Number.isNaN(Number(t))&&Math.floor(Number(t))===t},u=function(t){return"bigint"==typeof t||t>=0&&Number.isSafeInteger(t)};function f(t,e){if(0===e.length)return t;for(var n,r=t.slice(),i=r.length-1,s=0,o=0;i>0;i--,s++){o+=n=e[s%=e.length].codePointAt(0);var a=(n+s+o)%i,h=r[i],l=r[a];r[a]=h,r[i]=l}return r}var p=function(t,e){var n=[];if("bigint"==typeof t){var r=BigInt(e.length);do{n.unshift(e[Number(t%r)]),t/=r}while(t>BigInt(0))}else do{n.unshift(e[t%e.length]),t=Math.floor(t/e.length)}while(t>0);return n},c=function(t,e){return t.reduce((function(n,r){var i=e.indexOf(r);if(-1===i)throw new Error("The provided ID ("+t.join("")+") is invalid, as it contains characters that do not exist in the alphabet ("+e.join("")+")");if("bigint"==typeof n)return n*BigInt(e.length)+BigInt(i);var s=n*e.length+i;if(Number.isSafeInteger(s))return s;if("function"==typeof BigInt)return BigInt(n)*BigInt(e.length)+BigInt(i);throw new Error("Unable to decode the provided string, due to lack of support for BigInt numbers in the current environment")}),0)},g=/^\+?[0-9]+$/,d=function(t){return g.test(t)?parseInt(t,10):NaN},b=function(t,e,n){return Array.from({length:Math.ceil(t.length/e)},(function(r,i){return n(t.slice(i*e,(i+1)*e))}))},v=function(t){return new RegExp(t.map((function(t){return m(t)})).sort((function(t,e){return e.length-t.length})).join("|"))},y=function(t){return new RegExp("^["+t.map((function(t){return m(t)})).sort((function(t,e){return e.length-t.length})).join("")+"]+$")},m=function(t){return t.replace(/[-[\]{}()*+?.,\\^$|#\s]/g,"\\$&")}}));
2//# sourceMappingURL=hashids.min.js.map
\No newline at end of file