dinero.js 1.9.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,1663 @@
1
+ (function (global, factory) {
2
+ typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
3
+ typeof define === 'function' && define.amd ? define(factory) :
4
+ (global = global || self, global.Dinero = factory());
5
+ }(this, (function () { 'use strict';
6
+
7
+ /**
8
+ * Default values for all Dinero objects.
9
+ *
10
+ * You can override default values for all subsequent Dinero objects by changing them directly on the global `Dinero` object.
11
+ * Existing instances won't be affected.
12
+ *
13
+ * @property {Number} defaultAmount - The default amount for new Dinero objects (see {@link module:Dinero Dinero} for format).
14
+ * @property {String} defaultCurrency - The default currency for new Dinero objects (see {@link module:Dinero Dinero} for format).
15
+ * @property {Number} defaultPrecision - The default precision for new Dinero objects (see {@link module:Dinero Dinero} for format).
16
+ *
17
+ * @example
18
+ * // Will set currency to 'EUR' for all Dinero objects.
19
+ * Dinero.defaultCurrency = 'EUR'
20
+ *
21
+ * @type {Object}
22
+ */
23
+ var Defaults = {
24
+ defaultAmount: 0,
25
+ defaultCurrency: 'USD',
26
+ defaultPrecision: 2
27
+ };
28
+ /**
29
+ * Global settings for all Dinero objects.
30
+ *
31
+ * You can override global values for all subsequent Dinero objects by changing them directly on the global `Dinero` object.
32
+ * Existing instances won't be affected.
33
+ *
34
+ * @property {String} globalLocale - The global locale for new Dinero objects (see {@link module:Dinero~setLocale setLocale} for format).
35
+ * @property {String} globalFormat - The global format for new Dinero objects (see {@link module:Dinero~toFormat toFormat} for format).
36
+ * @property {String} globalRoundingMode - The global rounding mode for new Dinero objects (see {@link module:Dinero~multiply multiply} or {@link module:Dinero~divide divide} for format).
37
+ * @property {String} globalFormatRoundingMode - The global rounding mode to format new Dinero objects (see {@link module:Dinero~toFormat toFormat} or {@link module:Dinero~toRoundedUnit toRoundedUnit} for format).
38
+ * @property {(String|Promise)} globalExchangeRatesApi.endpoint - The global exchange rate API endpoint for new Dinero objects, or the global promise that resolves to the exchanges rates (see {@link module:Dinero~convert convert} for format).
39
+ * @property {String} globalExchangeRatesApi.propertyPath - The global exchange rate API property path for new Dinero objects (see {@link module:Dinero~convert convert} for format).
40
+ * @property {Object} globalExchangeRatesApi.headers - The global exchange rate API headers for new Dinero objects (see {@link module:Dinero~convert convert} for format).
41
+ *
42
+ * @example
43
+ * // Will set locale to 'fr-FR' for all Dinero objects.
44
+ * Dinero.globalLocale = 'fr-FR'
45
+ * @example
46
+ * // Will set global exchange rate API parameters for all Dinero objects.
47
+ * Dinero.globalExchangeRatesApi = {
48
+ * endpoint: 'https://yourexchangerates.api/latest?base={{from}}',
49
+ * propertyPath: 'data.rates.{{to}}',
50
+ * headers: {
51
+ * 'user-key': 'xxxxxxxxx'
52
+ * }
53
+ * }
54
+ *
55
+ * @type {Object}
56
+ */
57
+
58
+ var Globals = {
59
+ globalLocale: 'en-US',
60
+ globalFormat: '$0,0.00',
61
+ globalRoundingMode: 'HALF_EVEN',
62
+ globalFormatRoundingMode: 'HALF_AWAY_FROM_ZERO',
63
+ globalExchangeRatesApi: {
64
+ endpoint: undefined,
65
+ headers: undefined,
66
+ propertyPath: undefined
67
+ }
68
+ };
69
+
70
+ function _typeof(obj) {
71
+ "@babel/helpers - typeof";
72
+
73
+ if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
74
+ _typeof = function (obj) {
75
+ return typeof obj;
76
+ };
77
+ } else {
78
+ _typeof = function (obj) {
79
+ return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
80
+ };
81
+ }
82
+
83
+ return _typeof(obj);
84
+ }
85
+
86
+ function _toArray(arr) {
87
+ return _arrayWithHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableRest();
88
+ }
89
+
90
+ function _arrayWithHoles(arr) {
91
+ if (Array.isArray(arr)) return arr;
92
+ }
93
+
94
+ function _iterableToArray(iter) {
95
+ if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter);
96
+ }
97
+
98
+ function _unsupportedIterableToArray(o, minLen) {
99
+ if (!o) return;
100
+ if (typeof o === "string") return _arrayLikeToArray(o, minLen);
101
+ var n = Object.prototype.toString.call(o).slice(8, -1);
102
+ if (n === "Object" && o.constructor) n = o.constructor.name;
103
+ if (n === "Map" || n === "Set") return Array.from(o);
104
+ if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
105
+ }
106
+
107
+ function _arrayLikeToArray(arr, len) {
108
+ if (len == null || len > arr.length) len = arr.length;
109
+
110
+ for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
111
+
112
+ return arr2;
113
+ }
114
+
115
+ function _nonIterableRest() {
116
+ throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
117
+ }
118
+
119
+ /**
120
+ * Static methods for Dinero.
121
+ * @ignore
122
+ *
123
+ * @type {Object}
124
+ */
125
+ var Static = {
126
+ /**
127
+ * Returns an array of Dinero objects, normalized to the same precision (the highest).
128
+ *
129
+ * @memberof module:Dinero
130
+ * @method
131
+ *
132
+ * @param {Dinero[]} objects - An array of Dinero objects
133
+ *
134
+ * @example
135
+ * // returns an array of Dinero objects
136
+ * // both with a precision of 3
137
+ * // and an amount of 1000
138
+ * Dinero.normalizePrecision([
139
+ * Dinero({ amount: 100, precision: 2 }),
140
+ * Dinero({ amount: 1000, precision: 3 })
141
+ * ])
142
+ *
143
+ * @return {Dinero[]}
144
+ */
145
+ normalizePrecision: function normalizePrecision(objects) {
146
+ var highestPrecision = objects.reduce(function (a, b) {
147
+ return Math.max(a.getPrecision(), b.getPrecision());
148
+ });
149
+ return objects.map(function (object) {
150
+ return object.getPrecision() !== highestPrecision ? object.convertPrecision(highestPrecision) : object;
151
+ });
152
+ },
153
+
154
+ /**
155
+ * Returns the smallest Dinero object from an array of Dinero objects
156
+ *
157
+ * @memberof module:Dinero
158
+ * @method
159
+ *
160
+ * @param {Dinero[]} objects - An array of Dinero objects
161
+ *
162
+ * @example
163
+ * // returns the smallest Dinero object with amount of 500 from an array of Dinero objects with different precisions
164
+ * Dinero.minimum([
165
+ * Dinero({ amount: 500, precision: 3 }),
166
+ * Dinero({ amount: 100, precision: 2 })
167
+ * ])
168
+ * @example
169
+ * // returns the smallest Dinero object with amount of 50 from an array of Dinero objects
170
+ * Dinero.minimum([
171
+ * Dinero({ amount: 50 }),
172
+ * Dinero({ amount: 100 })
173
+ * ])
174
+ *
175
+ * @return {Dinero[]}
176
+ */
177
+ minimum: function minimum(objects) {
178
+ var _objects = _toArray(objects),
179
+ firstObject = _objects[0],
180
+ tailObjects = _objects.slice(1);
181
+
182
+ var currentMinimum = firstObject;
183
+ tailObjects.forEach(function (obj) {
184
+ currentMinimum = currentMinimum.lessThan(obj) ? currentMinimum : obj;
185
+ });
186
+ return currentMinimum;
187
+ },
188
+
189
+ /**
190
+ * Returns the biggest Dinero object from an array of Dinero objects
191
+ *
192
+ * @memberof module:Dinero
193
+ * @method
194
+ *
195
+ * @param {Dinero[]} objects - An array of Dinero objects
196
+ *
197
+ * @example
198
+ * // returns the biggest Dinero object with amount of 20, from an array of Dinero objects with different precisions
199
+ * Dinero.maximum([
200
+ * Dinero({ amount: 20, precision: 2 }),
201
+ * Dinero({ amount: 150, precision: 3 })
202
+ * ])
203
+ * @example
204
+ * // returns the biggest Dinero object with amount of 100, from an array of Dinero objects
205
+ * Dinero.maximum([
206
+ * Dinero({ amount: 100 }),
207
+ * Dinero({ amount: 50 })
208
+ * ])
209
+ *
210
+ * @return {Dinero[]}
211
+ */
212
+ maximum: function maximum(objects) {
213
+ var _objects2 = _toArray(objects),
214
+ firstObject = _objects2[0],
215
+ tailObjects = _objects2.slice(1);
216
+
217
+ var currentMaximum = firstObject;
218
+ tailObjects.forEach(function (obj) {
219
+ currentMaximum = currentMaximum.greaterThan(obj) ? currentMaximum : obj;
220
+ });
221
+ return currentMaximum;
222
+ }
223
+ };
224
+
225
+ /**
226
+ * Returns whether a value is numeric.
227
+ * @ignore
228
+ *
229
+ * @param {} value - The value to test.
230
+ *
231
+ * @return {Boolean}
232
+ */
233
+ function isNumeric(value) {
234
+ return !isNaN(parseInt(value)) && isFinite(value);
235
+ }
236
+ /**
237
+ * Returns whether a value is a percentage.
238
+ * @ignore
239
+ *
240
+ * @param {} percentage - The percentage to test.
241
+ *
242
+ * @return {Boolean}
243
+ */
244
+
245
+ function isPercentage(percentage) {
246
+ return isNumeric(percentage) && percentage <= 100 && percentage >= 0;
247
+ }
248
+ /**
249
+ * Returns whether an array of ratios is valid.
250
+ * @ignore
251
+ *
252
+ * @param {} ratios - The ratios to test.
253
+ *
254
+ * @return {Boolean}
255
+ */
256
+
257
+ function areValidRatios(ratios) {
258
+ return ratios.length > 0 && ratios.every(function (ratio) {
259
+ return ratio >= 0;
260
+ }) && ratios.some(function (ratio) {
261
+ return ratio > 0;
262
+ });
263
+ }
264
+ /**
265
+ * Returns whether a value is even.
266
+ * @ignore
267
+ *
268
+ * @param {Number} value - The value to test.
269
+ *
270
+ * @return {Boolean}
271
+ */
272
+
273
+ function isEven(value) {
274
+ return value % 2 === 0;
275
+ }
276
+ /**
277
+ * Returns whether a value is a float.
278
+ * @ignore
279
+ *
280
+ * @param {} value - The value to test.
281
+ *
282
+ * @return {Boolean}
283
+ */
284
+
285
+ function isFloat(value) {
286
+ return isNumeric(value) && !Number.isInteger(value);
287
+ }
288
+ /**
289
+ * Returns how many fraction digits a number has.
290
+ * @ignore
291
+ *
292
+ * @param {Number} [number=0] - The number to test.
293
+ *
294
+ * @return {Number}
295
+ */
296
+
297
+ function countFractionDigits() {
298
+ var number = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
299
+ var stringRepresentation = number.toString();
300
+
301
+ if (stringRepresentation.indexOf('e-') > 0) {
302
+ // It's too small for a normal string representation, e.g. 1e-7 instead of 0.00000001
303
+ return parseInt(stringRepresentation.split('e-')[1]);
304
+ } else {
305
+ var fractionDigits = stringRepresentation.split('.')[1];
306
+ return fractionDigits ? fractionDigits.length : 0;
307
+ }
308
+ }
309
+ /**
310
+ * Returns whether a number is half.
311
+ * @ignore
312
+ *
313
+ * @param {Number} number - The number to test.
314
+ *
315
+ * @return {Number}
316
+ */
317
+
318
+ function isHalf(number) {
319
+ return Math.abs(number) % 1 === 0.5;
320
+ }
321
+ /**
322
+ * Fetches a JSON resource.
323
+ * @ignore
324
+ *
325
+ * @param {String} url - The resource to fetch.
326
+ * @param {Object} [options.headers] - The headers to pass.
327
+ *
328
+ * @throws {Error} If `request.status` is lesser than 200 or greater or equal to 400.
329
+ * @throws {Error} If network fails.
330
+ *
331
+ * @return {JSON}
332
+ */
333
+
334
+ function getJSON(url) {
335
+ var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
336
+ return new Promise(function (resolve, reject) {
337
+ var request = Object.assign(new XMLHttpRequest(), {
338
+ onreadystatechange: function onreadystatechange() {
339
+ if (request.readyState === 4) {
340
+ if (request.status >= 200 && request.status < 400) resolve(JSON.parse(request.responseText));else reject(new Error(request.statusText));
341
+ }
342
+ },
343
+ onerror: function onerror() {
344
+ reject(new Error('Network error'));
345
+ }
346
+ });
347
+ request.open('GET', url, true);
348
+ setXHRHeaders(request, options.headers);
349
+ request.send();
350
+ });
351
+ }
352
+ /**
353
+ * Returns an XHR object with attached headers.
354
+ * @ignore
355
+ *
356
+ * @param {XMLHttpRequest} xhr - The XHR request to set headers to.
357
+ * @param {Object} headers - The headers to set.
358
+ *
359
+ * @return {XMLHttpRequest}
360
+ */
361
+
362
+ function setXHRHeaders(xhr) {
363
+ var headers = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
364
+
365
+ for (var header in headers) {
366
+ xhr.setRequestHeader(header, headers[header]);
367
+ }
368
+
369
+ return xhr;
370
+ }
371
+ /**
372
+ * Returns whether a value is undefined.
373
+ * @ignore
374
+ *
375
+ * @param {} value - The value to test.
376
+ *
377
+ * @return {Boolean}
378
+ */
379
+
380
+ function isUndefined(value) {
381
+ return typeof value === 'undefined';
382
+ }
383
+ /**
384
+ * Returns an object flattened to one level deep.
385
+ * @ignore
386
+ *
387
+ * @param {Object} object - The object to flatten.
388
+ * @param {String} separator - The separator to use between flattened nodes.
389
+ *
390
+ * @return {Object}
391
+ */
392
+
393
+ function flattenObject(object) {
394
+ var separator = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '.';
395
+ var finalObject = {};
396
+ Object.entries(object).forEach(function (item) {
397
+ if (_typeof(item[1]) === 'object') {
398
+ var flatObject = flattenObject(item[1]);
399
+ Object.entries(flatObject).forEach(function (node) {
400
+ finalObject[item[0] + separator + node[0]] = node[1];
401
+ });
402
+ } else {
403
+ finalObject[item[0]] = item[1];
404
+ }
405
+ });
406
+ return finalObject;
407
+ }
408
+ /**
409
+ * Returns whether a value is thenable.
410
+ * @ignore
411
+ *
412
+ * @param {} value - The value to test.
413
+ *
414
+ * @return {Boolean}
415
+ */
416
+
417
+ function isThenable(value) {
418
+ return Boolean(value) && (_typeof(value) === 'object' || typeof value === 'function') && typeof value.then === 'function';
419
+ }
420
+
421
+ function Calculator() {
422
+ var floatMultiply = function floatMultiply(a, b) {
423
+ var getFactor = function getFactor(number) {
424
+ return Math.pow(10, countFractionDigits(number));
425
+ };
426
+
427
+ var factor = Math.max(getFactor(a), getFactor(b));
428
+ return Math.round(a * factor) * Math.round(b * factor) / (factor * factor);
429
+ };
430
+
431
+ var roundingModes = {
432
+ HALF_ODD: function HALF_ODD(number) {
433
+ var rounded = Math.round(number);
434
+ return isHalf(number) ? isEven(rounded) ? rounded - 1 : rounded : rounded;
435
+ },
436
+ HALF_EVEN: function HALF_EVEN(number) {
437
+ var rounded = Math.round(number);
438
+ return isHalf(number) ? isEven(rounded) ? rounded : rounded - 1 : rounded;
439
+ },
440
+ HALF_UP: function HALF_UP(number) {
441
+ return Math.round(number);
442
+ },
443
+ HALF_DOWN: function HALF_DOWN(number) {
444
+ return isHalf(number) ? Math.floor(number) : Math.round(number);
445
+ },
446
+ HALF_TOWARDS_ZERO: function HALF_TOWARDS_ZERO(number) {
447
+ return isHalf(number) ? Math.sign(number) * Math.floor(Math.abs(number)) : Math.round(number);
448
+ },
449
+ HALF_AWAY_FROM_ZERO: function HALF_AWAY_FROM_ZERO(number) {
450
+ return isHalf(number) ? Math.sign(number) * Math.ceil(Math.abs(number)) : Math.round(number);
451
+ },
452
+ DOWN: function DOWN(number) {
453
+ return Math.floor(number);
454
+ }
455
+ };
456
+ return {
457
+ /**
458
+ * Returns the sum of two numbers.
459
+ * @ignore
460
+ *
461
+ * @param {Number} a - The first number to add.
462
+ * @param {Number} b - The second number to add.
463
+ *
464
+ * @return {Number}
465
+ */
466
+ add: function add(a, b) {
467
+ return a + b;
468
+ },
469
+
470
+ /**
471
+ * Returns the difference of two numbers.
472
+ * @ignore
473
+ *
474
+ * @param {Number} a - The first number to subtract.
475
+ * @param {Number} b - The second number to subtract.
476
+ *
477
+ * @return {Number}
478
+ */
479
+ subtract: function subtract(a, b) {
480
+ return a - b;
481
+ },
482
+
483
+ /**
484
+ * Returns the product of two numbers.
485
+ * @ignore
486
+ *
487
+ * @param {Number} a - The first number to multiply.
488
+ * @param {Number} b - The second number to multiply.
489
+ *
490
+ * @return {Number}
491
+ */
492
+ multiply: function multiply(a, b) {
493
+ return isFloat(a) || isFloat(b) ? floatMultiply(a, b) : a * b;
494
+ },
495
+
496
+ /**
497
+ * Returns the quotient of two numbers.
498
+ * @ignore
499
+ *
500
+ * @param {Number} a - The first number to divide.
501
+ * @param {Number} b - The second number to divide.
502
+ *
503
+ * @return {Number}
504
+ */
505
+ divide: function divide(a, b) {
506
+ return a / b;
507
+ },
508
+
509
+ /**
510
+ * Returns the remainder of two numbers.
511
+ * @ignore
512
+ *
513
+ * @param {Number} a - The first number to divide.
514
+ * @param {Number} b - The second number to divide.
515
+ *
516
+ * @return {Number}
517
+ */
518
+ modulo: function modulo(a, b) {
519
+ return a % b;
520
+ },
521
+
522
+ /**
523
+ * Returns a rounded number based off a specific rounding mode.
524
+ * @ignore
525
+ *
526
+ * @param {Number} number - The number to round.
527
+ * @param {String} [roundingMode='HALF_EVEN'] - The rounding mode to use.
528
+ *
529
+ * @returns {Number}
530
+ */
531
+ round: function round(number) {
532
+ var roundingMode = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'HALF_EVEN';
533
+ return roundingModes[roundingMode](number);
534
+ }
535
+ };
536
+ }
537
+
538
+ var calculator = Calculator();
539
+ function Format(format) {
540
+ var matches = /^(?:(\$|USD)?0(?:(,)0)?(\.)?(0+)?|0(?:(,)0)?(\.)?(0+)?\s?(dollar)?)$/gm.exec(format);
541
+ return {
542
+ /**
543
+ * Returns the matches.
544
+ * @ignore
545
+ *
546
+ * @return {Array}
547
+ */
548
+ getMatches: function getMatches() {
549
+ return matches !== null ? matches.slice(1).filter(function (match) {
550
+ return !isUndefined(match);
551
+ }) : [];
552
+ },
553
+
554
+ /**
555
+ * Returns the amount of fraction digits to display.
556
+ * @ignore
557
+ *
558
+ * @return {Number}
559
+ */
560
+ getMinimumFractionDigits: function getMinimumFractionDigits() {
561
+ var decimalPosition = function decimalPosition(match) {
562
+ return match === '.';
563
+ };
564
+
565
+ return !isUndefined(this.getMatches().find(decimalPosition)) ? this.getMatches()[calculator.add(this.getMatches().findIndex(decimalPosition), 1)].split('').length : 0;
566
+ },
567
+
568
+ /**
569
+ * Returns the currency display mode.
570
+ * @ignore
571
+ *
572
+ * @return {String}
573
+ */
574
+ getCurrencyDisplay: function getCurrencyDisplay() {
575
+ var modes = {
576
+ USD: 'code',
577
+ dollar: 'name',
578
+ $: 'symbol'
579
+ };
580
+ return modes[this.getMatches().find(function (match) {
581
+ return match === 'USD' || match === 'dollar' || match === '$';
582
+ })];
583
+ },
584
+
585
+ /**
586
+ * Returns the formatting style.
587
+ * @ignore
588
+ *
589
+ * @return {String}
590
+ */
591
+ getStyle: function getStyle() {
592
+ return !isUndefined(this.getCurrencyDisplay(this.getMatches())) ? 'currency' : 'decimal';
593
+ },
594
+
595
+ /**
596
+ * Returns whether grouping should be used or not.
597
+ * @ignore
598
+ *
599
+ * @return {Boolean}
600
+ */
601
+ getUseGrouping: function getUseGrouping() {
602
+ return !isUndefined(this.getMatches().find(function (match) {
603
+ return match === ',';
604
+ }));
605
+ }
606
+ };
607
+ }
608
+
609
+ function CurrencyConverter(options) {
610
+ /* istanbul ignore next */
611
+ var mergeTags = function mergeTags() {
612
+ var string = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
613
+ var tags = arguments.length > 1 ? arguments[1] : undefined;
614
+
615
+ for (var tag in tags) {
616
+ string = string.replace("{{".concat(tag, "}}"), tags[tag]);
617
+ }
618
+
619
+ return string;
620
+ };
621
+ /* istanbul ignore next */
622
+
623
+
624
+ var getRatesFromRestApi = function getRatesFromRestApi(from, to) {
625
+ return getJSON(mergeTags(options.endpoint, {
626
+ from: from,
627
+ to: to
628
+ }), {
629
+ headers: options.headers
630
+ });
631
+ };
632
+
633
+ return {
634
+ /**
635
+ * Returns the exchange rate.
636
+ * @ignore
637
+ *
638
+ * @param {String} from - The base currency.
639
+ * @param {String} to - The destination currency.
640
+ *
641
+ * @return {Promise}
642
+ */
643
+ getExchangeRate: function getExchangeRate(from, to) {
644
+ return (isThenable(options.endpoint) ? options.endpoint : getRatesFromRestApi(from, to)).then(function (data) {
645
+ return flattenObject(data)[mergeTags(options.propertyPath, {
646
+ from: from,
647
+ to: to
648
+ })];
649
+ });
650
+ }
651
+ };
652
+ }
653
+
654
+ /**
655
+ * Performs an assertion.
656
+ * @ignore
657
+ *
658
+ * @param {Boolean} condition - The expression to assert.
659
+ * @param {String} errorMessage - The message to throw if the assertion fails
660
+ * @param {ErrorConstructor} [ErrorType=Error] - The error to throw if the assertion fails.
661
+ *
662
+ * @throws {Error} If `condition` returns `false`.
663
+ */
664
+
665
+ function assert(condition, errorMessage) {
666
+ var ErrorType = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : Error;
667
+ if (!condition) throw new ErrorType(errorMessage);
668
+ }
669
+ /**
670
+ * Asserts a value is a percentage.
671
+ * @ignore
672
+ *
673
+ * @param {} percentage - The percentage to test.
674
+ *
675
+ * @throws {RangeError} If `percentage` is out of range.
676
+ */
677
+
678
+ function assertPercentage(percentage) {
679
+ assert(isPercentage(percentage), 'You must provide a numeric value between 0 and 100.', RangeError);
680
+ }
681
+ /**
682
+ * Asserts an array of ratios is valid.
683
+ * @ignore
684
+ *
685
+ * @param {} ratios - The ratios to test.
686
+ *
687
+ * @throws {TypeError} If `ratios` are invalid.
688
+ */
689
+
690
+ function assertValidRatios(ratios) {
691
+ assert(areValidRatios(ratios), 'You must provide a non-empty array of numeric values greater than 0.', TypeError);
692
+ }
693
+ /**
694
+ * Asserts a value is an integer.
695
+ * @ignore
696
+ *
697
+ * @param {} number - The value to test.
698
+ *
699
+ * @throws {TypeError}
700
+ */
701
+
702
+ function assertInteger(number) {
703
+ assert(Number.isInteger(number), 'You must provide an integer.', TypeError);
704
+ }
705
+
706
+ var calculator$1 = Calculator();
707
+ /**
708
+ * A Dinero object is an immutable data structure representing a specific monetary value.
709
+ * It comes with methods for creating, parsing, manipulating, testing, transforming and formatting them.
710
+ *
711
+ * A Dinero object has:
712
+ *
713
+ * * An `amount`, expressed in minor currency units, as an integer.
714
+ * * A `currency`, expressed as an {@link https://en.wikipedia.org/wiki/ISO_4217#Active_codes ISO 4217 currency code}.
715
+ * * A `precision`, expressed as an integer, to represent the number of decimal places in the `amount`.
716
+ * This is helpful when you want to represent fractional minor currency units (e.g.: $10.4545).
717
+ * You can also use it to represent a currency with a different [exponent](https://en.wikipedia.org/wiki/ISO_4217#Treatment_of_minor_currency_units_.28the_.22exponent.22.29) than `2` (e.g.: Iraqi dinar with 1000 fils in 1 dinar (exponent of `3`), Japanese yen with no sub-units (exponent of `0`)).
718
+ * * An optional `locale` property that affects how output strings are formatted.
719
+ *
720
+ * Here's an overview of the public API:
721
+ *
722
+ * * **Access:** {@link module:Dinero~getAmount getAmount}, {@link module:Dinero~getCurrency getCurrency}, {@link module:Dinero~getLocale getLocale} and {@link module:Dinero~getPrecision getPrecision}.
723
+ * * **Manipulation:** {@link module:Dinero~add add}, {@link module:Dinero~subtract subtract}, {@link module:Dinero~multiply multiply}, {@link module:Dinero~divide divide}, {@link module:Dinero~percentage percentage}, {@link module:Dinero~allocate allocate} and {@link module:Dinero~convert convert}.
724
+ * * **Testing:** {@link module:Dinero~equalsTo equalsTo}, {@link module:Dinero~lessThan lessThan}, {@link module:Dinero~lessThanOrEqual lessThanOrEqual}, {@link module:Dinero~greaterThan greaterThan}, {@link module:Dinero~greaterThanOrEqual greaterThanOrEqual}, {@link module:Dinero~isZero isZero}, {@link module:Dinero~isPositive isPositive}, {@link module:Dinero~isNegative isNegative}, {@link module:Dinero~hasSubUnits hasSubUnits}, {@link module:Dinero~hasSameCurrency hasSameCurrency} and {@link module:Dinero~hasSameAmount hasSameAmount}.
725
+ * * **Configuration:** {@link module:Dinero~setLocale setLocale}.
726
+ * * **Conversion & formatting:** {@link module:Dinero~toFormat toFormat}, {@link module:Dinero~toUnit toUnit}, {@link module:Dinero~toRoundedUnit toRoundedUnit}, {@link module:Dinero~toObject toObject}, {@link module:Dinero~toJSON toJSON}, {@link module:Dinero~convertPrecision convertPrecision} and {@link module:Dinero.normalizePrecision normalizePrecision}.
727
+ *
728
+ * Dinero.js uses `number`s under the hood, so it's constrained by the [double-precision floating-point format](https://en.wikipedia.org/wiki/Double-precision_floating-point_format). Using values over [`Number.MAX_SAFE_INTEGER`](https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Objets_globaux/Number/MAX_SAFE_INTEGER) or below [`Number.MIN_SAFE_INTEGER`](https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Objets_globaux/Number/MIN_SAFE_INTEGER) will yield unpredictable results.
729
+ * Same goes with performing calculations: once the internal `amount` value exceeds those limits, precision is no longer guaranteed.
730
+ *
731
+ * @module Dinero
732
+ * @param {Number} [options.amount=0] - The amount in minor currency units (as an integer).
733
+ * @param {String} [options.currency='USD'] - An ISO 4217 currency code.
734
+ * @param {String} [options.precision=2] - The number of decimal places to represent.
735
+ *
736
+ * @throws {TypeError} If `amount` or `precision` is invalid. Integers over [`Number.MAX_SAFE_INTEGER`](https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Objets_globaux/Number/MAX_SAFE_INTEGER) or below [`Number.MIN_SAFE_INTEGER`](https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Objets_globaux/Number/MIN_SAFE_INTEGER) are considered valid, even though they can lead to imprecise amounts.
737
+ *
738
+ * @return {Object}
739
+ */
740
+
741
+ var Dinero = function Dinero(options) {
742
+ var _Object$assign = Object.assign({}, {
743
+ amount: Dinero.defaultAmount,
744
+ currency: Dinero.defaultCurrency,
745
+ precision: Dinero.defaultPrecision
746
+ }, options),
747
+ amount = _Object$assign.amount,
748
+ currency = _Object$assign.currency,
749
+ precision = _Object$assign.precision;
750
+
751
+ assertInteger(amount);
752
+ assertInteger(precision);
753
+ var globalLocale = Dinero.globalLocale,
754
+ globalFormat = Dinero.globalFormat,
755
+ globalRoundingMode = Dinero.globalRoundingMode,
756
+ globalFormatRoundingMode = Dinero.globalFormatRoundingMode;
757
+ var globalExchangeRatesApi = Object.assign({}, Dinero.globalExchangeRatesApi);
758
+ /**
759
+ * Uses ES5 function notation so `this` can be passed through call, apply and bind
760
+ * @ignore
761
+ */
762
+
763
+ var create = function create(options) {
764
+ var obj = Object.assign({}, Object.assign({}, {
765
+ amount: amount,
766
+ currency: currency,
767
+ precision: precision
768
+ }, options), Object.assign({}, {
769
+ locale: this.locale
770
+ }, options));
771
+ return Object.assign(Dinero({
772
+ amount: obj.amount,
773
+ currency: obj.currency,
774
+ precision: obj.precision
775
+ }), {
776
+ locale: obj.locale
777
+ });
778
+ };
779
+ /**
780
+ * Uses ES5 function notation so `this` can be passed through call, apply and bind
781
+ * @ignore
782
+ */
783
+
784
+
785
+ var assertSameCurrency = function assertSameCurrency(comparator) {
786
+ assert(this.hasSameCurrency(comparator), 'You must provide a Dinero instance with the same currency.', TypeError);
787
+ };
788
+
789
+ return {
790
+ /**
791
+ * Returns the amount.
792
+ *
793
+ * @example
794
+ * // returns 500
795
+ * Dinero({ amount: 500 }).getAmount()
796
+ *
797
+ * @return {Number}
798
+ */
799
+ getAmount: function getAmount() {
800
+ return amount;
801
+ },
802
+
803
+ /**
804
+ * Returns the currency.
805
+ *
806
+ * @example
807
+ * // returns 'EUR'
808
+ * Dinero({ currency: 'EUR' }).getCurrency()
809
+ *
810
+ * @return {String}
811
+ */
812
+ getCurrency: function getCurrency() {
813
+ return currency;
814
+ },
815
+
816
+ /**
817
+ * Returns the locale.
818
+ *
819
+ * @example
820
+ * // returns 'fr-FR'
821
+ * Dinero().setLocale('fr-FR').getLocale()
822
+ *
823
+ * @return {String}
824
+ */
825
+ getLocale: function getLocale() {
826
+ return this.locale || globalLocale;
827
+ },
828
+
829
+ /**
830
+ * Returns a new Dinero object with an embedded locale.
831
+ *
832
+ * @param {String} newLocale - The new locale as an {@link http://tools.ietf.org/html/rfc5646 BCP 47 language tag}.
833
+ *
834
+ * @example
835
+ * // Returns a Dinero object with locale 'ja-JP'
836
+ * Dinero().setLocale('ja-JP')
837
+ *
838
+ * @return {Dinero}
839
+ */
840
+ setLocale: function setLocale(newLocale) {
841
+ return create.call(this, {
842
+ locale: newLocale
843
+ });
844
+ },
845
+
846
+ /**
847
+ * Returns the precision.
848
+ *
849
+ * @example
850
+ * // returns 3
851
+ * Dinero({ precision: 3 }).getPrecision()
852
+ *
853
+ * @return {Number}
854
+ */
855
+ getPrecision: function getPrecision() {
856
+ return precision;
857
+ },
858
+
859
+ /**
860
+ * Returns a new Dinero object with a new precision and a converted amount.
861
+ *
862
+ * By default, fractional minor currency units are rounded using the **half to even** rule ([banker's rounding](http://wiki.c2.com/?BankersRounding)).
863
+ * This can be necessary when you need to convert objects to a smaller precision.
864
+ *
865
+ * Rounding *can* lead to accuracy issues as you chain many times. Consider a minimal amount of subsequent conversions for safer results.
866
+ * You can also specify a different `roundingMode` to better fit your needs.
867
+ *
868
+ * @param {Number} newPrecision - The new precision.
869
+ * @param {String} [roundingMode='HALF_EVEN'] - The rounding mode to use: `'HALF_ODD'`, `'HALF_EVEN'`, `'HALF_UP'`, `'HALF_DOWN'`, `'HALF_TOWARDS_ZERO'`, `'HALF_AWAY_FROM_ZERO'` or `'DOWN'`.
870
+ *
871
+ * @example
872
+ * // Returns a Dinero object with precision 3 and amount 1000
873
+ * Dinero({ amount: 100, precision: 2 }).convertPrecision(3)
874
+ *
875
+ * @throws {TypeError} If `newPrecision` is invalid.
876
+ *
877
+ * @return {Dinero}
878
+ */
879
+ convertPrecision: function convertPrecision(newPrecision) {
880
+ var roundingMode = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : globalFormatRoundingMode;
881
+ assertInteger(newPrecision);
882
+ var precision = this.getPrecision();
883
+ var isNewPrecisionLarger = newPrecision > precision;
884
+ var operation = isNewPrecisionLarger ? calculator$1.multiply : calculator$1.divide;
885
+ var terms = isNewPrecisionLarger ? [newPrecision, precision] : [precision, newPrecision];
886
+ var factor = Math.pow(10, calculator$1.subtract.apply(calculator$1, terms));
887
+ return create.call(this, {
888
+ amount: calculator$1.round(operation(this.getAmount(), factor), roundingMode),
889
+ precision: newPrecision
890
+ });
891
+ },
892
+
893
+ /**
894
+ * Returns a new Dinero object that represents the sum of this and an other Dinero object.
895
+ *
896
+ * If Dinero objects have a different `precision`, they will be first converted to the highest.
897
+ *
898
+ * @param {Dinero} addend - The Dinero object to add.
899
+ *
900
+ * @example
901
+ * // returns a Dinero object with amount 600
902
+ * Dinero({ amount: 400 }).add(Dinero({ amount: 200 }))
903
+ * @example
904
+ * // returns a Dinero object with amount 144545 and precision 4
905
+ * Dinero({ amount: 400 }).add(Dinero({ amount: 104545, precision: 4 }))
906
+ *
907
+ * @throws {TypeError} If `addend` has a different currency.
908
+ *
909
+ * @return {Dinero}
910
+ */
911
+ add: function add(addend) {
912
+ assertSameCurrency.call(this, addend);
913
+ var addends = Dinero.normalizePrecision([this, addend]);
914
+ return create.call(this, {
915
+ amount: calculator$1.add(addends[0].getAmount(), addends[1].getAmount()),
916
+ precision: addends[0].getPrecision()
917
+ });
918
+ },
919
+
920
+ /**
921
+ * Returns a new Dinero object that represents the difference of this and an other Dinero object.
922
+ *
923
+ * If Dinero objects have a different `precision`, they will be first converted to the highest.
924
+ *
925
+ * @param {Dinero} subtrahend - The Dinero object to subtract.
926
+ *
927
+ * @example
928
+ * // returns a Dinero object with amount 200
929
+ * Dinero({ amount: 400 }).subtract(Dinero({ amount: 200 }))
930
+ * @example
931
+ * // returns a Dinero object with amount 64545 and precision 4
932
+ * Dinero({ amount: 104545, precision: 4 }).subtract(Dinero({ amount: 400 }))
933
+ *
934
+ * @throws {TypeError} If `subtrahend` has a different currency.
935
+ *
936
+ * @return {Dinero}
937
+ */
938
+ subtract: function subtract(subtrahend) {
939
+ assertSameCurrency.call(this, subtrahend);
940
+ var subtrahends = Dinero.normalizePrecision([this, subtrahend]);
941
+ return create.call(this, {
942
+ amount: calculator$1.subtract(subtrahends[0].getAmount(), subtrahends[1].getAmount()),
943
+ precision: subtrahends[0].getPrecision()
944
+ });
945
+ },
946
+
947
+ /**
948
+ * Returns a new Dinero object that represents the multiplied value by the given factor.
949
+ *
950
+ * By default, fractional minor currency units are rounded using the **half to even** rule ([banker's rounding](http://wiki.c2.com/?BankersRounding)).
951
+ *
952
+ * Rounding *can* lead to accuracy issues as you chain many times. Consider a minimal amount of subsequent calculations for safer results.
953
+ * You can also specify a different `roundingMode` to better fit your needs.
954
+ *
955
+ * @param {Number} multiplier - The factor to multiply by.
956
+ * @param {String} [roundingMode='HALF_EVEN'] - The rounding mode to use: `'HALF_ODD'`, `'HALF_EVEN'`, `'HALF_UP'`, `'HALF_DOWN'`, `'HALF_TOWARDS_ZERO'`, `'HALF_AWAY_FROM_ZERO'` or `'DOWN'`.
957
+ *
958
+ * @example
959
+ * // returns a Dinero object with amount 1600
960
+ * Dinero({ amount: 400 }).multiply(4)
961
+ * @example
962
+ * // returns a Dinero object with amount 800
963
+ * Dinero({ amount: 400 }).multiply(2.001)
964
+ * @example
965
+ * // returns a Dinero object with amount 801
966
+ * Dinero({ amount: 400 }).multiply(2.00125, 'HALF_UP')
967
+ *
968
+ * @return {Dinero}
969
+ */
970
+ multiply: function multiply(multiplier) {
971
+ var roundingMode = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : globalRoundingMode;
972
+ return create.call(this, {
973
+ amount: calculator$1.round(calculator$1.multiply(this.getAmount(), multiplier), roundingMode)
974
+ });
975
+ },
976
+
977
+ /**
978
+ * Returns a new Dinero object that represents the divided value by the given factor.
979
+ *
980
+ * By default, fractional minor currency units are rounded using the **half to even** rule ([banker's rounding](http://wiki.c2.com/?BankersRounding)).
981
+ *
982
+ * Rounding *can* lead to accuracy issues as you chain many times. Consider a minimal amount of subsequent calculations for safer results.
983
+ * You can also specify a different `roundingMode` to better fit your needs.
984
+ *
985
+ * As rounding is applied, precision may be lost in the process. If you want to accurately split a Dinero object, use {@link module:Dinero~allocate allocate} instead.
986
+ *
987
+ * @param {Number} divisor - The factor to divide by.
988
+ * @param {String} [roundingMode='HALF_EVEN'] - The rounding mode to use: `'HALF_ODD'`, `'HALF_EVEN'`, `'HALF_UP'`, `'HALF_DOWN'`, `'HALF_TOWARDS_ZERO'`, `'HALF_AWAY_FROM_ZERO'` or `'DOWN'`.
989
+ *
990
+ * @example
991
+ * // returns a Dinero object with amount 100
992
+ * Dinero({ amount: 400 }).divide(4)
993
+ * @example
994
+ * // returns a Dinero object with amount 52
995
+ * Dinero({ amount: 105 }).divide(2)
996
+ * @example
997
+ * // returns a Dinero object with amount 53
998
+ * Dinero({ amount: 105 }).divide(2, 'HALF_UP')
999
+ *
1000
+ * @return {Dinero}
1001
+ */
1002
+ divide: function divide(divisor) {
1003
+ var roundingMode = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : globalRoundingMode;
1004
+ return create.call(this, {
1005
+ amount: calculator$1.round(calculator$1.divide(this.getAmount(), divisor), roundingMode)
1006
+ });
1007
+ },
1008
+
1009
+ /**
1010
+ * Returns a new Dinero object that represents a percentage of this.
1011
+ *
1012
+ * As rounding is applied, precision may be lost in the process. If you want to accurately split a Dinero object, use {@link module:Dinero~allocate allocate} instead.
1013
+ *
1014
+ * @param {Number} percentage - The percentage to extract (between 0 and 100).
1015
+ * @param {String} [roundingMode='HALF_EVEN'] - The rounding mode to use: `'HALF_ODD'`, `'HALF_EVEN'`, `'HALF_UP'`, `'HALF_DOWN'`, `'HALF_TOWARDS_ZERO'`, `'HALF_AWAY_FROM_ZERO'` or `'DOWN'`.
1016
+ *
1017
+ * @example
1018
+ * // returns a Dinero object with amount 5000
1019
+ * Dinero({ amount: 10000 }).percentage(50)
1020
+ * @example
1021
+ * // returns a Dinero object with amount 29
1022
+ * Dinero({ amount: 57 }).percentage(50, "HALF_ODD")
1023
+ *
1024
+ * @throws {RangeError} If `percentage` is out of range.
1025
+ *
1026
+ * @return {Dinero}
1027
+ */
1028
+ percentage: function percentage(_percentage) {
1029
+ var roundingMode = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : globalRoundingMode;
1030
+ assertPercentage(_percentage);
1031
+ return this.multiply(calculator$1.divide(_percentage, 100), roundingMode);
1032
+ },
1033
+
1034
+ /**
1035
+ * Allocates the amount of a Dinero object according to a list of ratios.
1036
+ *
1037
+ * Sometimes you need to split monetary values but percentages can't cut it without adding or losing pennies.
1038
+ * A good example is invoicing: let's say you need to bill $1,000.03 and you want a 50% downpayment.
1039
+ * If you use {@link module:Dinero~percentage percentage}, you'll get an accurate Dinero object but the amount won't be billable: you can't split a penny.
1040
+ * If you round it, you'll bill a penny extra.
1041
+ * With {@link module:Dinero~allocate allocate}, you can split a monetary amount then distribute the remainder as evenly as possible.
1042
+ *
1043
+ * You can use percentage style or ratio style for `ratios`: `[25, 75]` and `[1, 3]` will do the same thing.
1044
+ *
1045
+ * Since v1.8.0, you can use zero ratios (such as [0, 50, 50]). If there's a remainder to distribute, zero ratios are skipped and return a Dinero object with amount zero.
1046
+ *
1047
+ * @param {Number[]} ratios - The ratios to allocate the money to.
1048
+ *
1049
+ * @example
1050
+ * // returns an array of two Dinero objects
1051
+ * // the first one with an amount of 502
1052
+ * // the second one with an amount of 501
1053
+ * Dinero({ amount: 1003 }).allocate([50, 50])
1054
+ * @example
1055
+ * // returns an array of two Dinero objects
1056
+ * // the first one with an amount of 25
1057
+ * // the second one with an amount of 75
1058
+ * Dinero({ amount: 100 }).allocate([1, 3])
1059
+ * @example
1060
+ * // since version 1.8.0
1061
+ * // returns an array of three Dinero objects
1062
+ * // the first one with an amount of 0
1063
+ * // the second one with an amount of 502
1064
+ * // the third one with an amount of 501
1065
+ * Dinero({ amount: 1003 }).allocate([0, 50, 50])
1066
+ *
1067
+ * @throws {TypeError} If ratios are invalid.
1068
+ *
1069
+ * @return {Dinero[]}
1070
+ */
1071
+ allocate: function allocate(ratios) {
1072
+ var _this = this;
1073
+
1074
+ assertValidRatios(ratios);
1075
+ var total = ratios.reduce(function (a, b) {
1076
+ return calculator$1.add(a, b);
1077
+ });
1078
+ var remainder = this.getAmount();
1079
+ var shares = ratios.map(function (ratio) {
1080
+ var share = Math.floor(calculator$1.divide(calculator$1.multiply(_this.getAmount(), ratio), total));
1081
+ remainder = calculator$1.subtract(remainder, share);
1082
+ return create.call(_this, {
1083
+ amount: share
1084
+ });
1085
+ });
1086
+ var i = 0;
1087
+
1088
+ while (remainder > 0) {
1089
+ if (ratios[i] > 0) {
1090
+ shares[i] = shares[i].add(create.call(this, {
1091
+ amount: 1
1092
+ }));
1093
+ remainder = calculator$1.subtract(remainder, 1);
1094
+ }
1095
+
1096
+ i += 1;
1097
+ }
1098
+
1099
+ return shares;
1100
+ },
1101
+
1102
+ /**
1103
+ * Returns a Promise containing a new Dinero object converted to another currency.
1104
+ *
1105
+ * You have two options to provide the exchange rates:
1106
+ *
1107
+ * 1. **Use an exchange rate REST API, and let Dinero handle the fetching and conversion.**
1108
+ * This is a simple option if you have access to an exchange rate REST API and want Dinero to do the rest.
1109
+ * 2. **Fetch the exchange rates on your own and provide them directly.**
1110
+ * This is useful if you're fetching your rates from somewhere else (a file, a database), use a different protocol or query language than REST (SOAP, GraphQL) or want to fetch rates once and cache them instead of making new requests every time.
1111
+ *
1112
+ * **If you want to use a REST API**, you must provide a third-party endpoint yourself. Dinero doesn't come bundled with an exchange rates endpoint.
1113
+ *
1114
+ * Here are some exchange rate APIs you can use:
1115
+ *
1116
+ * * [Fixer](https://fixer.io)
1117
+ * * [Open Exchange Rates](https://openexchangerates.org)
1118
+ * * [Coinbase](https://api.coinbase.com/v2/exchange-rates)
1119
+ * * More [foreign](https://github.com/toddmotto/public-apis#currency-exchange) and [crypto](https://github.com/toddmotto/public-apis#cryptocurrency) exchange rate APIs.
1120
+ *
1121
+ * **If you want to fetch your own rates and provide them directly**, you need to pass a promise that resolves to the exchanges rates.
1122
+ *
1123
+ * In both cases, you need to specify at least:
1124
+ *
1125
+ * * a **destination currency**: the currency in which you want to convert your Dinero object. You can specify it with `currency`.
1126
+ * * an **endpoint**: the API URL to query exchange rates, with parameters, or a promise that resolves to the exchange rates. You can specify it with `options.endpoint`.
1127
+ * * a **property path**: the path to access the wanted rate in your API's JSON response (or the custom promise's payload). For example, with a response of:
1128
+ * ```json
1129
+ * {
1130
+ * "data": {
1131
+ * "base": "USD",
1132
+ * "destination": "EUR",
1133
+ * "rate": "0.827728919"
1134
+ * }
1135
+ * }
1136
+ * ```
1137
+ * Then the property path is `'data.rate'`. You can specify it with `options.propertyPath`.
1138
+ *
1139
+ * The base currency (the one of your Dinero object) and the destination currency can be used as "merge tags" with the mustache syntax, respectively `{{from}}` and `{{to}}`.
1140
+ * You can use these tags to refer to these values in `options.endpoint` and `options.propertyPath`.
1141
+ *
1142
+ * For example, if you need to specify the base currency as a query parameter, you can do the following:
1143
+ *
1144
+ * ```js
1145
+ * {
1146
+ * endpoint: 'https://yourexchangerates.api/latest?base={{from}}'
1147
+ * }
1148
+ * ```
1149
+ *
1150
+ * @param {String} currency - The destination currency, expressed as an {@link https://en.wikipedia.org/wiki/ISO_4217#Active_codes ISO 4217 currency code}.
1151
+ * @param {(String|Promise)} options.endpoint - The API endpoint to retrieve exchange rates. You can substitute this with a promise that resolves to the exchanges rates if you already have them.
1152
+ * @param {String} [options.propertyPath='rates.{{to}}'] - The property path to the rate.
1153
+ * @param {Object} [options.headers] - The HTTP headers to provide, if needed.
1154
+ * @param {String} [options.roundingMode='HALF_EVEN'] - The rounding mode to use: `'HALF_ODD'`, `'HALF_EVEN'`, `'HALF_UP'`, `'HALF_DOWN'`, `'HALF_TOWARDS_ZERO'`, `'HALF_AWAY_FROM_ZERO'` or `'DOWN'`.
1155
+ *
1156
+ * @example
1157
+ * // your global API parameters
1158
+ * Dinero.globalExchangeRatesApi = { ... }
1159
+ *
1160
+ * // returns a Promise containing a Dinero object with the destination currency
1161
+ * // and the initial amount converted to the new currency.
1162
+ * Dinero({ amount: 500 }).convert('EUR')
1163
+ * @example
1164
+ * // returns a Promise containing a Dinero object,
1165
+ * // with specific API parameters and rounding mode for this specific instance.
1166
+ * Dinero({ amount: 500 })
1167
+ * .convert('XBT', {
1168
+ * endpoint: 'https://yourexchangerates.api/latest?base={{from}}',
1169
+ * propertyPath: 'data.rates.{{to}}',
1170
+ * headers: {
1171
+ * 'user-key': 'xxxxxxxxx'
1172
+ * },
1173
+ * roundingMode: 'HALF_UP'
1174
+ * })
1175
+ * @example
1176
+ * // usage with exchange rates provided as a custom promise
1177
+ * // using the default `propertyPath` format (so it doesn't have to be specified)
1178
+ * const rates = {
1179
+ * rates: {
1180
+ * EUR: 0.81162
1181
+ * }
1182
+ * }
1183
+ *
1184
+ * Dinero({ amount: 500 })
1185
+ * .convert('EUR', {
1186
+ * endpoint: new Promise(resolve => resolve(rates))
1187
+ * })
1188
+ * @example
1189
+ * // usage with Promise.prototype.then and Promise.prototype.catch
1190
+ * Dinero({ amount: 500 })
1191
+ * .convert('EUR')
1192
+ * .then(dinero => {
1193
+ * dinero.getCurrency() // returns 'EUR'
1194
+ * })
1195
+ * .catch(err => {
1196
+ * // handle errors
1197
+ * })
1198
+ * @example
1199
+ * // usage with async/await
1200
+ * (async () => {
1201
+ * const price = await Dinero({ amount: 500 }).convert('EUR')
1202
+ * price.getCurrency() // returns 'EUR'
1203
+ * })()
1204
+ *
1205
+ * @return {Promise}
1206
+ */
1207
+ convert: function convert(currency) {
1208
+ var _this2 = this;
1209
+
1210
+ var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
1211
+ _ref$endpoint = _ref.endpoint,
1212
+ endpoint = _ref$endpoint === void 0 ? globalExchangeRatesApi.endpoint : _ref$endpoint,
1213
+ _ref$propertyPath = _ref.propertyPath,
1214
+ propertyPath = _ref$propertyPath === void 0 ? globalExchangeRatesApi.propertyPath || 'rates.{{to}}' : _ref$propertyPath,
1215
+ _ref$headers = _ref.headers,
1216
+ headers = _ref$headers === void 0 ? globalExchangeRatesApi.headers : _ref$headers,
1217
+ _ref$roundingMode = _ref.roundingMode,
1218
+ roundingMode = _ref$roundingMode === void 0 ? globalRoundingMode : _ref$roundingMode;
1219
+
1220
+ var options = Object.assign({}, {
1221
+ endpoint: endpoint,
1222
+ propertyPath: propertyPath,
1223
+ headers: headers,
1224
+ roundingMode: roundingMode
1225
+ });
1226
+ return CurrencyConverter(options).getExchangeRate(this.getCurrency(), currency).then(function (rate) {
1227
+ assert(!isUndefined(rate), "No rate was found for the destination currency \"".concat(currency, "\"."), TypeError);
1228
+ return create.call(_this2, {
1229
+ amount: calculator$1.round(calculator$1.multiply(_this2.getAmount(), parseFloat(rate)), options.roundingMode),
1230
+ currency: currency
1231
+ });
1232
+ });
1233
+ },
1234
+
1235
+ /**
1236
+ * Checks whether the value represented by this object equals to the other.
1237
+ *
1238
+ * @param {Dinero} comparator - The Dinero object to compare to.
1239
+ *
1240
+ * @example
1241
+ * // returns true
1242
+ * Dinero({ amount: 500, currency: 'EUR' }).equalsTo(Dinero({ amount: 500, currency: 'EUR' }))
1243
+ * @example
1244
+ * // returns false
1245
+ * Dinero({ amount: 500, currency: 'EUR' }).equalsTo(Dinero({ amount: 800, currency: 'EUR' }))
1246
+ * @example
1247
+ * // returns false
1248
+ * Dinero({ amount: 500, currency: 'USD' }).equalsTo(Dinero({ amount: 500, currency: 'EUR' }))
1249
+ * @example
1250
+ * // returns false
1251
+ * Dinero({ amount: 500, currency: 'USD' }).equalsTo(Dinero({ amount: 800, currency: 'EUR' }))
1252
+ * @example
1253
+ * // returns true
1254
+ * Dinero({ amount: 1000, currency: 'EUR', precision: 2 }).equalsTo(Dinero({ amount: 10000, currency: 'EUR', precision: 3 }))
1255
+ * @example
1256
+ * // returns false
1257
+ * Dinero({ amount: 10000, currency: 'EUR', precision: 2 }).equalsTo(Dinero({ amount: 10000, currency: 'EUR', precision: 3 }))
1258
+ *
1259
+ * @return {Boolean}
1260
+ */
1261
+ equalsTo: function equalsTo(comparator) {
1262
+ return this.hasSameAmount(comparator) && this.hasSameCurrency(comparator);
1263
+ },
1264
+
1265
+ /**
1266
+ * Checks whether the value represented by this object is less than the other.
1267
+ *
1268
+ * @param {Dinero} comparator - The Dinero object to compare to.
1269
+ *
1270
+ * @example
1271
+ * // returns true
1272
+ * Dinero({ amount: 500 }).lessThan(Dinero({ amount: 800 }))
1273
+ * @example
1274
+ * // returns false
1275
+ * Dinero({ amount: 800 }).lessThan(Dinero({ amount: 500 }))
1276
+ * @example
1277
+ * // returns true
1278
+ * Dinero({ amount: 5000, precision: 3 }).lessThan(Dinero({ amount: 800 }))
1279
+ * @example
1280
+ * // returns false
1281
+ * Dinero({ amount: 800 }).lessThan(Dinero({ amount: 5000, precision: 3 }))
1282
+ *
1283
+ * @throws {TypeError} If `comparator` has a different currency.
1284
+ *
1285
+ * @return {Boolean}
1286
+ */
1287
+ lessThan: function lessThan(comparator) {
1288
+ assertSameCurrency.call(this, comparator);
1289
+ var comparators = Dinero.normalizePrecision([this, comparator]);
1290
+ return comparators[0].getAmount() < comparators[1].getAmount();
1291
+ },
1292
+
1293
+ /**
1294
+ * Checks whether the value represented by this object is less than or equal to the other.
1295
+ *
1296
+ * @param {Dinero} comparator - The Dinero object to compare to.
1297
+ *
1298
+ * @example
1299
+ * // returns true
1300
+ * Dinero({ amount: 500 }).lessThanOrEqual(Dinero({ amount: 800 }))
1301
+ * @example
1302
+ * // returns true
1303
+ * Dinero({ amount: 500 }).lessThanOrEqual(Dinero({ amount: 500 }))
1304
+ * @example
1305
+ * // returns false
1306
+ * Dinero({ amount: 500 }).lessThanOrEqual(Dinero({ amount: 300 }))
1307
+ * @example
1308
+ * // returns true
1309
+ * Dinero({ amount: 5000, precision: 3 }).lessThanOrEqual(Dinero({ amount: 800 }))
1310
+ * @example
1311
+ * // returns true
1312
+ * Dinero({ amount: 5000, precision: 3 }).lessThanOrEqual(Dinero({ amount: 500 }))
1313
+ * @example
1314
+ * // returns false
1315
+ * Dinero({ amount: 800 }).lessThanOrEqual(Dinero({ amount: 5000, precision: 3 }))
1316
+ *
1317
+ * @throws {TypeError} If `comparator` has a different currency.
1318
+ *
1319
+ * @return {Boolean}
1320
+ */
1321
+ lessThanOrEqual: function lessThanOrEqual(comparator) {
1322
+ assertSameCurrency.call(this, comparator);
1323
+ var comparators = Dinero.normalizePrecision([this, comparator]);
1324
+ return comparators[0].getAmount() <= comparators[1].getAmount();
1325
+ },
1326
+
1327
+ /**
1328
+ * Checks whether the value represented by this object is greater than the other.
1329
+ *
1330
+ * @param {Dinero} comparator - The Dinero object to compare to.
1331
+ *
1332
+ * @example
1333
+ * // returns false
1334
+ * Dinero({ amount: 500 }).greaterThan(Dinero({ amount: 800 }))
1335
+ * @example
1336
+ * // returns true
1337
+ * Dinero({ amount: 800 }).greaterThan(Dinero({ amount: 500 }))
1338
+ * @example
1339
+ * // returns true
1340
+ * Dinero({ amount: 800 }).greaterThan(Dinero({ amount: 5000, precision: 3 }))
1341
+ * @example
1342
+ * // returns false
1343
+ * Dinero({ amount: 5000, precision: 3 }).greaterThan(Dinero({ amount: 800 }))
1344
+ *
1345
+ * @throws {TypeError} If `comparator` has a different currency.
1346
+ *
1347
+ * @return {Boolean}
1348
+ */
1349
+ greaterThan: function greaterThan(comparator) {
1350
+ assertSameCurrency.call(this, comparator);
1351
+ var comparators = Dinero.normalizePrecision([this, comparator]);
1352
+ return comparators[0].getAmount() > comparators[1].getAmount();
1353
+ },
1354
+
1355
+ /**
1356
+ * Checks whether the value represented by this object is greater than or equal to the other.
1357
+ *
1358
+ * @param {Dinero} comparator - The Dinero object to compare to.
1359
+ *
1360
+ * @example
1361
+ * // returns true
1362
+ * Dinero({ amount: 500 }).greaterThanOrEqual(Dinero({ amount: 300 }))
1363
+ * @example
1364
+ * // returns true
1365
+ * Dinero({ amount: 500 }).greaterThanOrEqual(Dinero({ amount: 500 }))
1366
+ * @example
1367
+ * // returns false
1368
+ * Dinero({ amount: 500 }).greaterThanOrEqual(Dinero({ amount: 800 }))
1369
+ * @example
1370
+ * // returns true
1371
+ * Dinero({ amount: 800 }).greaterThanOrEqual(Dinero({ amount: 5000, precision: 3 }))
1372
+ * @example
1373
+ * // returns true
1374
+ * Dinero({ amount: 500 }).greaterThanOrEqual(Dinero({ amount: 5000, precision: 3 }))
1375
+ * @example
1376
+ * // returns false
1377
+ * Dinero({ amount: 5000, precision: 3 }).greaterThanOrEqual(Dinero({ amount: 800 }))
1378
+ *
1379
+ * @throws {TypeError} If `comparator` has a different currency.
1380
+ *
1381
+ * @return {Boolean}
1382
+ */
1383
+ greaterThanOrEqual: function greaterThanOrEqual(comparator) {
1384
+ assertSameCurrency.call(this, comparator);
1385
+ var comparators = Dinero.normalizePrecision([this, comparator]);
1386
+ return comparators[0].getAmount() >= comparators[1].getAmount();
1387
+ },
1388
+
1389
+ /**
1390
+ * Checks if the value represented by this object is zero.
1391
+ *
1392
+ * @example
1393
+ * // returns true
1394
+ * Dinero({ amount: 0 }).isZero()
1395
+ * @example
1396
+ * // returns false
1397
+ * Dinero({ amount: 100 }).isZero()
1398
+ *
1399
+ * @return {Boolean}
1400
+ */
1401
+ isZero: function isZero() {
1402
+ return this.getAmount() === 0;
1403
+ },
1404
+
1405
+ /**
1406
+ * Checks if the value represented by this object is positive.
1407
+ *
1408
+ * @example
1409
+ * // returns false
1410
+ * Dinero({ amount: -10 }).isPositive()
1411
+ * @example
1412
+ * // returns true
1413
+ * Dinero({ amount: 10 }).isPositive()
1414
+ * @example
1415
+ * // returns true
1416
+ * Dinero({ amount: 0 }).isPositive()
1417
+ *
1418
+ * @return {Boolean}
1419
+ */
1420
+ isPositive: function isPositive() {
1421
+ return this.getAmount() >= 0;
1422
+ },
1423
+
1424
+ /**
1425
+ * Checks if the value represented by this object is negative.
1426
+ *
1427
+ * @example
1428
+ * // returns true
1429
+ * Dinero({ amount: -10 }).isNegative()
1430
+ * @example
1431
+ * // returns false
1432
+ * Dinero({ amount: 10 }).isNegative()
1433
+ * @example
1434
+ * // returns false
1435
+ * Dinero({ amount: 0 }).isNegative()
1436
+ *
1437
+ * @return {Boolean}
1438
+ */
1439
+ isNegative: function isNegative() {
1440
+ return this.getAmount() < 0;
1441
+ },
1442
+
1443
+ /**
1444
+ * Checks if this has minor currency units.
1445
+ * Deprecates {@link module:Dinero~hasCents hasCents}.
1446
+ *
1447
+ * @example
1448
+ * // returns false
1449
+ * Dinero({ amount: 1100 }).hasSubUnits()
1450
+ * @example
1451
+ * // returns true
1452
+ * Dinero({ amount: 1150 }).hasSubUnits()
1453
+ *
1454
+ * @return {Boolean}
1455
+ */
1456
+ hasSubUnits: function hasSubUnits() {
1457
+ return calculator$1.modulo(this.getAmount(), Math.pow(10, precision)) !== 0;
1458
+ },
1459
+
1460
+ /**
1461
+ * Checks if this has minor currency units.
1462
+ *
1463
+ * @deprecated since version 1.4.0, will be removed in 2.0.0
1464
+ * Use {@link module:Dinero~hasSubUnits hasSubUnits} instead.
1465
+ *
1466
+ * @example
1467
+ * // returns false
1468
+ * Dinero({ amount: 1100 }).hasCents()
1469
+ * @example
1470
+ * // returns true
1471
+ * Dinero({ amount: 1150 }).hasCents()
1472
+ *
1473
+ * @return {Boolean}
1474
+ */
1475
+ hasCents: function hasCents() {
1476
+ return calculator$1.modulo(this.getAmount(), Math.pow(10, precision)) !== 0;
1477
+ },
1478
+
1479
+ /**
1480
+ * Checks whether the currency represented by this object equals to the other.
1481
+ *
1482
+ * @param {Dinero} comparator - The Dinero object to compare to.
1483
+ *
1484
+ * @example
1485
+ * // returns true
1486
+ * Dinero({ amount: 2000, currency: 'EUR' }).hasSameCurrency(Dinero({ amount: 1000, currency: 'EUR' }))
1487
+ * @example
1488
+ * // returns false
1489
+ * Dinero({ amount: 1000, currency: 'EUR' }).hasSameCurrency(Dinero({ amount: 1000, currency: 'USD' }))
1490
+ *
1491
+ * @return {Boolean}
1492
+ */
1493
+ hasSameCurrency: function hasSameCurrency(comparator) {
1494
+ return this.getCurrency() === comparator.getCurrency();
1495
+ },
1496
+
1497
+ /**
1498
+ * Checks whether the amount represented by this object equals to the other.
1499
+ *
1500
+ * @param {Dinero} comparator - The Dinero object to compare to.
1501
+ *
1502
+ * @example
1503
+ * // returns true
1504
+ * Dinero({ amount: 1000, currency: 'EUR' }).hasSameAmount(Dinero({ amount: 1000 }))
1505
+ * @example
1506
+ * // returns false
1507
+ * Dinero({ amount: 2000, currency: 'EUR' }).hasSameAmount(Dinero({ amount: 1000, currency: 'EUR' }))
1508
+ * @example
1509
+ * // returns true
1510
+ * Dinero({ amount: 1000, currency: 'EUR', precision: 2 }).hasSameAmount(Dinero({ amount: 10000, precision: 3 }))
1511
+ * @example
1512
+ * // returns false
1513
+ * Dinero({ amount: 10000, currency: 'EUR', precision: 2 }).hasSameAmount(Dinero({ amount: 10000, precision: 3 }))
1514
+ *
1515
+ * @return {Boolean}
1516
+ */
1517
+ hasSameAmount: function hasSameAmount(comparator) {
1518
+ var comparators = Dinero.normalizePrecision([this, comparator]);
1519
+ return comparators[0].getAmount() === comparators[1].getAmount();
1520
+ },
1521
+
1522
+ /**
1523
+ * Returns this object formatted as a string.
1524
+ *
1525
+ * The format is a mask which defines how the output string will be formatted.
1526
+ * It defines whether to display a currency, in what format, how many fraction digits to display and whether to use grouping separators.
1527
+ * The output is formatted according to the applying locale.
1528
+ *
1529
+ * Object | Format | String
1530
+ * :--------------------------- | :---------------- | :---
1531
+ * `Dinero({ amount: 500050 })` | `'$0,0.00'` | $5,000.50
1532
+ * `Dinero({ amount: 500050 })` | `'$0,0'` | $5,001
1533
+ * `Dinero({ amount: 500050 })` | `'$0'` | $5001
1534
+ * `Dinero({ amount: 500050 })` | `'$0.0'` | $5000.5
1535
+ * `Dinero({ amount: 500050 })` | `'USD0,0.0'` | USD5,000.5
1536
+ * `Dinero({ amount: 500050 })` | `'0,0.0 dollar'` | 5,000.5 dollars
1537
+ *
1538
+ * Don't try to substitute the `$` sign or the `USD` code with your target currency, nor adapt the format string to the exact format you want.
1539
+ * The format is a mask which defines a pattern and returns a valid, localized currency string.
1540
+ * If you want to display the object in a custom way, either use {@link module:Dinero~getAmount getAmount}, {@link module:Dinero~toUnit toUnit} or {@link module:Dinero~toRoundedUnit toRoundedUnit} and manipulate the output string as you wish.
1541
+ *
1542
+ * {@link module:Dinero~toFormat toFormat} wraps around `Number.prototype.toLocaleString`. For that reason, **format will vary depending on how it's implemented in the end user's environment**.
1543
+ *
1544
+ * You can also use `toLocaleString` directly:
1545
+ * `Dinero().toRoundedUnit(digits, roundingMode).toLocaleString(locale, options)`.
1546
+ *
1547
+ * By default, amounts are rounded using the **half away from zero** rule ([commercial rounding](https://en.wikipedia.org/wiki/Rounding#Round_half_away_from_zero)).
1548
+ * You can also specify a different `roundingMode` to better fit your needs.
1549
+ *
1550
+ * @param {String} [format='$0,0.00'] - The format mask to format to.
1551
+ * @param {String} [roundingMode='HALF_AWAY_FROM_ZERO'] - The rounding mode to use: `'HALF_ODD'`, `'HALF_EVEN'`, `'HALF_UP'`, `'HALF_DOWN'`, `'HALF_TOWARDS_ZERO'`, `'HALF_AWAY_FROM_ZERO'` or `'DOWN'`.
1552
+ *
1553
+ * @example
1554
+ * // returns $2,000
1555
+ * Dinero({ amount: 200000 }).toFormat('$0,0')
1556
+ * @example
1557
+ * // returns €50.5
1558
+ * Dinero({ amount: 5050, currency: 'EUR' }).toFormat('$0,0.0')
1559
+ * @example
1560
+ * // returns 100 euros
1561
+ * Dinero({ amount: 10000, currency: 'EUR' }).setLocale('fr-FR').toFormat('0,0 dollar')
1562
+ * @example
1563
+ * // returns 2000
1564
+ * Dinero({ amount: 200000, currency: 'EUR' }).toFormat()
1565
+ * @example
1566
+ * // returns $10
1567
+ * Dinero({ amount: 1050 }).toFormat('$0', 'HALF_EVEN')
1568
+ *
1569
+ * @return {String}
1570
+ */
1571
+ toFormat: function toFormat() {
1572
+ var format = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : globalFormat;
1573
+ var roundingMode = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : globalFormatRoundingMode;
1574
+ var formatter = Format(format);
1575
+ return this.toRoundedUnit(formatter.getMinimumFractionDigits(), roundingMode).toLocaleString(this.getLocale(), {
1576
+ currencyDisplay: formatter.getCurrencyDisplay(),
1577
+ useGrouping: formatter.getUseGrouping(),
1578
+ minimumFractionDigits: formatter.getMinimumFractionDigits(),
1579
+ style: formatter.getStyle(),
1580
+ currency: this.getCurrency()
1581
+ });
1582
+ },
1583
+
1584
+ /**
1585
+ * Returns the amount represented by this object in units.
1586
+ *
1587
+ * @example
1588
+ * // returns 10.5
1589
+ * Dinero({ amount: 1050 }).toUnit()
1590
+ * @example
1591
+ * // returns 10.545
1592
+ * Dinero({ amount: 10545, precision: 3 }).toUnit()
1593
+ *
1594
+ * @return {Number}
1595
+ */
1596
+ toUnit: function toUnit() {
1597
+ return calculator$1.divide(this.getAmount(), Math.pow(10, precision));
1598
+ },
1599
+
1600
+ /**
1601
+ * Returns the amount represented by this object in rounded units.
1602
+ *
1603
+ * By default, the method uses the **half away from zero** rule ([commercial rounding](https://en.wikipedia.org/wiki/Rounding#Round_half_away_from_zero)).
1604
+ * You can also specify a different `roundingMode` to better fit your needs.
1605
+ *
1606
+ * @example
1607
+ * // returns 10.6
1608
+ * Dinero({ amount: 1055 }).toRoundedUnit(1)
1609
+ * @example
1610
+ * // returns 10
1611
+ * Dinero({ amount: 1050 }).toRoundedUnit(0, 'HALF_EVEN')
1612
+ *
1613
+ * @param {Number} digits - The number of fraction digits to round to.
1614
+ * @param {String} [roundingMode='HALF_AWAY_FROM_ZERO'] - The rounding mode to use: `'HALF_ODD'`, `'HALF_EVEN'`, `'HALF_UP'`, `'HALF_DOWN'`, `'HALF_TOWARDS_ZERO'`, `'HALF_AWAY_FROM_ZERO'` or `'DOWN'`.
1615
+ *
1616
+ * @return {Number}
1617
+ */
1618
+ toRoundedUnit: function toRoundedUnit(digits) {
1619
+ var roundingMode = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : globalFormatRoundingMode;
1620
+ var factor = Math.pow(10, digits);
1621
+ return calculator$1.divide(calculator$1.round(calculator$1.multiply(this.toUnit(), factor), roundingMode), factor);
1622
+ },
1623
+
1624
+ /**
1625
+ * Returns the object's data as an object literal.
1626
+ *
1627
+ * @example
1628
+ * // returns { amount: 500, currency: 'EUR', precision: 2 }
1629
+ * Dinero({ amount: 500, currency: 'EUR', precision: 2 }).toObject()
1630
+ *
1631
+ * @return {Object}
1632
+ */
1633
+ toObject: function toObject() {
1634
+ return {
1635
+ amount: amount,
1636
+ currency: currency,
1637
+ precision: precision
1638
+ };
1639
+ },
1640
+
1641
+ /**
1642
+ * Returns the object's data as an object literal.
1643
+ *
1644
+ * Alias of {@link module:Dinero~toObject toObject}.
1645
+ * It is defined so that calling `JSON.stringify` on a Dinero object will automatically extract the relevant data.
1646
+ *
1647
+ * @example
1648
+ * // returns '{"amount":500,"currency":"EUR","precision":2}'
1649
+ * JSON.stringify(Dinero({ amount: 500, currency: 'EUR', precision: 2 }))
1650
+ *
1651
+ * @return {Object}
1652
+ */
1653
+ toJSON: function toJSON() {
1654
+ return this.toObject();
1655
+ }
1656
+ };
1657
+ };
1658
+
1659
+ var dinero = Object.assign(Dinero, Defaults, Globals, Static);
1660
+
1661
+ return dinero;
1662
+
1663
+ })));