UNPKG

@acrool/js-utils

Version:

Common javascript utils methods for project development

2 lines (1 loc) 7.54 kB
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});var k=20,_=1,N=1e6,F=1e6,d=-7,L=21,C=!1,M="[big.js] ",p=M+"Invalid ",P=p+"decimal places",T=p+"rounding mode",j=M+"Division by zero",h={},w=void 0,U=/^-?(\d+(\.\d*)?|\.\d+)(e[+-]?\d+)?$/i;function S(){function e(r){var t=this;if(!(t instanceof e))return r===w&&arguments.length===0?S():new e(r);if(r instanceof e)t.s=r.s,t.e=r.e,t.c=r.c.slice();else{if(typeof r!="string"){if(e.strict===!0&&typeof r!="bigint")throw TypeError(p+"value");r=r===0&&1/r<0?"-0":String(r)}V(t,r)}t.constructor=e}return e.prototype=h,e.DP=k,e.RM=_,e.NE=d,e.PE=L,e.strict=C,e.roundDown=0,e.roundHalfUp=1,e.roundHalfEven=2,e.roundUp=3,e}function V(e,r){var t,i,n;if(!U.test(r))throw Error(p+"number");for(e.s=r.charAt(0)=="-"?(r=r.slice(1),-1):1,(t=r.indexOf("."))>-1&&(r=r.replace(".","")),(i=r.search(/e/i))>0?(t<0&&(t=i),t+=+r.slice(i+1),r=r.substring(0,i)):t<0&&(t=r.length),n=r.length,i=0;i<n&&r.charAt(i)=="0";)++i;if(i==n)e.c=[e.e=0];else{for(;n>0&&r.charAt(--n)=="0";);for(e.e=t-i-1,e.c=[],t=0;i<=n;)e.c[t++]=+r.charAt(i++)}return e}function b(e,r,t,i){var n=e.c;if(t===w&&(t=e.constructor.RM),t!==0&&t!==1&&t!==2&&t!==3)throw Error(T);if(r<1)i=t===3&&(i||!!n[0])||r===0&&(t===1&&n[0]>=5||t===2&&(n[0]>5||n[0]===5&&(i||n[1]!==w))),n.length=1,i?(e.e=e.e-r+1,n[0]=1):n[0]=e.e=0;else if(r<n.length){if(i=t===1&&n[r]>=5||t===2&&(n[r]>5||n[r]===5&&(i||n[r+1]!==w||n[r-1]&1))||t===3&&(i||!!n[0]),n.length=r,i){for(;++n[--r]>9;)if(n[r]=0,r===0){++e.e,n.unshift(1);break}}for(r=n.length;!n[--r];)n.pop()}return e}function m(e,r,t){var i=e.e,n=e.c.join(""),c=n.length;if(r)n=n.charAt(0)+(c>1?"."+n.slice(1):"")+(i<0?"e":"e+")+i;else if(i<0){for(;++i;)n="0"+n;n="0."+n}else if(i>0)if(++i>c)for(i-=c;i--;)n+="0";else i<c&&(n=n.slice(0,i)+"."+n.slice(i));else c>1&&(n=n.charAt(0)+"."+n.slice(1));return e.s<0&&t?"-"+n:n}h.abs=function(){var e=new this.constructor(this);return e.s=1,e};h.cmp=function(e){var r,t=this,i=t.c,n=(e=new t.constructor(e)).c,c=t.s,u=e.s,o=t.e,s=e.e;if(!i[0]||!n[0])return i[0]?c:n[0]?-u:0;if(c!=u)return c;if(r=c<0,o!=s)return o>s^r?1:-1;for(u=(o=i.length)<(s=n.length)?o:s,c=-1;++c<u;)if(i[c]!=n[c])return i[c]>n[c]^r?1:-1;return o==s?0:o>s^r?1:-1};h.div=function(e){var r=this,t=r.constructor,i=r.c,n=(e=new t(e)).c,c=r.s==e.s?1:-1,u=t.DP;if(u!==~~u||u<0||u>N)throw Error(P);if(!n[0])throw Error(j);if(!i[0])return e.s=c,e.c=[e.e=0],e;var o,s,f,v,l,E=n.slice(),B=o=n.length,O=i.length,a=i.slice(0,o),g=a.length,x=e,I=x.c=[],A=0,D=u+(x.e=r.e-e.e)+1;for(x.s=c,c=D<0?0:D,E.unshift(0);g++<o;)a.push(0);do{for(f=0;f<10;f++){if(o!=(g=a.length))v=o>g?1:-1;else for(l=-1,v=0;++l<o;)if(n[l]!=a[l]){v=n[l]>a[l]?1:-1;break}if(v<0){for(s=g==o?n:E;g;){if(a[--g]<s[g]){for(l=g;l&&!a[--l];)a[l]=9;--a[l],a[g]+=10}a[g]-=s[g]}for(;!a[0];)a.shift()}else break}I[A++]=v?f:++f,a[0]&&v?a[g]=i[B]||0:a=[i[B]]}while((B++<O||a[0]!==w)&&c--);return!I[0]&&A!=1&&(I.shift(),x.e--,D--),A>D&&b(x,D,t.RM,a[0]!==w),x};h.eq=function(e){return this.cmp(e)===0};h.gt=function(e){return this.cmp(e)>0};h.gte=function(e){return this.cmp(e)>-1};h.lt=function(e){return this.cmp(e)<0};h.lte=function(e){return this.cmp(e)<1};h.minus=h.sub=function(e){var r,t,i,n,c=this,u=c.constructor,o=c.s,s=(e=new u(e)).s;if(o!=s)return e.s=-s,c.plus(e);var f=c.c.slice(),v=c.e,l=e.c,E=e.e;if(!f[0]||!l[0])return l[0]?e.s=-s:f[0]?e=new u(c):e.s=1,e;if(o=v-E){for((n=o<0)?(o=-o,i=f):(E=v,i=l),i.reverse(),s=o;s--;)i.push(0);i.reverse()}else for(t=((n=f.length<l.length)?f:l).length,o=s=0;s<t;s++)if(f[s]!=l[s]){n=f[s]<l[s];break}if(n&&(i=f,f=l,l=i,e.s=-e.s),(s=(t=l.length)-(r=f.length))>0)for(;s--;)f[r++]=0;for(s=r;t>o;){if(f[--t]<l[t]){for(r=t;r&&!f[--r];)f[r]=9;--f[r],f[t]+=10}f[t]-=l[t]}for(;f[--s]===0;)f.pop();for(;f[0]===0;)f.shift(),--E;return f[0]||(e.s=1,f=[E=0]),e.c=f,e.e=E,e};h.mod=function(e){var r,t=this,i=t.constructor,n=t.s,c=(e=new i(e)).s;if(!e.c[0])throw Error(j);return t.s=e.s=1,r=e.cmp(t)==1,t.s=n,e.s=c,r?new i(t):(n=i.DP,c=i.RM,i.DP=i.RM=0,t=t.div(e),i.DP=n,i.RM=c,this.minus(t.times(e)))};h.neg=function(){var e=new this.constructor(this);return e.s=-e.s,e};h.plus=h.add=function(e){var r,t,i,n=this,c=n.constructor;if(e=new c(e),n.s!=e.s)return e.s=-e.s,n.minus(e);var u=n.e,o=n.c,s=e.e,f=e.c;if(!o[0]||!f[0])return f[0]||(o[0]?e=new c(n):e.s=n.s),e;if(o=o.slice(),r=u-s){for(r>0?(s=u,i=f):(r=-r,i=o),i.reverse();r--;)i.push(0);i.reverse()}for(o.length-f.length<0&&(i=f,f=o,o=i),r=f.length,t=0;r;o[r]%=10)t=(o[--r]=o[r]+f[r]+t)/10|0;for(t&&(o.unshift(t),++s),r=o.length;o[--r]===0;)o.pop();return e.c=o,e.e=s,e};h.pow=function(e){var r=this,t=new r.constructor("1"),i=t,n=e<0;if(e!==~~e||e<-1e6||e>F)throw Error(p+"exponent");for(n&&(e=-e);e&1&&(i=i.times(r)),e>>=1,!!e;)r=r.times(r);return n?t.div(i):i};h.prec=function(e,r){if(e!==~~e||e<1||e>N)throw Error(p+"precision");return b(new this.constructor(this),e,r)};h.round=function(e,r){if(e===w)e=0;else if(e!==~~e||e<-1e6||e>N)throw Error(P);return b(new this.constructor(this),e+this.e+1,r)};h.sqrt=function(){var e,r,t,i=this,n=i.constructor,c=i.s,u=i.e,o=new n("0.5");if(!i.c[0])return new n(i);if(c<0)throw Error(M+"No square root");c=Math.sqrt(+m(i,!0,!0)),c===0||c===1/0?(r=i.c.join(""),r.length+u&1||(r+="0"),c=Math.sqrt(r),u=((u+1)/2|0)-(u<0||u&1),e=new n((c==1/0?"5e":(c=c.toExponential()).slice(0,c.indexOf("e")+1))+u)):e=new n(c+""),u=e.e+(n.DP+=4);do t=e,e=o.times(t.plus(i.div(t)));while(t.c.slice(0,u).join("")!==e.c.slice(0,u).join(""));return b(e,(n.DP-=4)+e.e+1,n.RM)};h.times=h.mul=function(e){var r,t=this,i=t.constructor,n=t.c,c=(e=new i(e)).c,u=n.length,o=c.length,s=t.e,f=e.e;if(e.s=t.s==e.s?1:-1,!n[0]||!c[0])return e.c=[e.e=0],e;for(e.e=s+f,u<o&&(r=n,n=c,c=r,f=u,u=o,o=f),r=new Array(f=u+o);f--;)r[f]=0;for(s=o;s--;){for(o=0,f=u+s;f>s;)o=r[f]+c[s]*n[f-s-1]+o,r[f--]=o%10,o=o/10|0;r[f]=o}for(o?++e.e:r.shift(),s=r.length;!r[--s];)r.pop();return e.c=r,e};h.toExponential=function(e,r){var t=this,i=t.c[0];if(e!==w){if(e!==~~e||e<0||e>N)throw Error(P);for(t=b(new t.constructor(t),++e,r);t.c.length<e;)t.c.push(0)}return m(t,!0,!!i)};h.toFixed=function(e,r){var t=this,i=t.c[0];if(e!==w){if(e!==~~e||e<0||e>N)throw Error(P);for(t=b(new t.constructor(t),e+t.e+1,r),e=e+t.e+1;t.c.length<e;)t.c.push(0)}return m(t,!1,!!i)};h.toJSON=h.toString=function(){var e=this,r=e.constructor;return m(e,e.e<=r.NE||e.e>=r.PE,!!e.c[0])};typeof Symbol<"u"&&(h[Symbol.for("nodejs.util.inspect.custom")]=h.toJSON);h.toNumber=function(){var e=+m(this,!0,!0);if(this.constructor.strict===!0&&!this.eq(e.toString()))throw Error(M+"Imprecise conversion");return e};h.toPrecision=function(e,r){var t=this,i=t.constructor,n=t.c[0];if(e!==w){if(e!==~~e||e<1||e>N)throw Error(p+"precision");for(t=b(new i(t),e,r);t.c.length<e;)t.c.push(0)}return m(t,e<=t.e||t.e<=i.NE||t.e>=i.PE,!!n)};h.valueOf=function(){var e=this,r=e.constructor;if(r.strict===!0)throw Error(M+"valueOf disallowed");return m(e,e.e<=r.NE||e.e>=r.PE,!0)};var R=S();function q(e=0,r=0){try{return new R(e).round(r,0).toFixed(r)}catch{return new R(0).toFixed(r)}}function Z(e=0,r=0){const i=q(e,r).toString().split(".");return i[0]=i[0].replace(/\B(?=(\d{3})+(?!\d))/g,","),i.join(".")}function $(e){if(!e.length)return{min:NaN,max:NaN};let r=-1/0,t=1/0;return e.forEach(([i,n])=>{r=Math.max(r,i),t=Math.min(t,n)}),r>t?{min:NaN,max:NaN}:{min:r,max:t}}function z(e){return/^0\.\d+$/.test(e)?e.replace(/^0/,""):/^\d*\.?\d+$/.test(e)?e.replace(/^0+(?=\d)/,""):e}exports.formatCurrency=Z;exports.intersectionMin=$;exports.removeLeadingZero=z;exports.safeFormatDecimal=q;