monetra 1.0.1 → 1.1.0
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.
- package/dist/index.d.mts +227 -25
- package/dist/index.d.ts +227 -25
- package/dist/index.js +439 -63
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +431 -62
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -2
- package/dist/index 2.js +0 -424
- package/dist/index 2.mjs +0 -381
- package/dist/index.d 2.mts +0 -76
- package/dist/index.d 2.ts +0 -76
- package/dist/index.js 2.map +0 -1
- package/dist/index.mjs 2.map +0 -1
package/dist/index.d.ts
CHANGED
|
@@ -74,50 +74,82 @@ declare class Money {
|
|
|
74
74
|
*/
|
|
75
75
|
readonly currency: Currency;
|
|
76
76
|
private constructor();
|
|
77
|
+
private static resolveCurrency;
|
|
77
78
|
/**
|
|
78
79
|
* Creates a Money instance from minor units (e.g., cents).
|
|
79
80
|
*
|
|
80
81
|
* @param minor - The amount in minor units. Can be a number or BigInt.
|
|
81
|
-
* @param currency - The currency of the money.
|
|
82
|
+
* @param currency - The currency of the money (object or code string).
|
|
82
83
|
* @returns A new Money instance.
|
|
83
84
|
* @example
|
|
84
85
|
* const m = Money.fromMinor(100, USD); // $1.00
|
|
86
|
+
* const m2 = Money.fromMinor(100, 'USD'); // $1.00
|
|
85
87
|
*/
|
|
86
|
-
static fromMinor(minor: bigint | number, currency: Currency): Money;
|
|
88
|
+
static fromMinor(minor: bigint | number, currency: Currency | string): Money;
|
|
87
89
|
/**
|
|
88
90
|
* Creates a Money instance from major units (e.g., "10.50").
|
|
89
91
|
*
|
|
90
92
|
* @param amount - The amount as a string. Must be a valid decimal number.
|
|
91
|
-
* @param currency - The currency of the money.
|
|
93
|
+
* @param currency - The currency of the money (object or code string).
|
|
92
94
|
* @returns A new Money instance.
|
|
93
95
|
* @throws {InvalidPrecisionError} If the amount has more decimal places than the currency allows.
|
|
94
96
|
* @example
|
|
95
97
|
* const m = Money.fromMajor("10.50", USD); // $10.50
|
|
96
98
|
*/
|
|
97
|
-
static fromMajor(amount: string, currency: Currency): Money;
|
|
99
|
+
static fromMajor(amount: string, currency: Currency | string): Money;
|
|
100
|
+
/**
|
|
101
|
+
* Creates a Money instance from a floating-point number.
|
|
102
|
+
*
|
|
103
|
+
* ⚠️ WARNING: Floating-point numbers can have precision issues.
|
|
104
|
+
* Prefer `Money.fromMajor("10.50", currency)` for exact values.
|
|
105
|
+
*
|
|
106
|
+
* @param amount - The float amount in major units.
|
|
107
|
+
* @param currency - The currency.
|
|
108
|
+
* @param options - Options for handling precision.
|
|
109
|
+
* @returns A new Money instance.
|
|
110
|
+
*/
|
|
111
|
+
static fromFloat(amount: number, currency: Currency | string, options?: {
|
|
112
|
+
rounding?: RoundingMode;
|
|
113
|
+
suppressWarning?: boolean;
|
|
114
|
+
}): Money;
|
|
115
|
+
/**
|
|
116
|
+
* Returns the minimum of the provided Money values.
|
|
117
|
+
* @param values - Money values to compare (must all be same currency).
|
|
118
|
+
* @returns The Money with the smallest amount.
|
|
119
|
+
* @throws {CurrencyMismatchError} If currencies don't match.
|
|
120
|
+
*/
|
|
121
|
+
static min(...values: Money[]): Money;
|
|
122
|
+
/**
|
|
123
|
+
* Returns the maximum of the provided Money values.
|
|
124
|
+
* @param values - Money values to compare (must all be same currency).
|
|
125
|
+
* @returns The Money with the largest amount.
|
|
126
|
+
* @throws {CurrencyMismatchError} If currencies don't match.
|
|
127
|
+
*/
|
|
128
|
+
static max(...values: Money[]): Money;
|
|
98
129
|
/**
|
|
99
130
|
* Creates a Money instance representing zero in the given currency.
|
|
100
131
|
*
|
|
101
132
|
* @param currency - The currency.
|
|
102
133
|
* @returns A new Money instance with value 0.
|
|
103
134
|
*/
|
|
104
|
-
static zero(currency: Currency): Money;
|
|
135
|
+
static zero(currency: Currency | string): Money;
|
|
136
|
+
private resolveOther;
|
|
105
137
|
/**
|
|
106
138
|
* Adds another Money value to this one.
|
|
107
139
|
*
|
|
108
|
-
* @param other - The
|
|
140
|
+
* @param other - The value to add (Money, minor units as number/bigint, or major units as string).
|
|
109
141
|
* @returns A new Money instance representing the sum.
|
|
110
142
|
* @throws {CurrencyMismatchError} If the currencies do not match.
|
|
111
143
|
*/
|
|
112
|
-
add(other: Money): Money;
|
|
144
|
+
add(other: Money | number | bigint | string): Money;
|
|
113
145
|
/**
|
|
114
146
|
* Subtracts another Money value from this one.
|
|
115
147
|
*
|
|
116
|
-
* @param other - The
|
|
148
|
+
* @param other - The value to subtract (Money, minor units as number/bigint, or major units as string).
|
|
117
149
|
* @returns A new Money instance representing the difference.
|
|
118
150
|
* @throws {CurrencyMismatchError} If the currencies do not match.
|
|
119
151
|
*/
|
|
120
|
-
subtract(other: Money): Money;
|
|
152
|
+
subtract(other: Money | number | bigint | string): Money;
|
|
121
153
|
/**
|
|
122
154
|
* Multiplies this Money value by a scalar.
|
|
123
155
|
*
|
|
@@ -129,6 +161,28 @@ declare class Money {
|
|
|
129
161
|
multiply(multiplier: string | number, options?: {
|
|
130
162
|
rounding?: RoundingMode;
|
|
131
163
|
}): Money;
|
|
164
|
+
/**
|
|
165
|
+
* Divides this Money value by a divisor.
|
|
166
|
+
*
|
|
167
|
+
* @param divisor - The number to divide by.
|
|
168
|
+
* @param options - Options for rounding if the result is not an integer.
|
|
169
|
+
* @returns A new Money instance representing the quotient.
|
|
170
|
+
* @throws {RoundingRequiredError} If the result is fractional and no rounding mode is provided.
|
|
171
|
+
* @throws {Error} If divisor is zero.
|
|
172
|
+
*/
|
|
173
|
+
divide(divisor: number | string, options?: {
|
|
174
|
+
rounding?: RoundingMode;
|
|
175
|
+
}): Money;
|
|
176
|
+
/**
|
|
177
|
+
* Returns the absolute value of this Money.
|
|
178
|
+
* @returns A new Money instance with the absolute value.
|
|
179
|
+
*/
|
|
180
|
+
abs(): Money;
|
|
181
|
+
/**
|
|
182
|
+
* Returns the negated value of this Money.
|
|
183
|
+
* @returns A new Money instance with the negated value.
|
|
184
|
+
*/
|
|
185
|
+
negate(): Money;
|
|
132
186
|
/**
|
|
133
187
|
* Allocates (splits) this Money value according to a list of ratios.
|
|
134
188
|
*
|
|
@@ -148,30 +202,71 @@ declare class Money {
|
|
|
148
202
|
format(options?: {
|
|
149
203
|
locale?: string;
|
|
150
204
|
symbol?: boolean;
|
|
205
|
+
display?: "symbol" | "code" | "name";
|
|
151
206
|
}): string;
|
|
152
207
|
/**
|
|
153
208
|
* Checks if this Money value is equal to another.
|
|
154
209
|
*
|
|
155
|
-
* @param other - The other Money value.
|
|
210
|
+
* @param other - The other Money value (Money, minor units as number/bigint, or major units as string).
|
|
156
211
|
* @returns True if amounts and currencies are equal.
|
|
157
212
|
*/
|
|
158
|
-
equals(other: Money): boolean;
|
|
213
|
+
equals(other: Money | number | bigint | string): boolean;
|
|
159
214
|
/**
|
|
160
215
|
* Checks if this Money value is greater than another.
|
|
161
216
|
*
|
|
162
|
-
* @param other - The other Money value.
|
|
217
|
+
* @param other - The other Money value (Money, minor units as number/bigint, or major units as string).
|
|
163
218
|
* @returns True if this value is greater.
|
|
164
219
|
* @throws {CurrencyMismatchError} If the currencies do not match.
|
|
165
220
|
*/
|
|
166
|
-
greaterThan(other: Money): boolean;
|
|
221
|
+
greaterThan(other: Money | number | bigint | string): boolean;
|
|
167
222
|
/**
|
|
168
223
|
* Checks if this Money value is less than another.
|
|
169
224
|
*
|
|
170
|
-
* @param other - The other Money value.
|
|
225
|
+
* @param other - The other Money value (Money, minor units as number/bigint, or major units as string).
|
|
171
226
|
* @returns True if this value is less.
|
|
172
227
|
* @throws {CurrencyMismatchError} If the currencies do not match.
|
|
173
228
|
*/
|
|
174
|
-
lessThan(other: Money): boolean;
|
|
229
|
+
lessThan(other: Money | number | bigint | string): boolean;
|
|
230
|
+
/**
|
|
231
|
+
* Checks if this Money value is greater than or equal to another.
|
|
232
|
+
*/
|
|
233
|
+
greaterThanOrEqual(other: Money | number | bigint | string): boolean;
|
|
234
|
+
/**
|
|
235
|
+
* Checks if this Money value is less than or equal to another.
|
|
236
|
+
*/
|
|
237
|
+
lessThanOrEqual(other: Money | number | bigint | string): boolean;
|
|
238
|
+
/**
|
|
239
|
+
* Checks if this Money value is positive (greater than zero).
|
|
240
|
+
*/
|
|
241
|
+
isPositive(): boolean;
|
|
242
|
+
/**
|
|
243
|
+
* Compares this Money to another, returning -1, 0, or 1.
|
|
244
|
+
* Useful for sorting.
|
|
245
|
+
*/
|
|
246
|
+
compare(other: Money | number | bigint | string): -1 | 0 | 1;
|
|
247
|
+
/**
|
|
248
|
+
* Calculates a percentage of the money.
|
|
249
|
+
* @param percent - The percentage (e.g., 50 for 50%).
|
|
250
|
+
* @param rounding - Rounding mode (defaults to HALF_EVEN).
|
|
251
|
+
*/
|
|
252
|
+
percentage(percent: number, rounding?: RoundingMode): Money;
|
|
253
|
+
/**
|
|
254
|
+
* Adds a percentage to the money.
|
|
255
|
+
* @param percent - The percentage to add.
|
|
256
|
+
* @param rounding - Rounding mode.
|
|
257
|
+
*/
|
|
258
|
+
addPercent(percent: number, rounding?: RoundingMode): Money;
|
|
259
|
+
/**
|
|
260
|
+
* Subtracts a percentage from the money.
|
|
261
|
+
* @param percent - The percentage to subtract.
|
|
262
|
+
* @param rounding - Rounding mode.
|
|
263
|
+
*/
|
|
264
|
+
subtractPercent(percent: number, rounding?: RoundingMode): Money;
|
|
265
|
+
/**
|
|
266
|
+
* Splits the money into equal parts.
|
|
267
|
+
* @param parts - Number of parts.
|
|
268
|
+
*/
|
|
269
|
+
split(parts: number): Money[];
|
|
175
270
|
/**
|
|
176
271
|
* Checks if this Money value is zero.
|
|
177
272
|
*
|
|
@@ -184,6 +279,92 @@ declare class Money {
|
|
|
184
279
|
* @returns True if the amount is negative.
|
|
185
280
|
*/
|
|
186
281
|
isNegative(): boolean;
|
|
282
|
+
/**
|
|
283
|
+
* Returns a JSON representation of the Money object.
|
|
284
|
+
*
|
|
285
|
+
* @returns An object with amount (string), currency (code), and precision.
|
|
286
|
+
*/
|
|
287
|
+
toJSON(): {
|
|
288
|
+
amount: string;
|
|
289
|
+
currency: string;
|
|
290
|
+
precision: number;
|
|
291
|
+
};
|
|
292
|
+
/**
|
|
293
|
+
* Returns a string representation of the Money object (formatted).
|
|
294
|
+
*/
|
|
295
|
+
toString(): string;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
declare function assertSameCurrency(a: Money, b: Money): void;
|
|
299
|
+
declare function assertNonNegative(money: Money): void;
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* Handles currency conversion using exchange rates.
|
|
303
|
+
*/
|
|
304
|
+
declare class Converter {
|
|
305
|
+
private rates;
|
|
306
|
+
private base;
|
|
307
|
+
/**
|
|
308
|
+
* Creates a new Converter.
|
|
309
|
+
*
|
|
310
|
+
* @param base - The base currency code (e.g., "USD").
|
|
311
|
+
* @param rates - A map of currency codes to exchange rates relative to the base.
|
|
312
|
+
* Example: { "EUR": 0.85, "GBP": 0.75 } (where 1 Base = 0.85 EUR).
|
|
313
|
+
*/
|
|
314
|
+
constructor(base: string, rates: Record<string, number>);
|
|
315
|
+
/**
|
|
316
|
+
* Converts a Money object to a target currency.
|
|
317
|
+
*
|
|
318
|
+
* @param money - The Money object to convert.
|
|
319
|
+
* @param toCurrency - The target currency (code or object).
|
|
320
|
+
* @returns A new Money object in the target currency.
|
|
321
|
+
* @throws {Error} If exchange rates are missing.
|
|
322
|
+
*/
|
|
323
|
+
convert(money: Money, toCurrency: Currency | string): Money;
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
/**
|
|
327
|
+
* A collection of Money objects in different currencies.
|
|
328
|
+
* Useful for representing a wallet or portfolio.
|
|
329
|
+
*/
|
|
330
|
+
declare class MoneyBag {
|
|
331
|
+
private contents;
|
|
332
|
+
/**
|
|
333
|
+
* Adds a Money amount to the bag.
|
|
334
|
+
* @param money The money to add.
|
|
335
|
+
*/
|
|
336
|
+
add(money: Money): void;
|
|
337
|
+
/**
|
|
338
|
+
* Subtracts a Money amount from the bag.
|
|
339
|
+
* @param money The money to subtract.
|
|
340
|
+
*/
|
|
341
|
+
subtract(money: Money): void;
|
|
342
|
+
/**
|
|
343
|
+
* Gets the amount for a specific currency.
|
|
344
|
+
* @param currency The currency to retrieve.
|
|
345
|
+
* @returns The Money amount in that currency (or zero if not present).
|
|
346
|
+
*/
|
|
347
|
+
get(currency: Currency | string): Money;
|
|
348
|
+
/**
|
|
349
|
+
* Calculates the total value of the bag in a specific currency.
|
|
350
|
+
*
|
|
351
|
+
* @param targetCurrency The currency to convert everything to.
|
|
352
|
+
* @param converter The converter instance with exchange rates.
|
|
353
|
+
* @returns The total amount in the target currency.
|
|
354
|
+
*/
|
|
355
|
+
total(targetCurrency: Currency | string, converter: Converter): Money;
|
|
356
|
+
/**
|
|
357
|
+
* Returns a list of all Money objects in the bag.
|
|
358
|
+
*/
|
|
359
|
+
getAll(): Money[];
|
|
360
|
+
/**
|
|
361
|
+
* Returns a JSON representation of the bag.
|
|
362
|
+
*/
|
|
363
|
+
toJSON(): {
|
|
364
|
+
amount: string;
|
|
365
|
+
currency: string;
|
|
366
|
+
precision: number;
|
|
367
|
+
}[];
|
|
187
368
|
}
|
|
188
369
|
|
|
189
370
|
/**
|
|
@@ -208,18 +389,28 @@ declare const JPY: Currency;
|
|
|
208
389
|
declare const ZAR: Currency;
|
|
209
390
|
/**
|
|
210
391
|
* A collection of common currencies.
|
|
392
|
+
* @deprecated Use registry instead.
|
|
211
393
|
*/
|
|
212
394
|
declare const CURRENCIES: Record<string, Currency>;
|
|
395
|
+
|
|
396
|
+
declare function getMinorUnitExponent(currency: Currency): bigint;
|
|
397
|
+
|
|
213
398
|
/**
|
|
214
|
-
*
|
|
215
|
-
*
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
399
|
+
* Registers a currency in the global registry.
|
|
400
|
+
* @param currency The currency to register.
|
|
401
|
+
*/
|
|
402
|
+
declare function registerCurrency(currency: Currency): void;
|
|
403
|
+
/**
|
|
404
|
+
* Retrieves a currency by its code.
|
|
405
|
+
* @param code The currency code (e.g., "USD").
|
|
406
|
+
* @returns The Currency object.
|
|
407
|
+
* @throws Error if the currency is not found.
|
|
219
408
|
*/
|
|
220
409
|
declare function getCurrency(code: string): Currency;
|
|
221
|
-
|
|
222
|
-
|
|
410
|
+
/**
|
|
411
|
+
* Checks if a currency is registered.
|
|
412
|
+
*/
|
|
413
|
+
declare function isCurrencyRegistered(code: string): boolean;
|
|
223
414
|
|
|
224
415
|
declare function divideWithRounding(numerator: bigint, denominator: bigint, mode: RoundingMode): bigint;
|
|
225
416
|
|
|
@@ -228,7 +419,7 @@ declare class MonetraError extends Error {
|
|
|
228
419
|
}
|
|
229
420
|
|
|
230
421
|
declare class CurrencyMismatchError extends MonetraError {
|
|
231
|
-
constructor(expected: string,
|
|
422
|
+
constructor(expected: string, received: string);
|
|
232
423
|
}
|
|
233
424
|
|
|
234
425
|
declare class InvalidPrecisionError extends MonetraError {
|
|
@@ -236,7 +427,7 @@ declare class InvalidPrecisionError extends MonetraError {
|
|
|
236
427
|
}
|
|
237
428
|
|
|
238
429
|
declare class RoundingRequiredError extends MonetraError {
|
|
239
|
-
constructor();
|
|
430
|
+
constructor(operation?: string, result?: number);
|
|
240
431
|
}
|
|
241
432
|
|
|
242
433
|
declare class InsufficientFundsError extends MonetraError {
|
|
@@ -247,4 +438,15 @@ declare class OverflowError extends MonetraError {
|
|
|
247
438
|
constructor(message?: string);
|
|
248
439
|
}
|
|
249
440
|
|
|
250
|
-
|
|
441
|
+
/**
|
|
442
|
+
* Helper function to create Money instances.
|
|
443
|
+
*
|
|
444
|
+
* If amount is a number or BigInt, it is treated as minor units.
|
|
445
|
+
* If amount is a string, it is treated as major units.
|
|
446
|
+
*
|
|
447
|
+
* @param amount - The amount (minor units if number/bigint, major units if string).
|
|
448
|
+
* @param currency - The currency code or object.
|
|
449
|
+
*/
|
|
450
|
+
declare function money(amount: number | bigint | string, currency: string | Currency): Money;
|
|
451
|
+
|
|
452
|
+
export { CURRENCIES, Converter, type Currency, CurrencyMismatchError, EUR, GBP, InsufficientFundsError, InvalidPrecisionError, JPY, MonetraError, Money, MoneyBag, OverflowError, RoundingMode, RoundingRequiredError, USD, ZAR, assertNonNegative, assertSameCurrency, divideWithRounding, getCurrency, getMinorUnitExponent, isCurrencyRegistered, money, registerCurrency };
|