UNPKG

@easymoney/bigint-money

Version:
3 lines (2 loc) 5.85 kB
function n(n){return n>0?n:-n}function e(n,e){switch(typeof n){case"string":case"number":const t=String(n).match(/^(\+|\-)?([0-9]*)?(\.([0-9]*))?$/);if(!t)throw new TypeError("Input string must follow the pattern (-)##.## or -##");const r=t[1],o=t[2],c=t[4];let u;if(u=void 0===o?0n:BigInt(o)*C,void 0!==c){const n=T-BigInt(c.length);u+=n>=0?BigInt(c)*10n**n:F(BigInt(c),10n**-n,e)}return"-"===r&&(u*=-1n),u;case"bigint":return n*C;default:throw new TypeError("value must be a safe integer, bigint or string")}}function t(n,e,t){return({amount:r,currency:y})=>{const money=((n,e,t)=>{if(t&&t.source)return{amount:n,currency:e};switch(typeof n){case"string":const t=String(n).match(/^(\+|\-)?([0-9]*)?(\.([0-9]*))?$/);if(!t)throw new TypeError("Input string must follow the pattern (-)##.## or -##");const r=t[1],o=t[2],c=t[4];let u;if(c&&0n!==BigInt(c))throw new TypeError(`The number ${n} is not an integer. It must be converted before passing it`);return u=void 0===o?0n:BigInt(o)*C,"-"===r&&(u*=-1n),{amount:u,currency:e};case"bigint":return{amount:n*C,currency:e};case"number":if(!Number.isSafeInteger(n))throw new TypeError(`The number ${n} is not a "safe" integer. It must be converted before passing it`);return{amount:BigInt(n)*C,currency:e};default:throw new TypeError("value must be a safe integer, bigint or string")}})(r,y,t),b={calculator:n,instanceMoney:money,roundindMode:e},L={},M={privateInstance:b,publicInstance:L};return L.add=N(a,M),L.getSource=N(c,b),L.getAmount=N(o,b),L.getCurrency=N(u,b),L.subtract=N(i,M),L.isSameCurrency=N(d,L),L.equals=N(s,M),L.compare=N(l,M),L.greaterThan=N(g,L),L.greaterThanOrEqual=N(m,L),L.lessThan=N(p,L),L.lessThanOrEqual=N(f,L),L.multiply=N(I,M),L.divide=N(h,M),L.allocate=N(E,M),L.allocateTo=N(w,M),L}}function r(n,e=M.HALF_EVEN){return t(n,e,null)}function o(n){return n.instanceMoney.amount/C}function c(n){return n.instanceMoney.amount}function u(n){return n.instanceMoney.currency}function a(n,money){const{publicInstance:e,privateInstance:r}=n,{calculator:o,instanceMoney:c,roundindMode:u}=r;y(e,money);const a=o.add(c.amount,money.getSource());return t(r.calculator,u,{source:1})({amount:a,currency:money.getCurrency()})}function i(n,money){const{publicInstance:e,privateInstance:r}=n,{calculator:o,instanceMoney:c,roundindMode:u}=r;y(e,money);const a=o.subtract(c.amount,money.getSource());return t(r.calculator,u,{source:1})({amount:a,currency:money.getCurrency()})}function s(n,money){return n.publicInstance.isSameCurrency(money)&&n.privateInstance.instanceMoney.amount===money.getSource()}function l(n,money){const{publicInstance:e,privateInstance:t}=n;return y(e,money),t.calculator.compare(t.instanceMoney.amount,money.getSource())}function g(n,money){return n.compare(money)>0}function m(n,money){return n.compare(money)>=0}function p(n,money){return n.compare(money)<0}function f(n,money){return n.compare(money)<=0}function d(n,money){return n.getCurrency()===money.getCurrency()}function y(n,money){v(d(n,money),new TypeError("Currencies must be identical"))}function b(n){v([M.CEILING,M.DOWN,M.FLOOR,M.HALF_DOWN,M.HALF_EVEN,M.HALF_UP,M.UP].includes(n),new TypeError("rounding mode should be one of RoundingModes.CEILING RoundingModes.DOWN RoundingModes.FLOOR RoundingModes.HALF_DOWN RoundingModes.HALF_EVEN RoundingModes.HALF_UP RoundingModes.UP"))}function I(n,e,r=M.HALF_EVEN){b(r);const{publicInstance:o,privateInstance:c}=n,{calculator:u}=c,a=u.multiply(o.getSource(),e,r);return t(c.calculator,r,{source:1})({amount:a,currency:o.getCurrency()})}function h(n,r,o=M.HALF_EVEN){b(o);const{privateInstance:c,publicInstance:u}=n,{calculator:a}=c,i=e(r,o),s=a.divide(u.getSource(),i,o);return t(a,o,null)({amount:s,currency:u.getCurrency()})}function E(n,e){if(0===e.length)throw new TypeError("Cannot allocate to none, ratios cannot be an empty array");const{privateInstance:r,publicInstance:o}=n,{calculator:c}=r,u=o.getAmount();let a=u;const i=[],s=e.reduce((n,e)=>n+e,0);if(s<=0)throw new TypeError("Cannot allocate to none, sum of ratios must be greater than zero");for(let n=0;n<e.length;n++){const t=e[n],r=Number.isInteger(t)?t:parseInt(String(100*t),10);if(t<0)throw new TypeError("Cannot allocate to none, ratio must be zero or positive");const o=c.share(u,BigInt(r),BigInt(s));i[n]=o,a=c.subtract(a,o)}if(0===c.compare(a,0n))return i.map(e=>t(c,n.privateInstance.roundindMode,null)({amount:e,currency:o.getCurrency()}));const l=e.map(n=>{const e=n/s*Number(u);return e-Math.floor(e)});for(;c.compare(a,0n)>0;){let n;if(0!==l.length){let e=[];l.forEach((n,t)=>{n===Math.max(...l)&&e.push(t)}),n=e[0]}else n=0;i[n]=c.add(i[n],1n),a=c.subtract(a,1n),l[n]=0}return i.map(e=>t(c,n.privateInstance.roundindMode,null)({amount:e,currency:o.getCurrency()}))}function w(n,e){if(!Number.isInteger(e))throw new TypeError("Number of targets must be an integer");if(e<=0)throw new TypeError("Cannot allocate to none, target must be greater than zero");return n.publicInstance.allocate(Array(e).fill(1))}function L(n){v("bigint"==typeof n,new TypeError("result must be big int"))}import{RoundingModes as M,bind as N,assert as v}from'@easymoney/core';const T=BigInt(20),C=10n**T,F=(e,t,r)=>{const o=n(e),c=n(t);let u=o/c;const a=o%c,i=e>0!=t>0?-1:1;if(r===M.HALF_DOWN||r===M.HALF_UP||r===M.HALF_EVEN){const n=2n*a;r===M.HALF_DOWN?n>c&&u++:r===M.HALF_UP?(n>c||n===c)&&u++:r===M.HALF_EVEN&&(n>c||n===c&&u%2n===1n)&&u++}else a>0n&&(r===M.FLOOR?-1===i&&u++:r===M.CEILING?1===i&&u++:r===M.DOWN||r===M.UP&&u++);return u*BigInt(i)},A=(n,e,t)=>F(n,e,t),O=r({compare:(n,e)=>n<e?-1:n>e?1:0,add:(n,e)=>{const t=n+e;return L(t),t},subtract:(n,e)=>{const t=n-e;return L(t),t},divide:A,multiply:(n,t,r)=>{const o=e(t,r);return A(n*o,C,r)},absolute:e=>n(e),mod:(n,e)=>n%e,share:(n,e,t)=>F(n*e,t,M.FLOOR)});export{O as createBigIntMoney,r as createBigIntMoneyUnit}; //# sourceMappingURL=index.es.js.map