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