UNPKG

dinero.js

Version:

An immutable library to create, calculate and format monetary values.

2 lines (1 loc) 9.93 kB
define(function(){'use strict';var A=Math.floor,B=Math.max,C=Math.pow,D=Math.abs,E=Number.isInteger;function a(b){"@babel/helpers - typeof";return a="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},a(b)}function b(a){return c(a)||d(a)||e(a)||g()}function c(a){if(Array.isArray(a))return a}function d(a){if("undefined"!=typeof Symbol&&Symbol.iterator in Object(a))return Array.from(a)}function e(a,b){if(a){if("string"==typeof a)return f(a,b);var c=Object.prototype.toString.call(a).slice(8,-1);return"Object"===c&&a.constructor&&(c=a.constructor.name),"Map"===c||"Set"===c?Array.from(c):"Arguments"===c||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(c)?f(a,b):void 0}}function f(a,b){(null==b||b>a.length)&&(b=a.length);for(var c=0,d=Array(b);c<b;c++)d[c]=a[c];return d}function g(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function h(a){return!isNaN(parseInt(a))&&isFinite(a)}function i(a){return h(a)&&100>=a&&0<=a}function j(a){return 0<a.length&&a.every(function(a){return 0<=a})&&a.some(function(a){return 0<a})}function k(a){return 0==a%2}function l(a){return h(a)&&!E(a)}function m(){var a=0<arguments.length&&arguments[0]!==void 0?arguments[0]:0,b=a.toString();if(0<b.indexOf("e-"))return parseInt(b.split("e-")[1]);var c=b.split(".")[1];return c?c.length:0}function n(a){return .5==D(a)%1}function o(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),p(e,b.headers),e.send()})}function p(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 q(a){return"undefined"==typeof a}function r(b){var c=1<arguments.length&&void 0!==arguments[1]?arguments[1]:".",d={};return Object.entries(b).forEach(function(b){if("object"===a(b[1])){var e=r(b[1]);Object.entries(e).forEach(function(a){d[b[0]+c+a[0]]=a[1]})}else d[b[0]]=b[1]}),d}function s(b){return!!b&&("object"===a(b)||"function"==typeof b)&&"function"==typeof b.then}function t(){var a=Math.sign,c=Math.round,d=function(d,a){var b=function(a){return C(10,m(a))},e=B(b(d),b(a));return c(d*e)*c(a*e)/(e*e)},e={HALF_ODD:function(a){var b=c(a);return n(a)?k(b)?b-1:b:b},HALF_EVEN:function(a){var b=c(a);return n(a)?k(b)?b:b-1:b},HALF_UP:function(a){return c(a)},HALF_DOWN:function(a){return n(a)?A(a):c(a)},HALF_TOWARDS_ZERO:function(b){return n(b)?a(b)*A(D(b)):c(b)},HALF_AWAY_FROM_ZERO:function(b){return n(b)?a(b)*Math.ceil(D(b)):c(b)},DOWN:function(a){return A(a)}};return{add:function(c,a){return c+a},subtract:function(c,a){return c-a},multiply:function(c,a){return l(c)||l(a)?d(c,a):c*a},divide:function(c,a){return c/a},modulo:function(c,a){return c%a},round:function(a){var b=1<arguments.length&&arguments[1]!==void 0?arguments[1]:"HALF_EVEN";return e[b](a)}}}function u(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!q(a)})},getMinimumFractionDigits:function(){var a=function(a){return"."===a};return q(this.getMatches().find(a))?0:this.getMatches()[F.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 q(this.getCurrencyDisplay(this.getMatches()))?"decimal":"currency"},getUseGrouping:function(){return!q(this.getMatches().find(function(a){return","===a}))}}}function v(a){var b=function(){var a=0<arguments.length&&arguments[0]!==void 0?arguments[0]:"",b=1<arguments.length?arguments[1]:void 0;for(var c in b)a=a.replace("{{".concat(c,"}}"),b[c]);return a},c=function(c,d){return o(b(a.endpoint,{from:c,to:d}),{headers:a.headers})};return{getExchangeRate:function(d,e){return(s(a.endpoint)?a.endpoint:c(d,e)).then(function(c){return r(c)[b(a.propertyPath,{from:d,to:e})]})}}}function w(a,b){var c=2<arguments.length&&arguments[2]!==void 0?arguments[2]:Error;if(!a)throw new c(b)}function x(a){w(i(a),"You must provide a numeric value between 0 and 100.",RangeError)}function y(a){w(j(a),"You must provide a non-empty array of numeric values greater than 0.",TypeError)}function z(a){w(E(a),"You must provide an integer.",TypeError)}var F=t(),G=t(),H=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;z(d),z(f);var g=a.globalLocale,h=a.globalFormat,i=a.globalRoundingMode,j=a.globalFormatRoundingMode,k=Object.assign({},a.globalExchangeRatesApi),l=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})},m=function(a){w(this.hasSameCurrency(a),"You must provide a Dinero instance with the same currency.",TypeError)};return{getAmount:function(){return d},getCurrency:function(){return e},getLocale:function(){return this.locale||g},setLocale:function(a){return l.call(this,{locale:a})},getPrecision:function(){return f},convertPrecision:function(a){var b=1<arguments.length&&void 0!==arguments[1]?arguments[1]:j;return z(a),l.call(this,{amount:G.round(G.multiply(this.getAmount(),C(10,G.subtract(a,this.getPrecision()))),b),precision:a})},add:function(b){m.call(this,b);var c=a.normalizePrecision([this,b]);return l.call(this,{amount:G.add(c[0].getAmount(),c[1].getAmount()),precision:c[0].getPrecision()})},subtract:function(b){m.call(this,b);var c=a.normalizePrecision([this,b]);return l.call(this,{amount:G.subtract(c[0].getAmount(),c[1].getAmount()),precision:c[0].getPrecision()})},multiply:function(a){var b=1<arguments.length&&arguments[1]!==void 0?arguments[1]:i;return l.call(this,{amount:G.round(G.multiply(this.getAmount(),a),b)})},divide:function(a){var b=1<arguments.length&&arguments[1]!==void 0?arguments[1]:i;return l.call(this,{amount:G.round(G.divide(this.getAmount(),a),b)})},percentage:function(a){return x(a),this.multiply(G.divide(a,100))},allocate:function(a){var b=this;y(a);for(var c=a.reduce(function(c,a){return G.add(c,a)}),d=this.getAmount(),e=a.map(function(a){var e=A(G.divide(G.multiply(b.getAmount(),a),c));return d=G.subtract(d,e),l.call(b,{amount:e})}),f=0;0<d;)0<a[f]&&(e[f]=e[f].add(l.call(this,{amount:1})),d=G.subtract(d,1)),f+=1;return e},convert:function(a){var b=this,c=1<arguments.length&&arguments[1]!==void 0?arguments[1]:{},d=c.endpoint,e=void 0===d?k.endpoint:d,f=c.propertyPath,g=void 0===f?k.propertyPath||"rates.{{to}}":f,h=c.headers,j=void 0===h?k.headers:h,m=c.roundingMode,n=void 0===m?i:m,o=Object.assign({},{endpoint:e,propertyPath:g,headers:j,roundingMode:n});return v(o).getExchangeRate(this.getCurrency(),a).then(function(c){return w(!q(c),"No rate was found for the destination currency \"".concat(a,"\"."),TypeError),l.call(b,{amount:G.round(G.multiply(b.getAmount(),parseFloat(c)),o.roundingMode),currency:a})})},equalsTo:function(a){return this.hasSameAmount(a)&&this.hasSameCurrency(a)},lessThan:function(b){m.call(this,b);var c=a.normalizePrecision([this,b]);return c[0].getAmount()<c[1].getAmount()},lessThanOrEqual:function(b){m.call(this,b);var c=a.normalizePrecision([this,b]);return c[0].getAmount()<=c[1].getAmount()},greaterThan:function(b){m.call(this,b);var c=a.normalizePrecision([this,b]);return c[0].getAmount()>c[1].getAmount()},greaterThanOrEqual:function(b){m.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!==G.modulo(this.getAmount(),C(10,f))},hasCents:function(){return 0!==G.modulo(this.getAmount(),C(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&&arguments[0]!==void 0?arguments[0]:h,b=1<arguments.length&&arguments[1]!==void 0?arguments[1]:j,c=u(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 G.divide(this.getAmount(),C(10,f))},toRoundedUnit:function(a){var b=1<arguments.length&&arguments[1]!==void 0?arguments[1]:j,c=C(10,a);return G.divide(G.round(G.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 B(c.getPrecision(),a.getPrecision())});return a.map(function(a){return a.getPrecision()===b?a:a.convertPrecision(b)})},minimum:function(a){var c=b(a),d=c[0],e=c.slice(1),f=d;return e.forEach(function(a){f=f.lessThan(a)?f:a}),f},maximum:function(a){var c=b(a),d=c[0],e=c.slice(1),f=d;return e.forEach(function(a){f=f.greaterThan(a)?f:a}),f}});return H});