Files
2026-02-14 07:57:18 -05:00

1 line
8.7 KiB
JavaScript

(function(a,b){'object'==typeof exports&&'undefined'!=typeof module?module.exports=b():'function'==typeof define&&define.amd?define(b):a.Dinero=b()})(this,function(){'use strict';function a(a){return!isNaN(parseInt(a))&&isFinite(a)}function b(b){return a(b)&&100>=b&&0<=b}function c(a){return 0<a.length&&a.every(function(a){return 0<a})}function d(a){return 0==a%2}function e(b){return a(b)&&!w(b)}function f(){var a=0<arguments.length&&arguments[0]!==void 0?arguments[0]:0,b=a.toString().split('.')[1];return b?b.length:0}function g(a){return .5==v(a)%1}function h(a){var b=1<arguments.length&&arguments[1]!==void 0?arguments[1]:{};return new Promise(function(c,d){var e=Object.assign(new XMLHttpRequest,{onreadystatechange:function(){4===e.readyState&&(200<=e.status&&400>e.status?c(JSON.parse(e.responseText)):d(new Error(e.statusText)))},onerror:function(){d(new Error('Network error'))}});e.open('GET',a,!0),i(e,b.headers),e.send()})}function i(a){var b=1<arguments.length&&arguments[1]!==void 0?arguments[1]:{};for(var c in b)a.setRequestHeader(c,b[c]);return a}function j(a){return'undefined'==typeof a}function k(a){var b=1<arguments.length&&void 0!==arguments[1]?arguments[1]:'.',c={};return Object.entries(a).forEach(function(a){if('object'===y(a[1])){var d=k(a[1]);Object.entries(d).forEach(function(d){c[a[0]+b+d[0]]=d[1]})}else c[a[0]]=a[1]}),c}function l(a){return!!a&&('object'===('undefined'==typeof a?'undefined':y(a))||'function'==typeof a)&&'function'==typeof a.then}function m(){var a=Math.sign,c=Math.round,h=function(d,a){var b=function(a){return u(10,f(a))},e=x(b(d),b(a));return c(d*e)*c(a*e)/(e*e)},b={HALF_ODD:function(a){var b=c(a);return g(a)?d(b)?b-1:b:b},HALF_EVEN:function(a){var b=c(a);return g(a)?d(b)?b:b-1:b},HALF_UP:function(a){return c(a)},HALF_DOWN:function(a){return g(a)?t(a):c(a)},HALF_TOWARDS_ZERO:function(b){return g(b)?a(b)*t(v(b)):c(b)},HALF_AWAY_FROM_ZERO:function(b){return g(b)?a(b)*Math.ceil(v(b)):c(b)}};return{add:function(c,a){return c+a},subtract:function(c,a){return c-a},multiply:function(c,a){return e(c)||e(a)?h(c,a):c*a},divide:function(c,a){return c/a},modulo:function(c,a){return c%a},round:function(a){var c=1<arguments.length&&arguments[1]!==void 0?arguments[1]:'HALF_EVEN';return b[c](a)}}}function n(a){var b=/^(?:(\$|USD)?0(?:(,)0)?(\.)?(0+)?|0(?:(,)0)?(\.)?(0+)?\s?(dollar)?)$/gm.exec(a);return{getMatches:function(){return null===b?[]:b.slice(1).filter(function(a){return!j(a)})},getMinimumFractionDigits:function(){var a=function(a){return'.'===a};return j(this.getMatches().find(a))?0:this.getMatches()[z.add(this.getMatches().findIndex(a),1)].split('').length},getCurrencyDisplay:function(){return{USD:'code',dollar:'name',$:'symbol'}[this.getMatches().find(function(a){return'USD'===a||'dollar'===a||'$'===a})]},getStyle:function(){return j(this.getCurrencyDisplay(this.getMatches()))?'decimal':'currency'},getUseGrouping:function(){return!j(this.getMatches().find(function(a){return','===a}))}}}function o(a){var b=function(){var a=0<arguments.length&&arguments[0]!==void 0?arguments[0]:'',b=arguments[1];for(var c in b)a=a.replace('{{'+c+'}}',b[c]);return a},c=function(c,d){return h(b(a.endpoint,{from:c,to:d}),{headers:a.headers})};return{getExchangeRate:function(d,e){return(l(a.endpoint)?a.endpoint:c(d,e)).then(function(c){return k(c)[b(a.propertyPath,{from:d,to:e})]})}}}function p(a){var b=1<arguments.length&&arguments[1]!==void 0?arguments[1]:new Error;if(!a)throw b}function q(a){p(b(a),new RangeError('You must provide a numeric value between 0 and 100.'))}function r(a){p(c(a),new TypeError('You must provide a non-empty array of numeric values greater than 0.'))}function s(a){p(w(a),new TypeError('You must provide an integer.'))}var t=Math.floor,u=Math.pow,v=Math.abs,w=Number.isInteger,x=Math.max,y='function'==typeof Symbol&&'symbol'==typeof Symbol.iterator?function(a){return typeof a}:function(a){return a&&'function'==typeof Symbol&&a.constructor===Symbol&&a!==Symbol.prototype?'symbol':typeof a},z=m(),A=m(),B=Object.assign(function a(b){var c=Object.assign({},{amount:a.defaultAmount,currency:a.defaultCurrency,precision:a.defaultPrecision},b),d=c.amount,e=c.currency,f=c.precision;s(d),s(f);var g=a.globalLocale,h=a.globalFormat,i=a.globalRoundingMode,k=a.globalFormatRoundingMode,l=Object.assign({},a.globalExchangeRatesApi),m=function(b){var c=Object.assign({},Object.assign({},{amount:d,currency:e,precision:f},b),Object.assign({},{locale:this.locale},b));return Object.assign(a({amount:c.amount,currency:c.currency,precision:c.precision}),{locale:c.locale})},v=function(a){p(this.hasSameCurrency(a),new TypeError('You must provide a Dinero instance with the same currency.'))};return{getAmount:function(){return d},getCurrency:function(){return e},getLocale:function(){return this.locale||g},setLocale:function(a){return m.call(this,{locale:a})},getPrecision:function(){return f},convertPrecision:function(a){var b=1<arguments.length&&void 0!==arguments[1]?arguments[1]:k;return s(a),m.call(this,{amount:A.round(A.multiply(this.getAmount(),u(10,A.subtract(a,this.getPrecision()))),b),precision:a})},add:function(b){v.call(this,b);var c=a.normalizePrecision([this,b]);return m.call(this,{amount:A.add(c[0].getAmount(),c[1].getAmount()),precision:c[0].getPrecision()})},subtract:function(b){v.call(this,b);var c=a.normalizePrecision([this,b]);return m.call(this,{amount:A.subtract(c[0].getAmount(),c[1].getAmount()),precision:c[0].getPrecision()})},multiply:function(a){var b=1<arguments.length&&void 0!==arguments[1]?arguments[1]:i;return m.call(this,{amount:A.round(A.multiply(this.getAmount(),a),b)})},divide:function(a){var b=1<arguments.length&&void 0!==arguments[1]?arguments[1]:i;return m.call(this,{amount:A.round(A.divide(this.getAmount(),a),b)})},percentage:function(a){return q(a),this.multiply(A.divide(a,100))},allocate:function(a){var b=this;r(a);for(var c=a.reduce(function(c,a){return A.add(c,a)}),d=this.getAmount(),e=a.map(function(a){var e=t(A.divide(A.multiply(b.getAmount(),a),c));return d=A.subtract(d,e),m.call(b,{amount:e})}),f=0;0<d;f++)e[f]=e[f].add(m.call(this,{amount:1})),d=A.subtract(d,1);return e},convert:function(a){var b=this,c=1<arguments.length&&void 0!==arguments[1]?arguments[1]:{},d=c.endpoint,e=void 0===d?l.endpoint:d,f=c.propertyPath,g=void 0===f?l.propertyPath||'rates.{{to}}':f,h=c.headers,k=void 0===h?l.headers:h,n=c.roundingMode,q=void 0===n?i:n,r=Object.assign({},{endpoint:e,propertyPath:g,headers:k,roundingMode:q});return o(r).getExchangeRate(this.getCurrency(),a).then(function(c){return p(!j(c),new TypeError('No rate was found for the destination currency "'+a+'".')),m.call(b,{amount:A.round(A.multiply(b.getAmount(),parseFloat(c)),r.roundingMode),currency:a})})},equalsTo:function(a){return this.hasSameAmount(a)&&this.hasSameCurrency(a)},lessThan:function(b){v.call(this,b);var c=a.normalizePrecision([this,b]);return c[0].getAmount()<c[1].getAmount()},lessThanOrEqual:function(b){v.call(this,b);var c=a.normalizePrecision([this,b]);return c[0].getAmount()<=c[1].getAmount()},greaterThan:function(b){v.call(this,b);var c=a.normalizePrecision([this,b]);return c[0].getAmount()>c[1].getAmount()},greaterThanOrEqual:function(b){v.call(this,b);var c=a.normalizePrecision([this,b]);return c[0].getAmount()>=c[1].getAmount()},isZero:function(){return 0===this.getAmount()},isPositive:function(){return 0<=this.getAmount()},isNegative:function(){return 0>this.getAmount()},hasSubUnits:function(){return 0!==A.modulo(this.getAmount(),u(10,f))},hasCents:function(){return 0!==A.modulo(this.getAmount(),u(10,f))},hasSameCurrency:function(a){return this.getCurrency()===a.getCurrency()},hasSameAmount:function(b){var c=a.normalizePrecision([this,b]);return c[0].getAmount()===c[1].getAmount()},toFormat:function(){var a=0<arguments.length&&void 0!==arguments[0]?arguments[0]:h,b=1<arguments.length&&void 0!==arguments[1]?arguments[1]:k,c=n(a);return this.toRoundedUnit(c.getMinimumFractionDigits(),b).toLocaleString(this.getLocale(),{currencyDisplay:c.getCurrencyDisplay(),useGrouping:c.getUseGrouping(),minimumFractionDigits:c.getMinimumFractionDigits(),style:c.getStyle(),currency:this.getCurrency()})},toUnit:function(){return A.divide(this.getAmount(),u(10,f))},toRoundedUnit:function(a){var b=1<arguments.length&&void 0!==arguments[1]?arguments[1]:k,c=u(10,a);return A.divide(A.round(A.multiply(this.toUnit(),c),b),c)},toObject:function(){return{amount:d,currency:e,precision:f}},toJSON:function(){return this.toObject()}}},{defaultAmount:0,defaultCurrency:'USD',defaultPrecision:2},{globalLocale:'en-US',globalFormat:'$0,0.00',globalRoundingMode:'HALF_EVEN',globalFormatRoundingMode:'HALF_AWAY_FROM_ZERO',globalExchangeRatesApi:{endpoint:void 0,headers:void 0,propertyPath:void 0}},{normalizePrecision:function(a){var b=a.reduce(function(c,a){return x(c.getPrecision(),a.getPrecision())});return a.map(function(a){return a.getPrecision()===b?a:a.convertPrecision(b)})}});return B});