monetra 0.0.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,250 @@
1
+ /**
2
+ * Represents a currency with its metadata.
3
+ *
4
+ * This interface defines the structure for currency objects used throughout the library.
5
+ * It includes the ISO 4217 code, the number of decimal places (precision),
6
+ * the currency symbol, and an optional default locale for formatting.
7
+ */
8
+ interface Currency {
9
+ /**
10
+ * The ISO 4217 currency code (e.g., "USD", "EUR", "ZAR").
11
+ */
12
+ code: string;
13
+ /**
14
+ * The number of decimal places for the currency.
15
+ * This defines the precision of the currency (e.g., 2 for USD, 0 for JPY).
16
+ */
17
+ decimals: number;
18
+ /**
19
+ * The symbol associated with the currency (e.g., "$", "€", "R").
20
+ */
21
+ symbol: string;
22
+ /**
23
+ * The default locale to use when formatting amounts in this currency (e.g., "en-US").
24
+ * If not provided, the formatter may fall back to a default or user-provided locale.
25
+ */
26
+ locale?: string;
27
+ }
28
+
29
+ /**
30
+ * Enumeration of rounding modes for handling fractional minor units.
31
+ */
32
+ declare enum RoundingMode {
33
+ /**
34
+ * Rounds towards the nearest neighbor. If equidistant, rounds away from zero.
35
+ * Example: 2.5 -> 3, -2.5 -> -3
36
+ */
37
+ HALF_UP = "HALF_UP",
38
+ /**
39
+ * Rounds towards the nearest neighbor. If equidistant, rounds towards zero.
40
+ * Example: 2.5 -> 2, -2.5 -> -2
41
+ */
42
+ HALF_DOWN = "HALF_DOWN",
43
+ /**
44
+ * Rounds towards the nearest neighbor. If equidistant, rounds towards the nearest even integer.
45
+ * Also known as Banker's Rounding.
46
+ * Example: 2.5 -> 2, 3.5 -> 4
47
+ */
48
+ HALF_EVEN = "HALF_EVEN",
49
+ /**
50
+ * Rounds towards negative infinity.
51
+ * Example: 2.9 -> 2, -2.1 -> -3
52
+ */
53
+ FLOOR = "FLOOR",
54
+ /**
55
+ * Rounds towards positive infinity.
56
+ * Example: 2.1 -> 3, -2.9 -> -2
57
+ */
58
+ CEIL = "CEIL"
59
+ }
60
+
61
+ /**
62
+ * Represents a monetary value in a specific currency.
63
+ *
64
+ * Money objects are immutable and store values in minor units (e.g., cents) using BigInt
65
+ * to avoid floating-point precision errors.
66
+ */
67
+ declare class Money {
68
+ /**
69
+ * The amount in minor units (e.g., cents).
70
+ */
71
+ readonly minor: bigint;
72
+ /**
73
+ * The currency of this money value.
74
+ */
75
+ readonly currency: Currency;
76
+ private constructor();
77
+ /**
78
+ * Creates a Money instance from minor units (e.g., cents).
79
+ *
80
+ * @param minor - The amount in minor units. Can be a number or BigInt.
81
+ * @param currency - The currency of the money.
82
+ * @returns A new Money instance.
83
+ * @example
84
+ * const m = Money.fromMinor(100, USD); // $1.00
85
+ */
86
+ static fromMinor(minor: bigint | number, currency: Currency): Money;
87
+ /**
88
+ * Creates a Money instance from major units (e.g., "10.50").
89
+ *
90
+ * @param amount - The amount as a string. Must be a valid decimal number.
91
+ * @param currency - The currency of the money.
92
+ * @returns A new Money instance.
93
+ * @throws {InvalidPrecisionError} If the amount has more decimal places than the currency allows.
94
+ * @example
95
+ * const m = Money.fromMajor("10.50", USD); // $10.50
96
+ */
97
+ static fromMajor(amount: string, currency: Currency): Money;
98
+ /**
99
+ * Creates a Money instance representing zero in the given currency.
100
+ *
101
+ * @param currency - The currency.
102
+ * @returns A new Money instance with value 0.
103
+ */
104
+ static zero(currency: Currency): Money;
105
+ /**
106
+ * Adds another Money value to this one.
107
+ *
108
+ * @param other - The Money value to add.
109
+ * @returns A new Money instance representing the sum.
110
+ * @throws {CurrencyMismatchError} If the currencies do not match.
111
+ */
112
+ add(other: Money): Money;
113
+ /**
114
+ * Subtracts another Money value from this one.
115
+ *
116
+ * @param other - The Money value to subtract.
117
+ * @returns A new Money instance representing the difference.
118
+ * @throws {CurrencyMismatchError} If the currencies do not match.
119
+ */
120
+ subtract(other: Money): Money;
121
+ /**
122
+ * Multiplies this Money value by a scalar.
123
+ *
124
+ * @param multiplier - The number to multiply by.
125
+ * @param options - Options for rounding if the result is not an integer.
126
+ * @returns A new Money instance representing the product.
127
+ * @throws {RoundingRequiredError} If the result is fractional and no rounding mode is provided.
128
+ */
129
+ multiply(multiplier: string | number, options?: {
130
+ rounding?: RoundingMode;
131
+ }): Money;
132
+ /**
133
+ * Allocates (splits) this Money value according to a list of ratios.
134
+ *
135
+ * The sum of the parts will always equal the original amount.
136
+ * Remainders are distributed to the parts with the largest fractional remainders.
137
+ *
138
+ * @param ratios - A list of numbers representing the ratios to split by.
139
+ * @returns An array of Money instances.
140
+ */
141
+ allocate(ratios: number[]): Money[];
142
+ /**
143
+ * Formats this Money value as a string.
144
+ *
145
+ * @param options - Formatting options.
146
+ * @returns The formatted string.
147
+ */
148
+ format(options?: {
149
+ locale?: string;
150
+ symbol?: boolean;
151
+ }): string;
152
+ /**
153
+ * Checks if this Money value is equal to another.
154
+ *
155
+ * @param other - The other Money value.
156
+ * @returns True if amounts and currencies are equal.
157
+ */
158
+ equals(other: Money): boolean;
159
+ /**
160
+ * Checks if this Money value is greater than another.
161
+ *
162
+ * @param other - The other Money value.
163
+ * @returns True if this value is greater.
164
+ * @throws {CurrencyMismatchError} If the currencies do not match.
165
+ */
166
+ greaterThan(other: Money): boolean;
167
+ /**
168
+ * Checks if this Money value is less than another.
169
+ *
170
+ * @param other - The other Money value.
171
+ * @returns True if this value is less.
172
+ * @throws {CurrencyMismatchError} If the currencies do not match.
173
+ */
174
+ lessThan(other: Money): boolean;
175
+ /**
176
+ * Checks if this Money value is zero.
177
+ *
178
+ * @returns True if the amount is zero.
179
+ */
180
+ isZero(): boolean;
181
+ /**
182
+ * Checks if this Money value is negative.
183
+ *
184
+ * @returns True if the amount is negative.
185
+ */
186
+ isNegative(): boolean;
187
+ }
188
+
189
+ /**
190
+ * United States Dollar (USD).
191
+ */
192
+ declare const USD: Currency;
193
+ /**
194
+ * Euro (EUR).
195
+ */
196
+ declare const EUR: Currency;
197
+ /**
198
+ * British Pound Sterling (GBP).
199
+ */
200
+ declare const GBP: Currency;
201
+ /**
202
+ * Japanese Yen (JPY).
203
+ */
204
+ declare const JPY: Currency;
205
+ /**
206
+ * South African Rand (ZAR).
207
+ */
208
+ declare const ZAR: Currency;
209
+ /**
210
+ * A collection of common currencies.
211
+ */
212
+ declare const CURRENCIES: Record<string, Currency>;
213
+ /**
214
+ * Retrieves a Currency object by its ISO 4217 code.
215
+ *
216
+ * @param code - The currency code (case-insensitive).
217
+ * @returns The Currency object corresponding to the code.
218
+ * @throws {Error} If the currency code is not found in the registry.
219
+ */
220
+ declare function getCurrency(code: string): Currency;
221
+
222
+ declare function getMinorUnitExponent(currency: Currency): bigint;
223
+
224
+ declare function divideWithRounding(numerator: bigint, denominator: bigint, mode: RoundingMode): bigint;
225
+
226
+ declare class MonetraError extends Error {
227
+ constructor(message: string);
228
+ }
229
+
230
+ declare class CurrencyMismatchError extends MonetraError {
231
+ constructor(expected: string, actual: string);
232
+ }
233
+
234
+ declare class InvalidPrecisionError extends MonetraError {
235
+ constructor(message: string);
236
+ }
237
+
238
+ declare class RoundingRequiredError extends MonetraError {
239
+ constructor();
240
+ }
241
+
242
+ declare class InsufficientFundsError extends MonetraError {
243
+ constructor();
244
+ }
245
+
246
+ declare class OverflowError extends MonetraError {
247
+ constructor(message?: string);
248
+ }
249
+
250
+ export { CURRENCIES, type Currency, CurrencyMismatchError, EUR, GBP, InsufficientFundsError, InvalidPrecisionError, JPY, MonetraError, Money, OverflowError, RoundingMode, RoundingRequiredError, USD, ZAR, divideWithRounding, getCurrency, getMinorUnitExponent };
@@ -0,0 +1,250 @@
1
+ /**
2
+ * Represents a currency with its metadata.
3
+ *
4
+ * This interface defines the structure for currency objects used throughout the library.
5
+ * It includes the ISO 4217 code, the number of decimal places (precision),
6
+ * the currency symbol, and an optional default locale for formatting.
7
+ */
8
+ interface Currency {
9
+ /**
10
+ * The ISO 4217 currency code (e.g., "USD", "EUR", "ZAR").
11
+ */
12
+ code: string;
13
+ /**
14
+ * The number of decimal places for the currency.
15
+ * This defines the precision of the currency (e.g., 2 for USD, 0 for JPY).
16
+ */
17
+ decimals: number;
18
+ /**
19
+ * The symbol associated with the currency (e.g., "$", "€", "R").
20
+ */
21
+ symbol: string;
22
+ /**
23
+ * The default locale to use when formatting amounts in this currency (e.g., "en-US").
24
+ * If not provided, the formatter may fall back to a default or user-provided locale.
25
+ */
26
+ locale?: string;
27
+ }
28
+
29
+ /**
30
+ * Enumeration of rounding modes for handling fractional minor units.
31
+ */
32
+ declare enum RoundingMode {
33
+ /**
34
+ * Rounds towards the nearest neighbor. If equidistant, rounds away from zero.
35
+ * Example: 2.5 -> 3, -2.5 -> -3
36
+ */
37
+ HALF_UP = "HALF_UP",
38
+ /**
39
+ * Rounds towards the nearest neighbor. If equidistant, rounds towards zero.
40
+ * Example: 2.5 -> 2, -2.5 -> -2
41
+ */
42
+ HALF_DOWN = "HALF_DOWN",
43
+ /**
44
+ * Rounds towards the nearest neighbor. If equidistant, rounds towards the nearest even integer.
45
+ * Also known as Banker's Rounding.
46
+ * Example: 2.5 -> 2, 3.5 -> 4
47
+ */
48
+ HALF_EVEN = "HALF_EVEN",
49
+ /**
50
+ * Rounds towards negative infinity.
51
+ * Example: 2.9 -> 2, -2.1 -> -3
52
+ */
53
+ FLOOR = "FLOOR",
54
+ /**
55
+ * Rounds towards positive infinity.
56
+ * Example: 2.1 -> 3, -2.9 -> -2
57
+ */
58
+ CEIL = "CEIL"
59
+ }
60
+
61
+ /**
62
+ * Represents a monetary value in a specific currency.
63
+ *
64
+ * Money objects are immutable and store values in minor units (e.g., cents) using BigInt
65
+ * to avoid floating-point precision errors.
66
+ */
67
+ declare class Money {
68
+ /**
69
+ * The amount in minor units (e.g., cents).
70
+ */
71
+ readonly minor: bigint;
72
+ /**
73
+ * The currency of this money value.
74
+ */
75
+ readonly currency: Currency;
76
+ private constructor();
77
+ /**
78
+ * Creates a Money instance from minor units (e.g., cents).
79
+ *
80
+ * @param minor - The amount in minor units. Can be a number or BigInt.
81
+ * @param currency - The currency of the money.
82
+ * @returns A new Money instance.
83
+ * @example
84
+ * const m = Money.fromMinor(100, USD); // $1.00
85
+ */
86
+ static fromMinor(minor: bigint | number, currency: Currency): Money;
87
+ /**
88
+ * Creates a Money instance from major units (e.g., "10.50").
89
+ *
90
+ * @param amount - The amount as a string. Must be a valid decimal number.
91
+ * @param currency - The currency of the money.
92
+ * @returns A new Money instance.
93
+ * @throws {InvalidPrecisionError} If the amount has more decimal places than the currency allows.
94
+ * @example
95
+ * const m = Money.fromMajor("10.50", USD); // $10.50
96
+ */
97
+ static fromMajor(amount: string, currency: Currency): Money;
98
+ /**
99
+ * Creates a Money instance representing zero in the given currency.
100
+ *
101
+ * @param currency - The currency.
102
+ * @returns A new Money instance with value 0.
103
+ */
104
+ static zero(currency: Currency): Money;
105
+ /**
106
+ * Adds another Money value to this one.
107
+ *
108
+ * @param other - The Money value to add.
109
+ * @returns A new Money instance representing the sum.
110
+ * @throws {CurrencyMismatchError} If the currencies do not match.
111
+ */
112
+ add(other: Money): Money;
113
+ /**
114
+ * Subtracts another Money value from this one.
115
+ *
116
+ * @param other - The Money value to subtract.
117
+ * @returns A new Money instance representing the difference.
118
+ * @throws {CurrencyMismatchError} If the currencies do not match.
119
+ */
120
+ subtract(other: Money): Money;
121
+ /**
122
+ * Multiplies this Money value by a scalar.
123
+ *
124
+ * @param multiplier - The number to multiply by.
125
+ * @param options - Options for rounding if the result is not an integer.
126
+ * @returns A new Money instance representing the product.
127
+ * @throws {RoundingRequiredError} If the result is fractional and no rounding mode is provided.
128
+ */
129
+ multiply(multiplier: string | number, options?: {
130
+ rounding?: RoundingMode;
131
+ }): Money;
132
+ /**
133
+ * Allocates (splits) this Money value according to a list of ratios.
134
+ *
135
+ * The sum of the parts will always equal the original amount.
136
+ * Remainders are distributed to the parts with the largest fractional remainders.
137
+ *
138
+ * @param ratios - A list of numbers representing the ratios to split by.
139
+ * @returns An array of Money instances.
140
+ */
141
+ allocate(ratios: number[]): Money[];
142
+ /**
143
+ * Formats this Money value as a string.
144
+ *
145
+ * @param options - Formatting options.
146
+ * @returns The formatted string.
147
+ */
148
+ format(options?: {
149
+ locale?: string;
150
+ symbol?: boolean;
151
+ }): string;
152
+ /**
153
+ * Checks if this Money value is equal to another.
154
+ *
155
+ * @param other - The other Money value.
156
+ * @returns True if amounts and currencies are equal.
157
+ */
158
+ equals(other: Money): boolean;
159
+ /**
160
+ * Checks if this Money value is greater than another.
161
+ *
162
+ * @param other - The other Money value.
163
+ * @returns True if this value is greater.
164
+ * @throws {CurrencyMismatchError} If the currencies do not match.
165
+ */
166
+ greaterThan(other: Money): boolean;
167
+ /**
168
+ * Checks if this Money value is less than another.
169
+ *
170
+ * @param other - The other Money value.
171
+ * @returns True if this value is less.
172
+ * @throws {CurrencyMismatchError} If the currencies do not match.
173
+ */
174
+ lessThan(other: Money): boolean;
175
+ /**
176
+ * Checks if this Money value is zero.
177
+ *
178
+ * @returns True if the amount is zero.
179
+ */
180
+ isZero(): boolean;
181
+ /**
182
+ * Checks if this Money value is negative.
183
+ *
184
+ * @returns True if the amount is negative.
185
+ */
186
+ isNegative(): boolean;
187
+ }
188
+
189
+ /**
190
+ * United States Dollar (USD).
191
+ */
192
+ declare const USD: Currency;
193
+ /**
194
+ * Euro (EUR).
195
+ */
196
+ declare const EUR: Currency;
197
+ /**
198
+ * British Pound Sterling (GBP).
199
+ */
200
+ declare const GBP: Currency;
201
+ /**
202
+ * Japanese Yen (JPY).
203
+ */
204
+ declare const JPY: Currency;
205
+ /**
206
+ * South African Rand (ZAR).
207
+ */
208
+ declare const ZAR: Currency;
209
+ /**
210
+ * A collection of common currencies.
211
+ */
212
+ declare const CURRENCIES: Record<string, Currency>;
213
+ /**
214
+ * Retrieves a Currency object by its ISO 4217 code.
215
+ *
216
+ * @param code - The currency code (case-insensitive).
217
+ * @returns The Currency object corresponding to the code.
218
+ * @throws {Error} If the currency code is not found in the registry.
219
+ */
220
+ declare function getCurrency(code: string): Currency;
221
+
222
+ declare function getMinorUnitExponent(currency: Currency): bigint;
223
+
224
+ declare function divideWithRounding(numerator: bigint, denominator: bigint, mode: RoundingMode): bigint;
225
+
226
+ declare class MonetraError extends Error {
227
+ constructor(message: string);
228
+ }
229
+
230
+ declare class CurrencyMismatchError extends MonetraError {
231
+ constructor(expected: string, actual: string);
232
+ }
233
+
234
+ declare class InvalidPrecisionError extends MonetraError {
235
+ constructor(message: string);
236
+ }
237
+
238
+ declare class RoundingRequiredError extends MonetraError {
239
+ constructor();
240
+ }
241
+
242
+ declare class InsufficientFundsError extends MonetraError {
243
+ constructor();
244
+ }
245
+
246
+ declare class OverflowError extends MonetraError {
247
+ constructor(message?: string);
248
+ }
249
+
250
+ export { CURRENCIES, type Currency, CurrencyMismatchError, EUR, GBP, InsufficientFundsError, InvalidPrecisionError, JPY, MonetraError, Money, OverflowError, RoundingMode, RoundingRequiredError, USD, ZAR, divideWithRounding, getCurrency, getMinorUnitExponent };