Skip to content

Instantly share code, notes, and snippets.

@trevorhreed
Created December 4, 2018 17:04
Show Gist options
  • Save trevorhreed/6fa6b9bf084e36471ea5012139e69b0a to your computer and use it in GitHub Desktop.
Save trevorhreed/6fa6b9bf084e36471ea5012139e69b0a to your computer and use it in GitHub Desktop.
console.clear()
const Pid = (function(){
// UUID generator
!function(n){"use strict";function e(){var e=n.crypto||n.msCrypto;if(!f&&e&&e.getRandomValues)try{var r=new Uint8Array(16);s=f=function(){return e.getRandomValues(r),r},f()}catch(n){}if(!f){var o=new Array(16);i=f=function(){for(var n,e=0;e<16;e++)0===(3&e)&&(n=4294967296*Math.random()),o[e]=n>>>((3&e)<<3)&255;return o},"undefined"!=typeof console&&console.warn&&console.warn("[SECURITY] node-uuid: crypto not usable, falling back to insecure Math.random()")}}function r(){if("function"==typeof require)try{var n=require("crypto").randomBytes;c=f=n&&function(){return n(16)},f()}catch(n){}}function o(n,e,r){var o=e&&r||0,t=0;for(e=e||[],n.toLowerCase().replace(/[0-9a-f]{2}/g,function(n){t<16&&(e[o+t++]=y[n])});t<16;)e[o+t++]=0;return e}function t(n,e){var r=e||0,o=v;return o[n[r++]]+o[n[r++]]+o[n[r++]]+o[n[r++]]+"-"+o[n[r++]]+o[n[r++]]+"-"+o[n[r++]]+o[n[r++]]+"-"+o[n[r++]]+o[n[r++]]+"-"+o[n[r++]]+o[n[r++]]+o[n[r++]]+o[n[r++]]+o[n[r++]]+o[n[r++]]}function u(n,e,r){var o=e&&r||0,u=e||[];n=n||{};var a=null!=n.clockseq?n.clockseq:g,f=null!=n.msecs?n.msecs:(new Date).getTime(),i=null!=n.nsecs?n.nsecs:C+1,c=f-h+(i-C)/1e4;if(c<0&&null==n.clockseq&&(a=a+1&16383),(c<0||f>h)&&null==n.nsecs&&(i=0),i>=1e4)throw new Error("uuid.v1(): Can't create more than 10M uuids/sec");h=f,C=i,g=a,f+=122192928e5;var s=(1e4*(268435455&f)+i)%4294967296;u[o++]=s>>>24&255,u[o++]=s>>>16&255,u[o++]=s>>>8&255,u[o++]=255&s;var l=f/4294967296*1e4&268435455;u[o++]=l>>>8&255,u[o++]=255&l,u[o++]=l>>>24&15|16,u[o++]=l>>>16&255,u[o++]=a>>>8|128,u[o++]=255&a;for(var d=n.node||w,v=0;v<6;v++)u[o+v]=d[v];return e?e:t(u)}function a(n,e,r){var o=e&&r||0;"string"==typeof n&&(e="binary"===n?new d(16):null,n=null),n=n||{};var u=n.random||(n.rng||f)();if(u[6]=15&u[6]|64,u[8]=63&u[8]|128,e)for(var a=0;a<16;a++)e[o+a]=u[a];return e||t(u)}var f,i,c,s,l;n?e():r();for(var d="function"==typeof Buffer?Buffer:Array,v=[],y={},m=0;m<256;m++)v[m]=(m+256).toString(16).substr(1),y[v[m]]=m;var p=f(),w=[1|p[0],p[1],p[2],p[3],p[4],p[5]],g=16383&(p[6]<<8|p[7]),h=0,C=0,R=a;R.v1=u,R.v4=a,R.parse=o,R.unparse=t,R.BufferClass=d,R._rng=f,R._mathRNG=i,R._nodeRNG=c,R._whatwgRNG=s,"undefined"!=typeof module&&module.exports?module.exports=R:"function"==typeof define&&define.amd?define(function(){return R}):(l=n.uuid,R.noConflict=function(){return n.uuid=l,R},n.uuid=R)}("undefined"!=typeof window?window:null);
// Big Number library
/* bignumber.js v6.0.0 https://github.com/MikeMcl/bignumber.js/LICENCE */
!function(e){"use strict";function r(e){function c(e,r){var n,t,i,s,u,f,a,g=this;if(!(g instanceof c))return new c(e,r);if(null==r){if(e instanceof c)return g.s=e.s,g.e=e.e,void(g.c=(e=e.c)?e.slice():e);if(u="number"==typeof e,u&&0*e==0){if(g.s=0>1/e?(e=-e,-1):1,e===~~e){for(i=0,s=e;s>=10;s/=10,i++);return g.e=i,void(g.c=[e])}a=e+""}else{if(!l.test(a=e+""))return S(g,a,u);g.s=45==a.charCodeAt(0)?(a=a.slice(1),-1):1}}else{if(o(r,2,k.length,"Base"),a=e+"",10==r)return g=new c(e instanceof c?e:a),A(g,D+g.e+1,T);if(u="number"==typeof e){if(0*e!=0)return S(g,a,u,r);if(g.s=0>1/e?(a=a.slice(1),-1):1,a.replace(/^0\.0*|\./,"").length>15)throw Error(p+e);u=!1}else g.s=45===a.charCodeAt(0)?(a=a.slice(1),-1):1,r>10&&37>r&&(a=a.toLowerCase());for(n=k.slice(0,r),i=s=0,f=a.length;f>s;s++)if(n.indexOf(t=a.charAt(s))<0){if("."==t&&s>i){i=f;continue}return S(g,e+"",u,r)}a=P(a,r,10,g.s)}for((i=a.indexOf("."))>-1&&(a=a.replace(".","")),(s=a.search(/e/i))>0?(0>i&&(i=s),i+=+a.slice(s+1),a=a.substring(0,s)):0>i&&(i=a.length),s=0;48===a.charCodeAt(s);s++);for(f=a.length;48===a.charCodeAt(--f););if(a=a.slice(s,f+1)){if(f=a.length,u&&f>15&&(e>w||e!==h(e)))throw Error(p+g.s*e);if(i=i-s-1,i>C)g.c=g.e=null;else if(x>i)g.c=[g.e=0];else{if(g.e=i,g.c=[],s=(i+1)%m,0>i&&(s+=m),f>s){for(s&&g.c.push(+a.slice(0,s)),f-=m;f>s;)g.c.push(+a.slice(s,s+=m));a=a.slice(s),s=m-a.length}else s-=f;for(;s--;a+="0");g.c.push(+a)}}else g.c=[g.e=0]}function y(e,r,n,i){var s,l,a,h,g;if(null==n?n=T:o(n,0,8),!e.c)return e.toString();if(s=e.c[0],a=e.e,null==r)g=t(e.c),g=1==i||2==i&&I>=a?u(g,a):f(g,a,"0");else if(e=A(new c(e),r,n),l=e.e,g=t(e.c),h=g.length,1==i||2==i&&(l>=r||I>=l)){for(;r>h;g+="0",h++);g=u(g,l)}else if(r-=a,g=f(g,l,"0"),l+1>h){if(--r>0)for(g+=".";r--;g+="0");}else if(r+=l-h,r>0)for(l+1==h&&(g+=".");r--;g+="0");return e.s<0&&s?"-"+g:g}function b(e,r){var n,t,i=0;for(s(e[0])&&(e=e[0]),n=new c(e[0]);++i<e.length;){if(t=new c(e[i]),!t.s){n=t;break}r.call(n,t)&&(n=t)}return n}function E(e,r,n){for(var t=1,i=r.length;!r[--i];r.pop());for(i=r[0];i>=10;i/=10,t++);return(n=t+n*m-1)>C?e.c=e.e=null:x>n?e.c=[e.e=0]:(e.e=n,e.c=r),e}function A(e,r,n,t){var i,o,s,u,f,c,l,g=e.c,p=v;if(g){e:{for(i=1,u=g[0];u>=10;u/=10,i++);if(o=r-i,0>o)o+=m,s=r,f=g[c=0],l=f/p[i-s-1]%10|0;else if(c=a((o+1)/m),c>=g.length){if(!t)break e;for(;g.length<=c;g.push(0));f=l=0,i=1,o%=m,s=o-m+1}else{for(f=u=g[c],i=1;u>=10;u/=10,i++);o%=m,s=o-m+i,l=0>s?0:f/p[i-s-1]%10|0}if(t=t||0>r||null!=g[c+1]||(0>s?f:f%p[i-s-1]),t=4>n?(l||t)&&(0==n||n==(e.s<0?3:2)):l>5||5==l&&(4==n||t||6==n&&(o>0?s>0?f/p[i-s]:0:g[c-1])%10&1||n==(e.s<0?8:7)),1>r||!g[0])return g.length=0,t?(r-=e.e+1,g[0]=p[(m-r%m)%m],e.e=-r||0):g[0]=e.e=0,e;if(0==o?(g.length=c,u=1,c--):(g.length=c+1,u=p[m-o],g[c]=s>0?h(f/p[i-s]%p[s])*u:0),t)for(;;){if(0==c){for(o=1,s=g[0];s>=10;s/=10,o++);for(s=g[0]+=u,u=1;s>=10;s/=10,u++);o!=u&&(e.e++,g[0]==d&&(g[0]=1));break}if(g[c]+=u,g[c]!=d)break;g[c--]=0,u=1}for(o=g.length;0===g[--o];g.pop());}e.e>C?e.c=e.e=null:e.e<x&&(e.c=[e.e=0])}return e}var R,P,S,_=c.prototype,L=new c(1),D=20,T=4,I=-7,B=21,x=-1e7,C=1e7,M=!1,U=1,F=0,G={decimalSeparator:".",groupSeparator:",",groupSize:3,secondaryGroupSize:0,fractionGroupSeparator:" ",fractionGroupSize:0},k="0123456789abcdefghijklmnopqrstuvwxyz";return c.clone=r,c.ROUND_UP=0,c.ROUND_DOWN=1,c.ROUND_CEIL=2,c.ROUND_FLOOR=3,c.ROUND_HALF_UP=4,c.ROUND_HALF_DOWN=5,c.ROUND_HALF_EVEN=6,c.ROUND_HALF_CEIL=7,c.ROUND_HALF_FLOOR=8,c.EUCLID=9,c.config=c.set=function(e){var r,n;if(null!=e){if("object"!=typeof e)throw Error(g+"Object expected: "+e);if(e.hasOwnProperty(r="DECIMAL_PLACES")&&(n=e[r],o(n,0,O,r),D=n),e.hasOwnProperty(r="ROUNDING_MODE")&&(n=e[r],o(n,0,8,r),T=n),e.hasOwnProperty(r="EXPONENTIAL_AT")&&(n=e[r],s(n)?(o(n[0],-O,0,r),o(n[1],0,O,r),I=n[0],B=n[1]):(o(n,-O,O,r),I=-(B=0>n?-n:n))),e.hasOwnProperty(r="RANGE"))if(n=e[r],s(n))o(n[0],-O,-1,r),o(n[1],1,O,r),x=n[0],C=n[1];else{if(o(n,-O,O,r),!n)throw Error(g+r+" cannot be zero: "+n);x=-(C=0>n?-n:n)}if(e.hasOwnProperty(r="CRYPTO")){if(n=e[r],n!==!!n)throw Error(g+r+" not true or false: "+n);if(n){if("undefined"==typeof crypto||!crypto||!crypto.getRandomValues&&!crypto.randomBytes)throw M=!n,Error(g+"crypto unavailable");M=n}else M=n}if(e.hasOwnProperty(r="MODULO_MODE")&&(n=e[r],o(n,0,9,r),U=n),e.hasOwnProperty(r="POW_PRECISION")&&(n=e[r],o(n,0,O,r),F=n),e.hasOwnProperty(r="FORMAT")){if(n=e[r],"object"!=typeof n)throw Error(g+r+" not an object: "+n);G=n}if(e.hasOwnProperty(r="ALPHABET")){if(n=e[r],"string"!=typeof n||/^.$|\.|(.).*\1/.test(n))throw Error(g+r+" invalid: "+n);k=n}}return{DECIMAL_PLACES:D,ROUNDING_MODE:T,EXPONENTIAL_AT:[I,B],RANGE:[x,C],CRYPTO:M,MODULO_MODE:U,POW_PRECISION:F,FORMAT:G,ALPHABET:k}},c.isBigNumber=function(e){return e instanceof c||e&&e._isBigNumber===!0||!1},c.maximum=c.max=function(){return b(arguments,_.lt)},c.minimum=c.min=function(){return b(arguments,_.gt)},c.random=function(){var e=9007199254740992,r=Math.random()*e&2097151?function(){return h(Math.random()*e)}:function(){return 8388608*(1073741824*Math.random()|0)+(8388608*Math.random()|0)};return function(e){var n,t,i,s,u,f=0,l=[],p=new c(L);if(null==e?e=D:o(e,0,O),s=a(e/m),M)if(crypto.getRandomValues){for(n=crypto.getRandomValues(new Uint32Array(s*=2));s>f;)u=131072*n[f]+(n[f+1]>>>11),u>=9e15?(t=crypto.getRandomValues(new Uint32Array(2)),n[f]=t[0],n[f+1]=t[1]):(l.push(u%1e14),f+=2);f=s/2}else{if(!crypto.randomBytes)throw M=!1,Error(g+"crypto unavailable");for(n=crypto.randomBytes(s*=7);s>f;)u=281474976710656*(31&n[f])+1099511627776*n[f+1]+4294967296*n[f+2]+16777216*n[f+3]+(n[f+4]<<16)+(n[f+5]<<8)+n[f+6],u>=9e15?crypto.randomBytes(7).copy(n,f):(l.push(u%1e14),f+=7);f=s/7}if(!M)for(;s>f;)u=r(),9e15>u&&(l[f++]=u%1e14);for(s=l[--f],e%=m,s&&e&&(u=v[m-e],l[f]=h(s/u)*u);0===l[f];l.pop(),f--);if(0>f)l=[i=0];else{for(i=-1;0===l[0];l.splice(0,1),i-=m);for(f=1,u=l[0];u>=10;u/=10,f++);m>f&&(i-=m-f)}return p.e=i,p.c=l,p}}(),P=function(){function e(e,r,n,t){for(var i,o,s=[0],u=0,f=e.length;f>u;){for(o=s.length;o--;s[o]*=r);for(s[0]+=t.indexOf(e.charAt(u++)),i=0;i<s.length;i++)s[i]>n-1&&(null==s[i+1]&&(s[i+1]=0),s[i+1]+=s[i]/n|0,s[i]%=n)}return s.reverse()}var r="0123456789";return function(n,i,o,s,u){var l,a,h,g,p,d,m,w,v=n.indexOf("."),N=D,O=T;for(v>=0&&(g=F,F=0,n=n.replace(".",""),w=new c(i),d=w.pow(n.length-v),F=g,w.c=e(f(t(d.c),d.e,"0"),10,o,r),w.e=w.c.length),m=e(n,i,o,u?(l=k,r):(l=r,k)),h=g=m.length;0==m[--g];m.pop());if(!m[0])return l.charAt(0);if(0>v?--h:(d.c=m,d.e=h,d.s=s,d=R(d,w,N,O,o),m=d.c,p=d.r,h=d.e),a=h+N+1,v=m[a],g=o/2,p=p||0>a||null!=m[a+1],p=4>O?(null!=v||p)&&(0==O||O==(d.s<0?3:2)):v>g||v==g&&(4==O||p||6==O&&1&m[a-1]||O==(d.s<0?8:7)),1>a||!m[0])n=p?f(l.charAt(1),-N,l.charAt(0)):l.charAt(0);else{if(m.length=a,p)for(--o;++m[--a]>o;)m[a]=0,a||(++h,m=[1].concat(m));for(g=m.length;!m[--g];);for(v=0,n="";g>=v;n+=l.charAt(m[v++]));n=f(n,h,l.charAt(0))}return n}}(),R=function(){function e(e,r,n){var t,i,o,s,u=0,f=e.length,c=r%N,l=r/N|0;for(e=e.slice();f--;)o=e[f]%N,s=e[f]/N|0,t=l*o+s*c,i=c*o+t%N*N+u,u=(i/n|0)+(t/N|0)+l*s,e[f]=i%n;return u&&(e=[u].concat(e)),e}function r(e,r,n,t){var i,o;if(n!=t)o=n>t?1:-1;else for(i=o=0;n>i;i++)if(e[i]!=r[i]){o=e[i]>r[i]?1:-1;break}return o}function t(e,r,n,t){for(var i=0;n--;)e[n]-=i,i=e[n]<r[n]?1:0,e[n]=i*t+e[n]-r[n];for(;!e[0]&&e.length>1;e.splice(0,1));}return function(i,o,s,u,f){var l,a,g,p,w,v,N,O,y,b,E,R,P,S,_,L,D,T=i.s==o.s?1:-1,I=i.c,B=o.c;if(!(I&&I[0]&&B&&B[0]))return new c(i.s&&o.s&&(I?!B||I[0]!=B[0]:B)?I&&0==I[0]||!B?0*T:T/0:NaN);for(O=new c(T),y=O.c=[],a=i.e-o.e,T=s+a+1,f||(f=d,a=n(i.e/m)-n(o.e/m),T=T/m|0),g=0;B[g]==(I[g]||0);g++);if(B[g]>(I[g]||0)&&a--,0>T)y.push(1),p=!0;else{for(S=I.length,L=B.length,g=0,T+=2,w=h(f/(B[0]+1)),w>1&&(B=e(B,w,f),I=e(I,w,f),L=B.length,S=I.length),P=L,b=I.slice(0,L),E=b.length;L>E;b[E++]=0);D=B.slice(),D=[0].concat(D),_=B[0],B[1]>=f/2&&_++;do{if(w=0,l=r(B,b,L,E),0>l){if(R=b[0],L!=E&&(R=R*f+(b[1]||0)),w=h(R/_),w>1)for(w>=f&&(w=f-1),v=e(B,w,f),N=v.length,E=b.length;1==r(v,b,N,E);)w--,t(v,N>L?D:B,N,f),N=v.length,l=1;else 0==w&&(l=w=1),v=B.slice(),N=v.length;if(E>N&&(v=[0].concat(v)),t(b,v,E,f),E=b.length,-1==l)for(;r(B,b,L,E)<1;)w++,t(b,E>L?D:B,E,f),E=b.length}else 0===l&&(w++,b=[0]);y[g++]=w,b[0]?b[E++]=I[P]||0:(b=[I[P]],E=1)}while((P++<S||null!=b[0])&&T--);p=null!=b[0],y[0]||y.splice(0,1)}if(f==d){for(g=1,T=y[0];T>=10;T/=10,g++);A(O,s+(O.e=g+a*m-1)+1,u,p)}else O.e=a,O.r=+p;return O}}(),S=function(){var e=/^(-?)0([xbo])(?=\w[\w.]*$)/i,r=/^([^.]+)\.$/,n=/^\.([^.]+)$/,t=/^-?(Infinity|NaN)$/,i=/^\s*\+(?=[\w.])|^\s+|\s+$/g;return function(o,s,u,f){var l,a=u?s:s.replace(i,"");if(!t.test(a)){if(!u&&(a=a.replace(e,function(e,r,n){return l="x"==(n=n.toLowerCase())?16:"b"==n?2:8,f&&f!=l?e:r}),f&&(l=f,a=a.replace(r,"$1").replace(n,"0.$1")),s!=a))return new c(a,l);throw Error(g+"Not a"+(f?" base "+f:"")+" number: "+s)}o.s=isNaN(a)?null:0>a?-1:1,o.c=o.e=null}}(),_.absoluteValue=_.abs=function(){var e=new c(this);return e.s<0&&(e.s=1),e},_.comparedTo=function(e,r){return i(this,new c(e,r))},_.decimalPlaces=_.dp=function(e,r){var t,i,s,u=this;if(null!=e)return o(e,0,O),null==r?r=T:o(r,0,8),A(new c(u),e+u.e+1,r);if(!(t=u.c))return null;if(i=((s=t.length-1)-n(this.e/m))*m,s=t[s])for(;s%10==0;s/=10,i--);return 0>i&&(i=0),i},_.dividedBy=_.div=function(e,r){return R(this,new c(e,r),D,T)},_.dividedToIntegerBy=_.idiv=function(e,r){return R(this,new c(e,r),0,1)},_.isEqualTo=_.eq=function(e,r){return 0===i(this,new c(e,r))},_.integerValue=function(e){var r=new c(this);return null==e?e=T:o(e,0,8),A(r,r.e+1,e)},_.isGreaterThan=_.gt=function(e,r){return i(this,new c(e,r))>0},_.isGreaterThanOrEqualTo=_.gte=function(e,r){return 1===(r=i(this,new c(e,r)))||0===r},_.isFinite=function(){return!!this.c},_.isInteger=function(){return!!this.c&&n(this.e/m)>this.c.length-2},_.isNaN=function(){return!this.s},_.isNegative=function(){return this.s<0},_.isPositive=function(){return this.s>0},_.isZero=function(){return!!this.c&&0==this.c[0]},_.isLessThan=_.lt=function(e,r){return i(this,new c(e,r))<0},_.isLessThanOrEqualTo=_.lte=function(e,r){return-1===(r=i(this,new c(e,r)))||0===r},_.minus=function(e,r){var t,i,o,s,u=this,f=u.s;if(e=new c(e,r),r=e.s,!f||!r)return new c(NaN);if(f!=r)return e.s=-r,u.plus(e);var l=u.e/m,a=e.e/m,h=u.c,g=e.c;if(!l||!a){if(!h||!g)return h?(e.s=-r,e):new c(g?u:NaN);if(!h[0]||!g[0])return g[0]?(e.s=-r,e):new c(h[0]?u:3==T?-0:0)}if(l=n(l),a=n(a),h=h.slice(),f=l-a){for((s=0>f)?(f=-f,o=h):(a=l,o=g),o.reverse(),r=f;r--;o.push(0));o.reverse()}else for(i=(s=(f=h.length)<(r=g.length))?f:r,f=r=0;i>r;r++)if(h[r]!=g[r]){s=h[r]<g[r];break}if(s&&(o=h,h=g,g=o,e.s=-e.s),r=(i=g.length)-(t=h.length),r>0)for(;r--;h[t++]=0);for(r=d-1;i>f;){if(h[--i]<g[i]){for(t=i;t&&!h[--t];h[t]=r);--h[t],h[i]+=d}h[i]-=g[i]}for(;0==h[0];h.splice(0,1),--a);return h[0]?E(e,h,a):(e.s=3==T?-1:1,e.c=[e.e=0],e)},_.modulo=_.mod=function(e,r){var n,t,i=this;return e=new c(e,r),!i.c||!e.s||e.c&&!e.c[0]?new c(NaN):!e.c||i.c&&!i.c[0]?new c(i):(9==U?(t=e.s,e.s=1,n=R(i,e,0,3),e.s=t,n.s*=t):n=R(i,e,0,U),i.minus(n.times(e)))},_.multipliedBy=_.times=function(e,r){var t,i,o,s,u,f,l,a,h,g,p,w,v,O,y,b=this,A=b.c,R=(e=new c(e,r)).c;if(!(A&&R&&A[0]&&R[0]))return!b.s||!e.s||A&&!A[0]&&!R||R&&!R[0]&&!A?e.c=e.e=e.s=null:(e.s*=b.s,A&&R?(e.c=[0],e.e=0):e.c=e.e=null),e;for(i=n(b.e/m)+n(e.e/m),e.s*=b.s,l=A.length,g=R.length,g>l&&(v=A,A=R,R=v,o=l,l=g,g=o),o=l+g,v=[];o--;v.push(0));for(O=d,y=N,o=g;--o>=0;){for(t=0,p=R[o]%y,w=R[o]/y|0,u=l,s=o+u;s>o;)a=A[--u]%y,h=A[u]/y|0,f=w*a+h*p,a=p*a+f%y*y+v[s]+t,t=(a/O|0)+(f/y|0)+w*h,v[s--]=a%O;v[s]=t}return t?++i:v.splice(0,1),E(e,v,i)},_.negated=function(){var e=new c(this);return e.s=-e.s||null,e},_.plus=function(e,r){var t,i=this,o=i.s;if(e=new c(e,r),r=e.s,!o||!r)return new c(NaN);if(o!=r)return e.s=-r,i.minus(e);var s=i.e/m,u=e.e/m,f=i.c,l=e.c;if(!s||!u){if(!f||!l)return new c(o/0);if(!f[0]||!l[0])return l[0]?e:new c(f[0]?i:0*o)}if(s=n(s),u=n(u),f=f.slice(),o=s-u){for(o>0?(u=s,t=l):(o=-o,t=f),t.reverse();o--;t.push(0));t.reverse()}for(o=f.length,r=l.length,0>o-r&&(t=l,l=f,f=t,r=o),o=0;r;)o=(f[--r]=f[r]+l[r]+o)/d|0,f[r]=d===f[r]?0:f[r]%d;return o&&(f=[o].concat(f),++u),E(e,f,u)},_.precision=_.sd=function(e,r){var n,t,i,s=this;if(null!=e&&e!==!!e)return o(e,1,O),null==r?r=T:o(r,0,8),A(new c(s),e,r);if(!(n=s.c))return null;if(i=n.length-1,t=i*m+1,i=n[i]){for(;i%10==0;i/=10,t--);for(i=n[0];i>=10;i/=10,t++);}return e&&s.e+1>t&&(t=s.e+1),t},_.shiftedBy=function(e){return o(e,-w,w),this.times("1e"+e)},_.squareRoot=_.sqrt=function(){var e,r,i,o,s,u=this,f=u.c,l=u.s,a=u.e,h=D+4,g=new c("0.5");if(1!==l||!f||!f[0])return new c(!l||0>l&&(!f||f[0])?NaN:f?u:1/0);if(l=Math.sqrt(+u),0==l||l==1/0?(r=t(f),(r.length+a)%2==0&&(r+="0"),l=Math.sqrt(r),a=n((a+1)/2)-(0>a||a%2),l==1/0?r="1e"+a:(r=l.toExponential(),r=r.slice(0,r.indexOf("e")+1)+a),i=new c(r)):i=new c(l+""),i.c[0])for(a=i.e,l=a+h,3>l&&(l=0);;)if(s=i,i=g.times(s.plus(R(u,s,h,1))),t(s.c).slice(0,l)===(r=t(i.c)).slice(0,l)){if(i.e<a&&--l,r=r.slice(l-3,l+1),"9999"!=r&&(o||"4999"!=r)){(!+r||!+r.slice(1)&&"5"==r.charAt(0))&&(A(i,i.e+D+2,1),e=!i.times(i).eq(u));break}if(!o&&(A(s,s.e+D+2,0),s.times(s).eq(u))){i=s;break}h+=4,l+=4,o=1}return A(i,i.e+D+1,T,e)},_.toExponential=function(e,r){return null!=e&&(o(e,0,O),e++),y(this,e,r,1)},_.toFixed=function(e,r){return null!=e&&(o(e,0,O),e=e+this.e+1),y(this,e,r)},_.toFormat=function(e,r){var n=this.toFixed(e,r);if(this.c){var t,i=n.split("."),o=+G.groupSize,s=+G.secondaryGroupSize,u=G.groupSeparator,f=i[0],c=i[1],l=this.s<0,a=l?f.slice(1):f,h=a.length;if(s&&(t=o,o=s,s=t,h-=t),o>0&&h>0){for(t=h%o||o,f=a.substr(0,t);h>t;t+=o)f+=u+a.substr(t,o);s>0&&(f+=u+a.slice(t)),l&&(f="-"+f)}n=c?f+G.decimalSeparator+((s=+G.fractionGroupSize)?c.replace(new RegExp("\\d{"+s+"}\\B","g"),"$&"+G.fractionGroupSeparator):c):f}return n},_.toFraction=function(e){var r,n,i,o,s,u,f,l,a,h,p,d,w=this,N=w.c;if(null!=e&&(l=new c(e),!l.isInteger()||l.lt(L)))throw Error(g+"Argument "+(l.isInteger()?"out of range: ":"not an integer: ")+e);if(!N)return w.toString();for(n=new c(L),h=i=new c(L),o=a=new c(L),d=t(N),u=n.e=d.length-w.e-1,n.c[0]=v[(f=u%m)<0?m+f:f],e=!e||l.comparedTo(n)>0?u>0?n:h:l,f=C,C=1/0,l=new c(d),a.c[0]=0;p=R(l,n,0,1),s=i.plus(p.times(o)),1!=s.comparedTo(e);)i=o,o=s,h=a.plus(p.times(s=h)),a=s,n=l.minus(p.times(s=n)),l=s;return s=R(e.minus(i),o,0,1),a=a.plus(s.times(h)),i=i.plus(s.times(o)),a.s=h.s=w.s,u*=2,r=R(h,o,u,T).minus(w).abs().comparedTo(R(a,i,u,T).minus(w).abs())<1?[h.toString(),o.toString()]:[a.toString(),i.toString()],C=f,r},_.toNumber=function(){return+this},_.exponentiatedBy=_.pow=function(e,r){var n,t,i,s,u=this;for(o(e,-w,w),null!=r&&(r=new c(r)),r?e>1&&u.gt(L)&&u.isInteger()&&r.gt(L)&&r.isInteger()?u=u.mod(r):(s=r,r=null):F&&(t=a(F/m+2)),i=new c(L),n=h(0>e?-e:e);;){if(n%2){if(i=i.times(u),!i.c)break;t?i.c.length>t&&(i.c.length=t):r&&(i=i.mod(r))}if(n=h(n/2),!n)break;u=u.times(u),t?u.c&&u.c.length>t&&(u.c.length=t):r&&(u=u.mod(r))}return r?i:(0>e&&(i=L.div(i)),s?i.mod(s):t?A(i,F,T):i)},_.toPrecision=function(e,r){return null!=e&&o(e,1,O),y(this,e,r,2)},_.toString=function(e){var r,n=this,i=n.s,s=n.e;return null===s?i?(r="Infinity",0>i&&(r="-"+r)):r="NaN":(r=t(n.c),null==e?r=I>=s||s>=B?u(r,s):f(r,s,"0"):(o(e,2,k.length,"Base"),r=P(f(r,s,"0"),10,e,i,!0)),0>i&&n.c[0]&&(r="-"+r)),r},_.valueOf=_.toJSON=function(){var e,r=this,n=r.e;return null===n?r.toString():(e=t(r.c),e=I>=n||n>=B?u(e,n):f(e,n,"0"),r.s<0?"-"+e:e)},_._isBigNumber=!0,null!=e&&c.set(e),c}function n(e){var r=0|e;return e>0||e===r?r:r-1}function t(e){for(var r,n,t=1,i=e.length,o=e[0]+"";i>t;){for(r=e[t++]+"",n=m-r.length;n--;r="0"+r);o+=r}for(i=o.length;48===o.charCodeAt(--i););return o.slice(0,i+1||1)}function i(e,r){var n,t,i=e.c,o=r.c,s=e.s,u=r.s,f=e.e,c=r.e;if(!s||!u)return null;if(n=i&&!i[0],t=o&&!o[0],n||t)return n?t?0:-u:s;if(s!=u)return s;if(n=0>s,t=f==c,!i||!o)return t?0:!i^n?1:-1;if(!t)return f>c^n?1:-1;for(u=(f=i.length)<(c=o.length)?f:c,s=0;u>s;s++)if(i[s]!=o[s])return i[s]>o[s]^n?1:-1;return f==c?0:f>c^n?1:-1}function o(e,r,n,t){if(r>e||e>n||e!==(0>e?a(e):h(e)))throw Error(g+(t||"Argument")+("number"==typeof e?r>e||e>n?" out of range: ":" not an integer: ":" not a primitive number: ")+e)}function s(e){return"[object Array]"==Object.prototype.toString.call(e)}function u(e,r){return(e.length>1?e.charAt(0)+"."+e.slice(1):e)+(0>r?"e":"e+")+r}function f(e,r,n){var t,i;if(0>r){for(i=n+".";++r;i+=n);e=i+e}else if(t=e.length,++r>t){for(i=n,r-=t;--r;i+=n);e+=i}else t>r&&(e=e.slice(0,r)+"."+e.slice(r));return e}var c,l=/^-?(?:\d+(?:\.\d*)?|\.\d+)(?:e[+-]?\d+)?$/i,a=Math.ceil,h=Math.floor,g="[BigNumber Error] ",p=g+"Number primitive has more than 15 significant digits: ",d=1e14,m=14,w=9007199254740991,v=[1,10,100,1e3,1e4,1e5,1e6,1e7,1e8,1e9,1e10,1e11,1e12,1e13],N=1e7,O=1e9;c=r(),c["default"]=c.BigNumber=c,"function"==typeof define&&define.amd?define(function(){return c}):"undefined"!=typeof module&&module.exports?module.exports=c:(e||(e="undefined"!=typeof self?self:Function("return this")()),e.BigNumber=c)}(this);
const newUuidV4 = uuid.v4
const UUID_BASE16 = 16
const PID_BASE36 = 36
const SYMBOLS = '0123456789abcdefghijklmnopqrstuvwxyz'
const RE_UUID_NO_HYPHEN = /^([a-f0-9]{8})([a-f0-9]{4})([a-f0-9]{4})([a-f0-9]{4})([a-f0-9]{12})$/
const RE_UUID_WITH_HYPHEN = '$1-$2-$3-$4-$5'
const create = (uuid = '') => {
if(uuid === '') uuid = newUuidV4()
uuid = uuid.replace(/-/g, '').toLowerCase()
let pid = baseToBase(uuid, UUID_BASE16, PID_BASE36)
return '' + pid.padStart(25, '0')
}
const toUuid = (pid) => {
let uuid = baseToBase(pid, PID_BASE36, UUID_BASE16)
uuid = uuid.padStart(32, '0')
return '' + uuid.replace(RE_UUID_NO_HYPHEN, RE_UUID_WITH_HYPHEN)
}
const baseToBase = (digits, fromBase, toBase) => {
return valueToForm(formToValue(digits, fromBase), toBase);
}
const valueToForm = (value, base) => {
if(!(value instanceof BigNumber)) value = BigNumber(value)
let digits = ''
while(value.comparedTo(0) > 0) {
const quotient = value.idiv(base)
const remainder = value.mod(base)
digits = SYMBOLS[remainder] + digits
value = quotient
}
return digits
}
const formToValue = (digits, base) => {
if(base > PID_BASE36) {
throw new Error(`Base '${base}' exceeds maximum '${PID_BASE36}'.`)
}
const subset = SYMBOLS.substr(0, base)
let value = BigNumber(0)
for(let c of digits){
const index = subset.indexOf(c)
if(index === -1){
throw new Error(`Invalid digit '${c}', not found in '${base}' digits '${subset}'.`)
}
value = value.times(base).plus(index)
}
return value
}
return { create, toUuid }
})()
const testCases = [
'ffffffff-ffff-ffff-ffff-ffffffffffff',
'00000000-0000-0000-0000-000000000000',
'528f4e50-1f26-4067-8c9d-8475a0da3e16',
'275c7d5b-ec7b-4a04-afe0-00e7602747e2',
'e84d040d-5260-4646-bda3-56161f62525e',
'52cf29d6-2b61-4c9d-abaf-9d1e7d676acd',
'f0f64bda-9afe-4443-a0aa-a74298a1e4d1',
'a20a37aa-6a2d-47b3-ad77-56a289e52a8a',
'd927d185-2214-4782-8699-6abb136880af',
'1e17729d-3fdd-4bec-8562-d514da77cff5'
];
const out = document.querySelector('#out')
const log = (...items) => {
out.textContent += items.join(' ') + '\n'
}
const clear = () => {
out.textContent = '';
}
log('Testing...')
log('==========')
let passed = 0
let failed = 0
for(let test of testCases){
let pid = Pid.create(test)
let uuid = Pid.toUuid(pid)
//log(test, pid, uuid)
result = 'PASS'
if(test !== uuid){
result = `FAIL — ${test} !== ${uuid}.`
}
if(pid.length !== 25){
result = `FAIL - `
}
if(uuid.length !== 36){
result = `FAIL — `
}
log(`${result}:`, test, '->', pid, '->', uuid)
if(result === 'PASS') passed++
else failed++
}
log('----------')
log(' Total: ' + (passed + failed))
log('Passed: ' + passed)
log('Failed: ' + failed)
log('\n\n')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment