decimal-eval
Version:
A tiny, safe, fast JavaScript library for decimal arithmetic expressions.
3 lines (2 loc) • 29.4 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).DecimalEval={})}(this,(function(e){"use strict";var t=/^-?(?:\d+(?:\.\d*)?|\.\d+)(?:e[+-]?\d+)?$/i,r=Math.ceil,n=Math.floor,i="[BigNumber Error] ",s=i+"Number primitive has more than 15 significant digits: ",o=1e14,u=14,a=9007199254740991,c=[1,10,100,1e3,1e4,1e5,1e6,1e7,1e8,1e9,1e10,1e11,1e12,1e13],f=1e7,l=1e9;function h(e){var t=0|e;return e>0||e===t?t:t-1}function p(e){for(var t,r,n=1,i=e.length,s=e[0]+"";n<i;){for(t=e[n++]+"",r=u-t.length;r--;t="0"+t);s+=t}for(i=s.length;48===s.charCodeAt(--i););return s.slice(0,i+1||1)}function d(e,t){var r,n,i=e.c,s=t.c,o=e.s,u=t.s,a=e.e,c=t.e;if(!o||!u)return null;if(r=i&&!i[0],n=s&&!s[0],r||n)return r?n?0:-u:o;if(o!=u)return o;if(r=o<0,n=a==c,!i||!s)return n?0:!i^r?1:-1;if(!n)return a>c^r?1:-1;for(u=(a=i.length)<(c=s.length)?a:c,o=0;o<u;o++)if(i[o]!=s[o])return i[o]>s[o]^r?1:-1;return a==c?0:a>c^r?1:-1}function v(e,t,r,s){if(e<t||e>r||e!==n(e))throw Error(i+(s||"Argument")+("number"==typeof e?e<t||e>r?" out of range: ":" not an integer: ":" not a primitive number: ")+String(e))}function g(e){var t=e.c.length-1;return h(e.e/u)==t&&e.c[t]%2!=0}function w(e,t){return(e.length>1?e.charAt(0)+"."+e.slice(1):e)+(t<0?"e":"e+")+t}function y(e,t,r){var n,i;if(t<0){for(i=r+".";++t;i+=r);e=i+e}else if(++t>(n=e.length)){for(i=r,t-=n;--t;i+=r);e+=i}else t<n&&(e=e.slice(0,t)+"."+e.slice(t));return e}var m=function e(m){var x,b,k,E,N,T,O,A,P,S,B=q.prototype={constructor:q,toString:null,valueOf:null},C=new q(1),U=20,L=4,R=-7,_=21,D=-1e7,I=1e7,M=!1,j=1,F=0,G={prefix:"",groupSize:3,secondaryGroupSize:0,groupSeparator:",",decimalSeparator:".",fractionGroupSize:0,fractionGroupSeparator:" ",suffix:""},V="0123456789abcdefghijklmnopqrstuvwxyz";function q(e,r){var i,o,c,f,l,h,p,d,g=this;if(!(g instanceof q))return new q(e,r);if(null==r){if(e&&!0===e._isBigNumber)return g.s=e.s,void(!e.c||e.e>I?g.c=g.e=null:e.e<D?g.c=[g.e=0]:(g.e=e.e,g.c=e.c.slice()));if((h="number"==typeof e)&&0*e==0){if(g.s=1/e<0?(e=-e,-1):1,e===~~e){for(f=0,l=e;l>=10;l/=10,f++);return void(f>I?g.c=g.e=null:(g.e=f,g.c=[e]))}d=String(e)}else{if(!t.test(d=String(e)))return k(g,d,h);g.s=45==d.charCodeAt(0)?(d=d.slice(1),-1):1}(f=d.indexOf("."))>-1&&(d=d.replace(".","")),(l=d.search(/e/i))>0?(f<0&&(f=l),f+=+d.slice(l+1),d=d.substring(0,l)):f<0&&(f=d.length)}else{if(v(r,2,V.length,"Base"),10==r)return W(g=new q(e),U+g.e+1,L);if(d=String(e),h="number"==typeof e){if(0*e!=0)return k(g,d,h,r);if(g.s=1/e<0?(d=d.slice(1),-1):1,q.DEBUG&&d.replace(/^0\.0*|\./,"").length>15)throw Error(s+e)}else g.s=45===d.charCodeAt(0)?(d=d.slice(1),-1):1;for(i=V.slice(0,r),f=l=0,p=d.length;l<p;l++)if(i.indexOf(o=d.charAt(l))<0){if("."==o){if(l>f){f=p;continue}}else if(!c&&(d==d.toUpperCase()&&(d=d.toLowerCase())||d==d.toLowerCase()&&(d=d.toUpperCase()))){c=!0,l=-1,f=0;continue}return k(g,String(e),h,r)}h=!1,(f=(d=b(d,r,10,g.s)).indexOf("."))>-1?d=d.replace(".",""):f=d.length}for(l=0;48===d.charCodeAt(l);l++);for(p=d.length;48===d.charCodeAt(--p););if(d=d.slice(l,++p)){if(p-=l,h&&q.DEBUG&&p>15&&(e>a||e!==n(e)))throw Error(s+g.s*e);if((f=f-l-1)>I)g.c=g.e=null;else if(f<D)g.c=[g.e=0];else{if(g.e=f,g.c=[],l=(f+1)%u,f<0&&(l+=u),l<p){for(l&&g.c.push(+d.slice(0,l)),p-=u;l<p;)g.c.push(+d.slice(l,l+=u));l=u-(d=d.slice(l)).length}else l-=p;for(;l--;d+="0");g.c.push(+d)}}else g.c=[g.e=0]}function $(e,t,r,n){var i,s,o,u,a;if(null==r?r=L:v(r,0,8),!e.c)return e.toString();if(i=e.c[0],o=e.e,null==t)a=p(e.c),a=1==n||2==n&&(o<=R||o>=_)?w(a,o):y(a,o,"0");else if(s=(e=W(new q(e),t,r)).e,u=(a=p(e.c)).length,1==n||2==n&&(t<=s||s<=R)){for(;u<t;a+="0",u++);a=w(a,s)}else if(t-=o,a=y(a,s,"0"),s+1>u){if(--t>0)for(a+=".";t--;a+="0");}else if((t+=s-u)>0)for(s+1==u&&(a+=".");t--;a+="0");return e.s<0&&i?"-"+a:a}function z(e,t){for(var r,n=1,i=new q(e[0]);n<e.length;n++){if(!(r=new q(e[n])).s){i=r;break}t.call(i,r)&&(i=r)}return i}function H(e,t,r){for(var n=1,i=t.length;!t[--i];t.pop());for(i=t[0];i>=10;i/=10,n++);return(r=n+r*u-1)>I?e.c=e.e=null:r<D?e.c=[e.e=0]:(e.e=r,e.c=t),e}function W(e,t,i,s){var a,f,l,h,p,d,v,g=e.c,w=c;if(g){e:{for(a=1,h=g[0];h>=10;h/=10,a++);if((f=t-a)<0)f+=u,l=t,v=(p=g[d=0])/w[a-l-1]%10|0;else if((d=r((f+1)/u))>=g.length){if(!s)break e;for(;g.length<=d;g.push(0));p=v=0,a=1,l=(f%=u)-u+1}else{for(p=h=g[d],a=1;h>=10;h/=10,a++);v=(l=(f%=u)-u+a)<0?0:p/w[a-l-1]%10|0}if(s=s||t<0||null!=g[d+1]||(l<0?p:p%w[a-l-1]),s=i<4?(v||s)&&(0==i||i==(e.s<0?3:2)):v>5||5==v&&(4==i||s||6==i&&(f>0?l>0?p/w[a-l]:0:g[d-1])%10&1||i==(e.s<0?8:7)),t<1||!g[0])return g.length=0,s?(t-=e.e+1,g[0]=w[(u-t%u)%u],e.e=-t||0):g[0]=e.e=0,e;if(0==f?(g.length=d,h=1,d--):(g.length=d+1,h=w[u-f],g[d]=l>0?n(p/w[a-l]%w[l])*h:0),s)for(;;){if(0==d){for(f=1,l=g[0];l>=10;l/=10,f++);for(l=g[0]+=h,h=1;l>=10;l/=10,h++);f!=h&&(e.e++,g[0]==o&&(g[0]=1));break}if(g[d]+=h,g[d]!=o)break;g[d--]=0,h=1}for(f=g.length;0===g[--f];g.pop());}e.e>I?e.c=e.e=null:e.e<D&&(e.c=[e.e=0])}return e}function X(e){var t,r=e.e;return null===r?e.toString():(t=p(e.c),t=r<=R||r>=_?w(t,r):y(t,r,"0"),e.s<0?"-"+t:t)}return q.clone=e,q.ROUND_UP=0,q.ROUND_DOWN=1,q.ROUND_CEIL=2,q.ROUND_FLOOR=3,q.ROUND_HALF_UP=4,q.ROUND_HALF_DOWN=5,q.ROUND_HALF_EVEN=6,q.ROUND_HALF_CEIL=7,q.ROUND_HALF_FLOOR=8,q.EUCLID=9,q.config=q.set=function(e){var t,r;if(null!=e){if("object"!=typeof e)throw Error(i+"Object expected: "+e);if(e.hasOwnProperty(t="DECIMAL_PLACES")&&(v(r=e[t],0,l,t),U=r),e.hasOwnProperty(t="ROUNDING_MODE")&&(v(r=e[t],0,8,t),L=r),e.hasOwnProperty(t="EXPONENTIAL_AT")&&((r=e[t])&&r.pop?(v(r[0],-l,0,t),v(r[1],0,l,t),R=r[0],_=r[1]):(v(r,-l,l,t),R=-(_=r<0?-r:r))),e.hasOwnProperty(t="RANGE"))if((r=e[t])&&r.pop)v(r[0],-l,-1,t),v(r[1],1,l,t),D=r[0],I=r[1];else{if(v(r,-l,l,t),!r)throw Error(i+t+" cannot be zero: "+r);D=-(I=r<0?-r:r)}if(e.hasOwnProperty(t="CRYPTO")){if((r=e[t])!==!!r)throw Error(i+t+" not true or false: "+r);if(r){if("undefined"==typeof crypto||!crypto||!crypto.getRandomValues&&!crypto.randomBytes)throw M=!r,Error(i+"crypto unavailable");M=r}else M=r}if(e.hasOwnProperty(t="MODULO_MODE")&&(v(r=e[t],0,9,t),j=r),e.hasOwnProperty(t="POW_PRECISION")&&(v(r=e[t],0,l,t),F=r),e.hasOwnProperty(t="FORMAT")){if("object"!=typeof(r=e[t]))throw Error(i+t+" not an object: "+r);G=r}if(e.hasOwnProperty(t="ALPHABET")){if("string"!=typeof(r=e[t])||/^.$|[+-.\s]|(.).*\1/.test(r))throw Error(i+t+" invalid: "+r);V=r}}return{DECIMAL_PLACES:U,ROUNDING_MODE:L,EXPONENTIAL_AT:[R,_],RANGE:[D,I],CRYPTO:M,MODULO_MODE:j,POW_PRECISION:F,FORMAT:G,ALPHABET:V}},q.isBigNumber=function(e){if(!e||!0!==e._isBigNumber)return!1;if(!q.DEBUG)return!0;var t,r,s=e.c,a=e.e,c=e.s;e:if("[object Array]"=={}.toString.call(s)){if((1===c||-1===c)&&a>=-l&&a<=l&&a===n(a)){if(0===s[0]){if(0===a&&1===s.length)return!0;break e}if((t=(a+1)%u)<1&&(t+=u),String(s[0]).length==t){for(t=0;t<s.length;t++)if((r=s[t])<0||r>=o||r!==n(r))break e;if(0!==r)return!0}}}else if(null===s&&null===a&&(null===c||1===c||-1===c))return!0;throw Error(i+"Invalid BigNumber: "+e)},q.maximum=q.max=function(){return z(arguments,B.lt)},q.minimum=q.min=function(){return z(arguments,B.gt)},q.random=(E=9007199254740992,N=Math.random()*E&2097151?function(){return n(Math.random()*E)}:function(){return 8388608*(1073741824*Math.random()|0)+(8388608*Math.random()|0)},function(e){var t,s,o,a,f,h=0,p=[],d=new q(C);if(null==e?e=U:v(e,0,l),a=r(e/u),M)if(crypto.getRandomValues){for(t=crypto.getRandomValues(new Uint32Array(a*=2));h<a;)(f=131072*t[h]+(t[h+1]>>>11))>=9e15?(s=crypto.getRandomValues(new Uint32Array(2)),t[h]=s[0],t[h+1]=s[1]):(p.push(f%1e14),h+=2);h=a/2}else{if(!crypto.randomBytes)throw M=!1,Error(i+"crypto unavailable");for(t=crypto.randomBytes(a*=7);h<a;)(f=281474976710656*(31&t[h])+1099511627776*t[h+1]+4294967296*t[h+2]+16777216*t[h+3]+(t[h+4]<<16)+(t[h+5]<<8)+t[h+6])>=9e15?crypto.randomBytes(7).copy(t,h):(p.push(f%1e14),h+=7);h=a/7}if(!M)for(;h<a;)(f=N())<9e15&&(p[h++]=f%1e14);for(a=p[--h],e%=u,a&&e&&(f=c[u-e],p[h]=n(a/f)*f);0===p[h];p.pop(),h--);if(h<0)p=[o=0];else{for(o=-1;0===p[0];p.splice(0,1),o-=u);for(h=1,f=p[0];f>=10;f/=10,h++);h<u&&(o-=u-h)}return d.e=o,d.c=p,d}),q.sum=function(){for(var e=1,t=arguments,r=new q(t[0]);e<t.length;)r=r.plus(t[e++]);return r},b=function(){var e="0123456789";function t(e,t,r,n){for(var i,s,o=[0],u=0,a=e.length;u<a;){for(s=o.length;s--;o[s]*=t);for(o[0]+=n.indexOf(e.charAt(u++)),i=0;i<o.length;i++)o[i]>r-1&&(null==o[i+1]&&(o[i+1]=0),o[i+1]+=o[i]/r|0,o[i]%=r)}return o.reverse()}return function(r,n,i,s,o){var u,a,c,f,l,h,d,v,g=r.indexOf("."),w=U,m=L;for(g>=0&&(f=F,F=0,r=r.replace(".",""),h=(v=new q(n)).pow(r.length-g),F=f,v.c=t(y(p(h.c),h.e,"0"),10,i,e),v.e=v.c.length),c=f=(d=t(r,n,i,o?(u=V,e):(u=e,V))).length;0==d[--f];d.pop());if(!d[0])return u.charAt(0);if(g<0?--c:(h.c=d,h.e=c,h.s=s,d=(h=x(h,v,w,m,i)).c,l=h.r,c=h.e),g=d[a=c+w+1],f=i/2,l=l||a<0||null!=d[a+1],l=m<4?(null!=g||l)&&(0==m||m==(h.s<0?3:2)):g>f||g==f&&(4==m||l||6==m&&1&d[a-1]||m==(h.s<0?8:7)),a<1||!d[0])r=l?y(u.charAt(1),-w,u.charAt(0)):u.charAt(0);else{if(d.length=a,l)for(--i;++d[--a]>i;)d[a]=0,a||(++c,d=[1].concat(d));for(f=d.length;!d[--f];);for(g=0,r="";g<=f;r+=u.charAt(d[g++]));r=y(r,c,u.charAt(0))}return r}}(),x=function(){function e(e,t,r){var n,i,s,o,u=0,a=e.length,c=t%f,l=t/f|0;for(e=e.slice();a--;)u=((i=c*(s=e[a]%f)+(n=l*s+(o=e[a]/f|0)*c)%f*f+u)/r|0)+(n/f|0)+l*o,e[a]=i%r;return u&&(e=[u].concat(e)),e}function t(e,t,r,n){var i,s;if(r!=n)s=r>n?1:-1;else for(i=s=0;i<r;i++)if(e[i]!=t[i]){s=e[i]>t[i]?1:-1;break}return s}function r(e,t,r,n){for(var i=0;r--;)e[r]-=i,i=e[r]<t[r]?1:0,e[r]=i*n+e[r]-t[r];for(;!e[0]&&e.length>1;e.splice(0,1));}return function(i,s,a,c,f){var l,p,d,v,g,w,y,m,x,b,k,E,N,T,O,A,P,S=i.s==s.s?1:-1,B=i.c,C=s.c;if(!(B&&B[0]&&C&&C[0]))return new q(i.s&&s.s&&(B?!C||B[0]!=C[0]:C)?B&&0==B[0]||!C?0*S:S/0:NaN);for(x=(m=new q(S)).c=[],S=a+(p=i.e-s.e)+1,f||(f=o,p=h(i.e/u)-h(s.e/u),S=S/u|0),d=0;C[d]==(B[d]||0);d++);if(C[d]>(B[d]||0)&&p--,S<0)x.push(1),v=!0;else{for(T=B.length,A=C.length,d=0,S+=2,(g=n(f/(C[0]+1)))>1&&(C=e(C,g,f),B=e(B,g,f),A=C.length,T=B.length),N=A,k=(b=B.slice(0,A)).length;k<A;b[k++]=0);P=C.slice(),P=[0].concat(P),O=C[0],C[1]>=f/2&&O++;do{if(g=0,(l=t(C,b,A,k))<0){if(E=b[0],A!=k&&(E=E*f+(b[1]||0)),(g=n(E/O))>1)for(g>=f&&(g=f-1),y=(w=e(C,g,f)).length,k=b.length;1==t(w,b,y,k);)g--,r(w,A<y?P:C,y,f),y=w.length,l=1;else 0==g&&(l=g=1),y=(w=C.slice()).length;if(y<k&&(w=[0].concat(w)),r(b,w,k,f),k=b.length,-1==l)for(;t(C,b,A,k)<1;)g++,r(b,A<k?P:C,k,f),k=b.length}else 0===l&&(g++,b=[0]);x[d++]=g,b[0]?b[k++]=B[N]||0:(b=[B[N]],k=1)}while((N++<T||null!=b[0])&&S--);v=null!=b[0],x[0]||x.splice(0,1)}if(f==o){for(d=1,S=x[0];S>=10;S/=10,d++);W(m,a+(m.e=d+p*u-1)+1,c,v)}else m.e=p,m.r=+v;return m}}(),T=/^(-?)0([xbo])(?=\w[\w.]*$)/i,O=/^([^.]+)\.$/,A=/^\.([^.]+)$/,P=/^-?(Infinity|NaN)$/,S=/^\s*\+(?=[\w.])|^\s+|\s+$/g,k=function(e,t,r,n){var s,o=r?t:t.replace(S,"");if(P.test(o))e.s=isNaN(o)?null:o<0?-1:1;else{if(!r&&(o=o.replace(T,(function(e,t,r){return s="x"==(r=r.toLowerCase())?16:"b"==r?2:8,n&&n!=s?e:t})),n&&(s=n,o=o.replace(O,"$1").replace(A,"0.$1")),t!=o))return new q(o,s);if(q.DEBUG)throw Error(i+"Not a"+(n?" base "+n:"")+" number: "+t);e.s=null}e.c=e.e=null},B.absoluteValue=B.abs=function(){var e=new q(this);return e.s<0&&(e.s=1),e},B.comparedTo=function(e,t){return d(this,new q(e,t))},B.decimalPlaces=B.dp=function(e,t){var r,n,i,s=this;if(null!=e)return v(e,0,l),null==t?t=L:v(t,0,8),W(new q(s),e+s.e+1,t);if(!(r=s.c))return null;if(n=((i=r.length-1)-h(this.e/u))*u,i=r[i])for(;i%10==0;i/=10,n--);return n<0&&(n=0),n},B.dividedBy=B.div=function(e,t){return x(this,new q(e,t),U,L)},B.dividedToIntegerBy=B.idiv=function(e,t){return x(this,new q(e,t),0,1)},B.exponentiatedBy=B.pow=function(e,t){var s,o,a,c,f,l,h,p,d=this;if((e=new q(e)).c&&!e.isInteger())throw Error(i+"Exponent not an integer: "+X(e));if(null!=t&&(t=new q(t)),f=e.e>14,!d.c||!d.c[0]||1==d.c[0]&&!d.e&&1==d.c.length||!e.c||!e.c[0])return p=new q(Math.pow(+X(d),f?2-g(e):+X(e))),t?p.mod(t):p;if(l=e.s<0,t){if(t.c?!t.c[0]:!t.s)return new q(NaN);(o=!l&&d.isInteger()&&t.isInteger())&&(d=d.mod(t))}else{if(e.e>9&&(d.e>0||d.e<-1||(0==d.e?d.c[0]>1||f&&d.c[1]>=24e7:d.c[0]<8e13||f&&d.c[0]<=9999975e7)))return c=d.s<0&&g(e)?-0:0,d.e>-1&&(c=1/c),new q(l?1/c:c);F&&(c=r(F/u+2))}for(f?(s=new q(.5),l&&(e.s=1),h=g(e)):h=(a=Math.abs(+X(e)))%2,p=new q(C);;){if(h){if(!(p=p.times(d)).c)break;c?p.c.length>c&&(p.c.length=c):o&&(p=p.mod(t))}if(a){if(0===(a=n(a/2)))break;h=a%2}else if(W(e=e.times(s),e.e+1,1),e.e>14)h=g(e);else{if(0===(a=+X(e)))break;h=a%2}d=d.times(d),c?d.c&&d.c.length>c&&(d.c.length=c):o&&(d=d.mod(t))}return o?p:(l&&(p=C.div(p)),t?p.mod(t):c?W(p,F,L,undefined):p)},B.integerValue=function(e){var t=new q(this);return null==e?e=L:v(e,0,8),W(t,t.e+1,e)},B.isEqualTo=B.eq=function(e,t){return 0===d(this,new q(e,t))},B.isFinite=function(){return!!this.c},B.isGreaterThan=B.gt=function(e,t){return d(this,new q(e,t))>0},B.isGreaterThanOrEqualTo=B.gte=function(e,t){return 1===(t=d(this,new q(e,t)))||0===t},B.isInteger=function(){return!!this.c&&h(this.e/u)>this.c.length-2},B.isLessThan=B.lt=function(e,t){return d(this,new q(e,t))<0},B.isLessThanOrEqualTo=B.lte=function(e,t){return-1===(t=d(this,new q(e,t)))||0===t},B.isNaN=function(){return!this.s},B.isNegative=function(){return this.s<0},B.isPositive=function(){return this.s>0},B.isZero=function(){return!!this.c&&0==this.c[0]},B.minus=function(e,t){var r,n,i,s,a=this,c=a.s;if(t=(e=new q(e,t)).s,!c||!t)return new q(NaN);if(c!=t)return e.s=-t,a.plus(e);var f=a.e/u,l=e.e/u,p=a.c,d=e.c;if(!f||!l){if(!p||!d)return p?(e.s=-t,e):new q(d?a:NaN);if(!p[0]||!d[0])return d[0]?(e.s=-t,e):new q(p[0]?a:3==L?-0:0)}if(f=h(f),l=h(l),p=p.slice(),c=f-l){for((s=c<0)?(c=-c,i=p):(l=f,i=d),i.reverse(),t=c;t--;i.push(0));i.reverse()}else for(n=(s=(c=p.length)<(t=d.length))?c:t,c=t=0;t<n;t++)if(p[t]!=d[t]){s=p[t]<d[t];break}if(s&&(i=p,p=d,d=i,e.s=-e.s),(t=(n=d.length)-(r=p.length))>0)for(;t--;p[r++]=0);for(t=o-1;n>c;){if(p[--n]<d[n]){for(r=n;r&&!p[--r];p[r]=t);--p[r],p[n]+=o}p[n]-=d[n]}for(;0==p[0];p.splice(0,1),--l);return p[0]?H(e,p,l):(e.s=3==L?-1:1,e.c=[e.e=0],e)},B.modulo=B.mod=function(e,t){var r,n,i=this;return e=new q(e,t),!i.c||!e.s||e.c&&!e.c[0]?new q(NaN):!e.c||i.c&&!i.c[0]?new q(i):(9==j?(n=e.s,e.s=1,r=x(i,e,0,3),e.s=n,r.s*=n):r=x(i,e,0,j),(e=i.minus(r.times(e))).c[0]||1!=j||(e.s=i.s),e)},B.multipliedBy=B.times=function(e,t){var r,n,i,s,a,c,l,p,d,v,g,w,y,m,x,b=this,k=b.c,E=(e=new q(e,t)).c;if(!(k&&E&&k[0]&&E[0]))return!b.s||!e.s||k&&!k[0]&&!E||E&&!E[0]&&!k?e.c=e.e=e.s=null:(e.s*=b.s,k&&E?(e.c=[0],e.e=0):e.c=e.e=null),e;for(n=h(b.e/u)+h(e.e/u),e.s*=b.s,(l=k.length)<(v=E.length)&&(y=k,k=E,E=y,i=l,l=v,v=i),i=l+v,y=[];i--;y.push(0));for(m=o,x=f,i=v;--i>=0;){for(r=0,g=E[i]%x,w=E[i]/x|0,s=i+(a=l);s>i;)r=((p=g*(p=k[--a]%x)+(c=w*p+(d=k[a]/x|0)*g)%x*x+y[s]+r)/m|0)+(c/x|0)+w*d,y[s--]=p%m;y[s]=r}return r?++n:y.splice(0,1),H(e,y,n)},B.negated=function(){var e=new q(this);return e.s=-e.s||null,e},B.plus=function(e,t){var r,n=this,i=n.s;if(t=(e=new q(e,t)).s,!i||!t)return new q(NaN);if(i!=t)return e.s=-t,n.minus(e);var s=n.e/u,a=e.e/u,c=n.c,f=e.c;if(!s||!a){if(!c||!f)return new q(i/0);if(!c[0]||!f[0])return f[0]?e:new q(c[0]?n:0*i)}if(s=h(s),a=h(a),c=c.slice(),i=s-a){for(i>0?(a=s,r=f):(i=-i,r=c),r.reverse();i--;r.push(0));r.reverse()}for((i=c.length)-(t=f.length)<0&&(r=f,f=c,c=r,t=i),i=0;t;)i=(c[--t]=c[t]+f[t]+i)/o|0,c[t]=o===c[t]?0:c[t]%o;return i&&(c=[i].concat(c),++a),H(e,c,a)},B.precision=B.sd=function(e,t){var r,n,i,s=this;if(null!=e&&e!==!!e)return v(e,1,l),null==t?t=L:v(t,0,8),W(new q(s),e,t);if(!(r=s.c))return null;if(n=(i=r.length-1)*u+1,i=r[i]){for(;i%10==0;i/=10,n--);for(i=r[0];i>=10;i/=10,n++);}return e&&s.e+1>n&&(n=s.e+1),n},B.shiftedBy=function(e){return v(e,-9007199254740991,a),this.times("1e"+e)},B.squareRoot=B.sqrt=function(){var e,t,r,n,i,s=this,o=s.c,u=s.s,a=s.e,c=U+4,f=new q("0.5");if(1!==u||!o||!o[0])return new q(!u||u<0&&(!o||o[0])?NaN:o?s:1/0);if(0==(u=Math.sqrt(+X(s)))||u==1/0?(((t=p(o)).length+a)%2==0&&(t+="0"),u=Math.sqrt(+t),a=h((a+1)/2)-(a<0||a%2),r=new q(t=u==1/0?"5e"+a:(t=u.toExponential()).slice(0,t.indexOf("e")+1)+a)):r=new q(u+""),r.c[0])for((u=(a=r.e)+c)<3&&(u=0);;)if(i=r,r=f.times(i.plus(x(s,i,c,1))),p(i.c).slice(0,u)===(t=p(r.c)).slice(0,u)){if(r.e<a&&--u,"9999"!=(t=t.slice(u-3,u+1))&&(n||"4999"!=t)){+t&&(+t.slice(1)||"5"!=t.charAt(0))||(W(r,r.e+U+2,1),e=!r.times(r).eq(s));break}if(!n&&(W(i,i.e+U+2,0),i.times(i).eq(s))){r=i;break}c+=4,u+=4,n=1}return W(r,r.e+U+1,L,e)},B.toExponential=function(e,t){return null!=e&&(v(e,0,l),e++),$(this,e,t,1)},B.toFixed=function(e,t){return null!=e&&(v(e,0,l),e=e+this.e+1),$(this,e,t)},B.toFormat=function(e,t,r){var n,s=this;if(null==r)null!=e&&t&&"object"==typeof t?(r=t,t=null):e&&"object"==typeof e?(r=e,e=t=null):r=G;else if("object"!=typeof r)throw Error(i+"Argument not an object: "+r);if(n=s.toFixed(e,t),s.c){var o,u=n.split("."),a=+r.groupSize,c=+r.secondaryGroupSize,f=r.groupSeparator||"",l=u[0],h=u[1],p=s.s<0,d=p?l.slice(1):l,v=d.length;if(c&&(o=a,a=c,c=o,v-=o),a>0&&v>0){for(o=v%a||a,l=d.substr(0,o);o<v;o+=a)l+=f+d.substr(o,a);c>0&&(l+=f+d.slice(o)),p&&(l="-"+l)}n=h?l+(r.decimalSeparator||"")+((c=+r.fractionGroupSize)?h.replace(new RegExp("\\d{"+c+"}\\B","g"),"$&"+(r.fractionGroupSeparator||"")):h):l}return(r.prefix||"")+n+(r.suffix||"")},B.toFraction=function(e){var t,r,n,s,o,a,f,l,h,d,v,g,w=this,y=w.c;if(null!=e&&(!(f=new q(e)).isInteger()&&(f.c||1!==f.s)||f.lt(C)))throw Error(i+"Argument "+(f.isInteger()?"out of range: ":"not an integer: ")+X(f));if(!y)return new q(w);for(t=new q(C),h=r=new q(C),n=l=new q(C),g=p(y),o=t.e=g.length-w.e-1,t.c[0]=c[(a=o%u)<0?u+a:a],e=!e||f.comparedTo(t)>0?o>0?t:h:f,a=I,I=1/0,f=new q(g),l.c[0]=0;d=x(f,t,0,1),1!=(s=r.plus(d.times(n))).comparedTo(e);)r=n,n=s,h=l.plus(d.times(s=h)),l=s,t=f.minus(d.times(s=t)),f=s;return s=x(e.minus(r),n,0,1),l=l.plus(s.times(h)),r=r.plus(s.times(n)),l.s=h.s=w.s,v=x(h,n,o*=2,L).minus(w).abs().comparedTo(x(l,r,o,L).minus(w).abs())<1?[h,n]:[l,r],I=a,v},B.toNumber=function(){return+X(this)},B.toPrecision=function(e,t){return null!=e&&v(e,1,l),$(this,e,t,2)},B.toString=function(e){var t,r=this,n=r.s,i=r.e;return null===i?n?(t="Infinity",n<0&&(t="-"+t)):t="NaN":(null==e?t=i<=R||i>=_?w(p(r.c),i):y(p(r.c),i,"0"):10===e?t=y(p((r=W(new q(r),U+i+1,L)).c),r.e,"0"):(v(e,2,V.length,"Base"),t=b(y(p(r.c),i,"0"),10,e,n,!0)),n<0&&r.c[0]&&(t="-"+t)),t},B.valueOf=B.toJSON=function(){return X(this)},B._isBigNumber=!0,B[Symbol.toStringTag]="BigNumber",B[Symbol.for("nodejs.util.inspect.custom")]=B.valueOf,null!=m&&q.set(m),q}();function x(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function b(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}function k(e,t,r){return t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}var E=function e(t){var r,n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};x(this,e),k(this,"label",void 0),k(this,"isBinary",void 0),k(this,"isPrefix",void 0),k(this,"precedence",void 0),k(this,"updateContext",void 0),this.label=t,this.isBinary=!!n.isBinary,this.isPrefix=!!n.isPrefix,this.precedence=null!==(r=n.precedence)&&void 0!==r?r:-1},N={start:new E("start"),end:new E("end"),parenL:new E("("),parenR:new E(")"),numeric:new E("numeric"),identifier:new E("identifier"),plus:new E("+",{isBinary:!0,precedence:13}),minus:new E("-",{isBinary:!0,precedence:13}),times:new E("*",{isBinary:!0,precedence:14}),div:new E("/",{isBinary:!0,precedence:14}),prefixPlus:new E("+",{isPrefix:!0,precedence:16}),prefixMinus:new E("-",{isPrefix:!0,precedence:16})};N.parenL.updateContext=function(){this.allowPrefix=!0},N.parenR.updateContext=function(){this.allowPrefix=!1},N.numeric.updateContext=function(){this.allowPrefix=!1},N.identifier.updateContext=function(){this.allowPrefix=!1};var T=function e(t){x(this,e),k(this,"type",void 0),k(this,"start",void 0),k(this,"end",void 0),this.type="",this.start=t,this.end=t};function O(e){return e>=48&&e<=57}function A(e){return 46===e||O(e)}function P(e){return e>=65&&e<=90||e>=97&&e<=122}function S(e){return!!P(e)||(!!O(e)||95===e)}var B=["+","-","*","/","(",")"],C=[];function U(e,t){if("string"!=typeof e||!/^\S+$/.test(e))throw new Error("The custom operator should be a non-empty string");if(B.includes(e))throw new Error("The custom operator cannot use reserved character, including: ".concat(B.join(", ")));if(A(e.charCodeAt(0)))throw new Error("The custom operator cannot start with a possible number, including: `.`, 0-9");if(63===e.charCodeAt(0))throw new Error("The custom operator cannot start with `?`");if(null!=t&&("number"!=typeof t||t<0))throw new Error("The precedence should be a number greater than 0")}var L=function e(t,r,n){var i=arguments.length>3&&void 0!==arguments[3]&&arguments[3];x(this,e),k(this,"value",void 0),k(this,"codes",void 0),k(this,"type",void 0),k(this,"calc",void 0),this.value=t,this.codes=t.split("").map((function(e,r){return t.charCodeAt(r)})),this.type=new E(t,{isBinary:!i,isPrefix:i,precedence:r}),this.calc=n};var R={"+":function(e,t){return String(Number(e)+Number(t))},"-":function(e,t){return String(Number(e)-Number(t))},"*":function(e,t){return String(Number(e)*Number(t))},"/":function(e,t){return String(Number(e)/Number(t))}};function _(e,t,r){switch(r){case"+":case"-":case"*":case"/":return R[r](e,t);default:for(var n=0;n<C.length;n++){var i=C[n];if(i.type.isBinary&&i.value===r)return i.calc(e,t)}}throw new Error("Unexpected binary operator: ".concat(r))}function D(e,t){switch(t){case"+":return e;case"-":return String(-e);default:for(var r=0;r<C.length;r++){var n=C[r];if(n.type.isPrefix&&n.value===t)return n.calc(e)}}throw new Error("Unexpected unary operator: ".concat(t))}function I(e){var t,r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(e instanceof T)switch(e.type){case"Expression":return I(e.expression,r);case"BinaryExpression":return _(I(e.left,r),I(e.right,r),e.operator);case"UnaryExpression":return D(I(e.argument,r),e.operator);case"NumericLiteral":return e.value;case"Identifier":if(void 0===(t=r[e.name]))throw new Error("The scope name `".concat(e.name,"` is not initialized"));return String(t);default:throw new Error("Unexpected type: ".concat(e.type))}return e}var M=function(){function e(t){x(this,e),k(this,"input",void 0),k(this,"node",void 0),k(this,"pos",void 0),k(this,"tokenType",void 0),k(this,"value",void 0),k(this,"start",void 0),k(this,"end",void 0),k(this,"lastTokenStart",void 0),k(this,"lastTokenEnd",void 0),k(this,"allowPrefix",void 0),this.input=String(t),this.tokenType=N.start,this.value="",this.pos=0,this.start=0,this.end=0,this.lastTokenStart=0,this.lastTokenEnd=0,this.allowPrefix=!0}var t,r,n;return t=e,n=[{key:"evaluate",value:function(t,r){return new e(t).compile()(r)}}],(r=[{key:"compile",value:function(){var e=this.node;return void 0===e&&(e=this.parse()),function(t){return e?I(e,null!=t?t:{}):"0"}}},{key:"parse",value:function(){this.next();var e=this.startNode(this.start);return this.tokenType===N.end?this.node=null:(e.expression=this.parseExpression(),this.tokenType!==N.end&&this.unexpected(this.value),this.node=this.finishNode(e,"Expression"))}},{key:"parseExpression",value:function(){return this.parseExprAtom(this.start,-1)}},{key:"parseExprAtom",value:function(e,t){if(this.tokenType===N.parenL){this.next();var r=this.parseExprAtom(this.start,-1);return this.expect(N.parenR),this.parseExprOp(r,e,t)}var n=this.parseMaybeUnary(t);return this.parseExprOp(n,e,t)}},{key:"parseExprOp",value:function(e,t,r){var n=this.tokenType.precedence;if(this.tokenType.isBinary&&n>r){var i=this.startNode(t),s=this.value;this.next();var o=this.start,u=this.parseExprAtom(o,n),a=this.parseExprOp(u,o,n);return i.left=e,i.operator=s,i.right=a,this.finishNode(i,"BinaryExpression"),this.parseExprOp(i,t,r)}return e}},{key:"parseMaybeUnary",value:function(e){var t=this.tokenType.precedence,r=this.startNode(),n=this.start,i=this.value;if(this.tokenType.isPrefix&&t>=e)return r.operator=i,r.prefix=!0,this.next(),r.argument=this.parseExprAtom(this.start,t),this.finishNode(r,"UnaryExpression");if(this.tokenType===N.numeric){var s=function(e){for(var t=e,r=[],n=0;n<t.length;n++)95!==t.charCodeAt(n)&&r.push(t[n]);return r.length!==t.length&&(t=r.join("")),t}(i);return r.rawValue=i,r.value=s,this.next(),this.finishNode(r,"NumericLiteral")}return this.tokenType===N.identifier?(r.name=i,this.next(),this.finishNode(r,"Identifier")):this.unexpected(i,n)}},{key:"next",value:function(){if(this.lastTokenStart=this.start,this.lastTokenEnd=this.end,this.skipSpace(),this.start=this.pos,this.pos>=this.input.length){if(this.tokenType===N.end)return;this.finishToken(N.end)}else this.readToken()}},{key:"readToken",value:function(){return A(this.codeAt(this.pos))?this.readNumeric():this.readTokenFromCode()}},{key:"readNumeric",value:function(){for(var e=this.pos,t=-1,r=!0,n=!1,i=!1,s=-1;this.isValidPosition();){var o=this.codeAt(this.pos);if(O(o))-1===t&&(t=0),this.pos++,i=!1,n=!0;else{if(i){s=this.pos,i=!1;break}if(69===o||101===o){if(0!==t){s=this.pos;break}t++,this.pos++,!this.isValidPosition()||43!==this.codeAt(this.pos)&&45!==this.codeAt(this.pos)||this.pos++,r=!1,i=!0}else if(46===o){if(!r){s=this.pos;break}r=!1,this.pos++,this.pos-e==1&&(i=!0)}else{if(95!==o)break;if(!n){s=this.pos;break}n=!1,i=!0,this.pos++}}}i&&(s=this.pos-1),s>=0&&this.unexpected(this.input[s],s);var u=this.input.slice(e,this.pos);this.finishToken(N.numeric,u)}},{key:"readTokenFromCode",value:function(){var e,t,r,n=this.codeAt(this.pos);for(t=0;t<C.length;t++){var i=C[t];for(r=0;r<i.codes.length&&i.codes[r]===this.codeAt(this.pos+r);r++);if(r===i.codes.length){e=i;break}}if(e)return this.pos+=e.codes.length,this.finishToken(e.type,e.value);switch(n){case 40:return this.pos++,this.finishToken(N.parenL,"(");case 41:return this.pos++,this.finishToken(N.parenR,")");case 42:return this.pos++,this.finishToken(N.times,"*");case 43:return this.pos++,this.allowPrefix?this.finishToken(N.prefixPlus,"+"):this.finishToken(N.plus,"+");case 45:return this.pos++,this.allowPrefix?this.finishToken(N.prefixMinus,"-"):this.finishToken(N.minus,"-");case 47:return this.pos++,this.finishToken(N.div,"/");default:if(P(n))return this.readIdentifier()}this.unexpected(this.input[this.pos])}},{key:"readIdentifier",value:function(){for(var e=this.pos;this.isValidPosition()&&S(this.codeAt(this.pos));)this.pos++;var t=this.input.slice(e,this.pos);this.finishToken(N.identifier,t)}},{key:"finishToken",value:function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"",r=this.tokenType;this.end=this.pos,this.tokenType=e,this.value=t,this.updateContext(r)}},{key:"updateContext",value:function(e){var t=this.tokenType;t.isBinary||t.isPrefix?this.allowPrefix=!0:t.updateContext&&t.updateContext.call(this,e)}},{key:"codeAt",value:function(e){return this.input.charCodeAt(e)}},{key:"isValidPosition",value:function(){return this.pos<this.input.length}},{key:"startNode",value:function(e){return new T(null!=e?e:this.start)}},{key:"finishNode",value:function(e,t){return e.type=t,e.end=this.lastTokenEnd,e}},{key:"skipSpace",value:function(){for(;this.isValidPosition();){var e=this.codeAt(this.pos);if(32===e||160===e)this.pos++;else if(13===e||10===e||8232===e||8233===e)13===e&&10===this.codeAt(this.pos+1)&&this.pos++,this.pos++;else{if(!(e>8&&e<14))break;this.pos++}}}},{key:"expect",value:function(e){this.eat(e)||this.unexpected(this.value)}},{key:"eat",value:function(e){return this.tokenType===e&&(this.next(),!0)}},{key:"unexpected",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"",t=arguments.length>1?arguments[1]:void 0;this.raise(null!=t?t:this.start,"Unexpected token ".concat(e))}},{key:"raise",value:function(e,t){throw e>this.input.length-1?t="Unexpected end of input":t+=" at position ".concat(e),new SyntaxError(t)}}])&&b(t.prototype,r),n&&b(t,n),e}();k(M,"createBinaryOperator",void 0),k(M,"createUnaryOperator",void 0),k(M,"useOperator",void 0),k(M,"useAdapter",void 0),k(M,"Node",T),k(M,"TokenType",E),k(M,"tokenTypes",N),k(M,"installedOperators",C);var j=M.evaluate;M.createBinaryOperator=function(e,t,r){if(U(e,t),"function"!=typeof r)throw new Error("Expected to receive a calculation method, like: `(left, right) => String(left - right)`");return new L(e,t,r,!1)},M.createUnaryOperator=function(e,t,r){if(U(e,t),"function"!=typeof r)throw new Error("Expected to receive a calculation method, like: `(value) => String(Math.abs(value))`");return new L(e,t,r,!0)},M.useOperator=function(e){C.includes(e)||C.unshift(e)},M.useAdapter=function(e){e=e||{},["+","-","*","/"].forEach((function(t){if("function"!=typeof e[t])throw new Error("Missing method for calculation operator `".concat(t,"`"))})),R=e};var F={evaluate:j,Parser:M,Operator:L,version:"0.1.1"},G={"+":function(e,t){return new m(e).plus(t).toString()},"-":function(e,t){return new m(e).minus(t).toString()},"*":function(e,t){return new m(e).times(t).toString()},"/":function(e,t){return new m(e).div(t).toString()}};M.useAdapter(G),e.BigNumber=m,e.Operator=L,e.Parser=M,e.default=F,e.evaluate=j,Object.defineProperty(e,"__esModule",{value:!0})}));
//# sourceMappingURL=index.min.js.map