UNPKG

@easymoney/bigint-money

Version:
3 lines (2 loc) 6.27 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],u=t[4];let c;if(c=void 0===o?0n:BigInt(o)*L,void 0!==u){const n=v-BigInt(u.length);c+=n>=0?BigInt(u)*10n**n:N(BigInt(u),10n**-n,e)}return"-"===r&&(c*=-1n),c;case"bigint":return n*L;default:throw new TypeError("value must be a safe integer, bigint or string")}}function t(n,e,t){return({amount:r,currency:f})=>{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],u=t[4];let c;if(u&&0n!==BigInt(u))throw new TypeError(`The number ${n} is not an integer. It must be converted before passing it`);return c=void 0===o?0n:BigInt(o)*L,"-"===r&&(c*=-1n),{amount:c,currency:e};case"bigint":return{amount:n*L,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)*L,currency:e};default:throw new TypeError("value must be a safe integer, bigint or string")}})(r,f,t),y={calculator:n,instanceMoney:money,roundindMode:e},R={},v={privateInstance:y,publicInstance:R};return R.add=w.bind(i,v),R.getSource=w.bind(u,y),R.getAmount=w.bind(o,y),R.getCurrency=w.bind(c,y),R.subtract=w.bind(a,v),R.isSameCurrency=w.bind(b,R),R.equals=w.bind(s,v),R.compare=w.bind(d,v),R.greaterThan=w.bind(g,R),R.greaterThanOrEqual=w.bind(l,R),R.lessThan=w.bind(m,R),R.lessThanOrEqual=w.bind(p,R),R.multiply=w.bind(I,v),R.divide=w.bind(M,v),R.allocate=w.bind(h,v),R.allocateTo=w.bind(E,v),R}}function r(n,e=w.RoundingModes.HALF_EVEN){return t(n,e,null)}function o(n){return n.instanceMoney.amount/L}function u(n){return n.instanceMoney.amount}function c(n){return n.instanceMoney.currency}function i(n,money){const{publicInstance:e,privateInstance:r}=n,{calculator:o,instanceMoney:u,roundindMode:c}=r;f(e,money);const i=o.add(u.amount,money.getSource());return t(r.calculator,c,{source:1})({amount:i,currency:money.getCurrency()})}function a(n,money){const{publicInstance:e,privateInstance:r}=n,{calculator:o,instanceMoney:u,roundindMode:c}=r;f(e,money);const i=o.subtract(u.amount,money.getSource());return t(r.calculator,c,{source:1})({amount:i,currency:money.getCurrency()})}function s(n,money){return n.publicInstance.isSameCurrency(money)&&n.privateInstance.instanceMoney.amount===money.getSource()}function d(n,money){const{publicInstance:e,privateInstance:t}=n;return f(e,money),t.calculator.compare(t.instanceMoney.amount,money.getSource())}function g(n,money){return n.compare(money)>0}function l(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 b(n,money){return n.getCurrency()===money.getCurrency()}function f(n,money){w.assert(b(n,money),new TypeError("Currencies must be identical"))}function y(n){w.assert([w.RoundingModes.CEILING,w.RoundingModes.DOWN,w.RoundingModes.FLOOR,w.RoundingModes.HALF_DOWN,w.RoundingModes.HALF_EVEN,w.RoundingModes.HALF_UP,w.RoundingModes.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=w.RoundingModes.HALF_EVEN){y(r);const{publicInstance:o,privateInstance:u}=n,{calculator:c}=u,i=c.multiply(o.getSource(),e,r);return t(u.calculator,r,{source:1})({amount:i,currency:o.getCurrency()})}function M(n,r,o=w.RoundingModes.HALF_EVEN){y(o);const{privateInstance:u,publicInstance:c}=n,{calculator:i}=u,a=e(r,o),s=i.divide(c.getSource(),a,o);return t(i,o,null)({amount:s,currency:c.getCurrency()})}function h(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:u}=r,c=o.getAmount();let i=c;const a=[],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=u.share(c,BigInt(r),BigInt(s));a[n]=o,i=u.subtract(i,o)}if(0===u.compare(i,0n))return a.map(e=>t(u,n.privateInstance.roundindMode,null)({amount:e,currency:o.getCurrency()}));const d=e.map(n=>{const e=n/s*Number(c);return e-Math.floor(e)});for(;u.compare(i,0n)>0;){let n;if(0!==d.length){let e=[];d.forEach((n,t)=>{n===Math.max(...d)&&e.push(t)}),n=e[0]}else n=0;a[n]=u.add(a[n],1n),i=u.subtract(i,1n),d[n]=0}return a.map(e=>t(u,n.privateInstance.roundindMode,null)({amount:e,currency:o.getCurrency()}))}function E(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 R(n){w.assert("bigint"==typeof n,new TypeError("result must be big int"))}Object.defineProperty(exports,'__esModule',{value:1});var w=require('@easymoney/core');const v=BigInt(20),L=10n**v,N=(e,t,r)=>{const o=n(e),u=n(t);let c=o/u;const i=o%u,a=e>0!=t>0?-1:1;if(r===w.RoundingModes.HALF_DOWN||r===w.RoundingModes.HALF_UP||r===w.RoundingModes.HALF_EVEN){const n=2n*i;r===w.RoundingModes.HALF_DOWN?n>u&&c++:r===w.RoundingModes.HALF_UP?(n>u||n===u)&&c++:r===w.RoundingModes.HALF_EVEN&&(n>u||n===u&&c%2n===1n)&&c++}else i>0n&&(r===w.RoundingModes.FLOOR?-1===a&&c++:r===w.RoundingModes.CEILING?1===a&&c++:r===w.RoundingModes.DOWN||r===w.RoundingModes.UP&&c++);return c*BigInt(a)},T=(n,e,t)=>N(n,e,t),C=r({compare:(n,e)=>n<e?-1:n>e?1:0,add:(n,e)=>{const t=n+e;return R(t),t},subtract:(n,e)=>{const t=n-e;return R(t),t},divide:T,multiply:(n,t,r)=>{const o=e(t,r);return T(n*o,L,r)},absolute:e=>n(e),mod:(n,e)=>n%e,share:(n,e,t)=>N(n*e,t,w.RoundingModes.FLOOR)});exports.createBigIntMoney=C,exports.createBigIntMoneyUnit=r; //# sourceMappingURL=index.cjs.js.map