@puvaanraaj/moneta 0.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/LICENSE +21 -0
- package/README.md +160 -0
- package/dist/index.cjs +314 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +283 -0
- package/dist/index.d.ts +283 -0
- package/dist/index.js +305 -0
- package/dist/index.js.map +1 -0
- package/package.json +57 -0
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ISO 4217 currency definitions.
|
|
3
|
+
* Each entry contains the decimal precision used for minor-unit storage.
|
|
4
|
+
*/
|
|
5
|
+
declare const CURRENCIES: {
|
|
6
|
+
readonly AED: {
|
|
7
|
+
readonly code: "AED";
|
|
8
|
+
readonly decimals: 2;
|
|
9
|
+
readonly name: "UAE Dirham";
|
|
10
|
+
};
|
|
11
|
+
readonly AUD: {
|
|
12
|
+
readonly code: "AUD";
|
|
13
|
+
readonly decimals: 2;
|
|
14
|
+
readonly name: "Australian Dollar";
|
|
15
|
+
};
|
|
16
|
+
readonly BRL: {
|
|
17
|
+
readonly code: "BRL";
|
|
18
|
+
readonly decimals: 2;
|
|
19
|
+
readonly name: "Brazilian Real";
|
|
20
|
+
};
|
|
21
|
+
readonly CAD: {
|
|
22
|
+
readonly code: "CAD";
|
|
23
|
+
readonly decimals: 2;
|
|
24
|
+
readonly name: "Canadian Dollar";
|
|
25
|
+
};
|
|
26
|
+
readonly CHF: {
|
|
27
|
+
readonly code: "CHF";
|
|
28
|
+
readonly decimals: 2;
|
|
29
|
+
readonly name: "Swiss Franc";
|
|
30
|
+
};
|
|
31
|
+
readonly CNY: {
|
|
32
|
+
readonly code: "CNY";
|
|
33
|
+
readonly decimals: 2;
|
|
34
|
+
readonly name: "Chinese Yuan";
|
|
35
|
+
};
|
|
36
|
+
readonly CZK: {
|
|
37
|
+
readonly code: "CZK";
|
|
38
|
+
readonly decimals: 2;
|
|
39
|
+
readonly name: "Czech Koruna";
|
|
40
|
+
};
|
|
41
|
+
readonly DKK: {
|
|
42
|
+
readonly code: "DKK";
|
|
43
|
+
readonly decimals: 2;
|
|
44
|
+
readonly name: "Danish Krone";
|
|
45
|
+
};
|
|
46
|
+
readonly EUR: {
|
|
47
|
+
readonly code: "EUR";
|
|
48
|
+
readonly decimals: 2;
|
|
49
|
+
readonly name: "Euro";
|
|
50
|
+
};
|
|
51
|
+
readonly GBP: {
|
|
52
|
+
readonly code: "GBP";
|
|
53
|
+
readonly decimals: 2;
|
|
54
|
+
readonly name: "British Pound";
|
|
55
|
+
};
|
|
56
|
+
readonly HKD: {
|
|
57
|
+
readonly code: "HKD";
|
|
58
|
+
readonly decimals: 2;
|
|
59
|
+
readonly name: "Hong Kong Dollar";
|
|
60
|
+
};
|
|
61
|
+
readonly HUF: {
|
|
62
|
+
readonly code: "HUF";
|
|
63
|
+
readonly decimals: 2;
|
|
64
|
+
readonly name: "Hungarian Forint";
|
|
65
|
+
};
|
|
66
|
+
readonly IDR: {
|
|
67
|
+
readonly code: "IDR";
|
|
68
|
+
readonly decimals: 2;
|
|
69
|
+
readonly name: "Indonesian Rupiah";
|
|
70
|
+
};
|
|
71
|
+
readonly ILS: {
|
|
72
|
+
readonly code: "ILS";
|
|
73
|
+
readonly decimals: 2;
|
|
74
|
+
readonly name: "Israeli New Shekel";
|
|
75
|
+
};
|
|
76
|
+
readonly INR: {
|
|
77
|
+
readonly code: "INR";
|
|
78
|
+
readonly decimals: 2;
|
|
79
|
+
readonly name: "Indian Rupee";
|
|
80
|
+
};
|
|
81
|
+
readonly JPY: {
|
|
82
|
+
readonly code: "JPY";
|
|
83
|
+
readonly decimals: 0;
|
|
84
|
+
readonly name: "Japanese Yen";
|
|
85
|
+
};
|
|
86
|
+
readonly KRW: {
|
|
87
|
+
readonly code: "KRW";
|
|
88
|
+
readonly decimals: 0;
|
|
89
|
+
readonly name: "South Korean Won";
|
|
90
|
+
};
|
|
91
|
+
readonly KWD: {
|
|
92
|
+
readonly code: "KWD";
|
|
93
|
+
readonly decimals: 3;
|
|
94
|
+
readonly name: "Kuwaiti Dinar";
|
|
95
|
+
};
|
|
96
|
+
readonly MXN: {
|
|
97
|
+
readonly code: "MXN";
|
|
98
|
+
readonly decimals: 2;
|
|
99
|
+
readonly name: "Mexican Peso";
|
|
100
|
+
};
|
|
101
|
+
readonly MYR: {
|
|
102
|
+
readonly code: "MYR";
|
|
103
|
+
readonly decimals: 2;
|
|
104
|
+
readonly name: "Malaysian Ringgit";
|
|
105
|
+
};
|
|
106
|
+
readonly NOK: {
|
|
107
|
+
readonly code: "NOK";
|
|
108
|
+
readonly decimals: 2;
|
|
109
|
+
readonly name: "Norwegian Krone";
|
|
110
|
+
};
|
|
111
|
+
readonly NZD: {
|
|
112
|
+
readonly code: "NZD";
|
|
113
|
+
readonly decimals: 2;
|
|
114
|
+
readonly name: "New Zealand Dollar";
|
|
115
|
+
};
|
|
116
|
+
readonly PHP: {
|
|
117
|
+
readonly code: "PHP";
|
|
118
|
+
readonly decimals: 2;
|
|
119
|
+
readonly name: "Philippine Peso";
|
|
120
|
+
};
|
|
121
|
+
readonly PLN: {
|
|
122
|
+
readonly code: "PLN";
|
|
123
|
+
readonly decimals: 2;
|
|
124
|
+
readonly name: "Polish Zloty";
|
|
125
|
+
};
|
|
126
|
+
readonly RON: {
|
|
127
|
+
readonly code: "RON";
|
|
128
|
+
readonly decimals: 2;
|
|
129
|
+
readonly name: "Romanian Leu";
|
|
130
|
+
};
|
|
131
|
+
readonly SAR: {
|
|
132
|
+
readonly code: "SAR";
|
|
133
|
+
readonly decimals: 2;
|
|
134
|
+
readonly name: "Saudi Riyal";
|
|
135
|
+
};
|
|
136
|
+
readonly SEK: {
|
|
137
|
+
readonly code: "SEK";
|
|
138
|
+
readonly decimals: 2;
|
|
139
|
+
readonly name: "Swedish Krona";
|
|
140
|
+
};
|
|
141
|
+
readonly SGD: {
|
|
142
|
+
readonly code: "SGD";
|
|
143
|
+
readonly decimals: 2;
|
|
144
|
+
readonly name: "Singapore Dollar";
|
|
145
|
+
};
|
|
146
|
+
readonly THB: {
|
|
147
|
+
readonly code: "THB";
|
|
148
|
+
readonly decimals: 2;
|
|
149
|
+
readonly name: "Thai Baht";
|
|
150
|
+
};
|
|
151
|
+
readonly TRY: {
|
|
152
|
+
readonly code: "TRY";
|
|
153
|
+
readonly decimals: 2;
|
|
154
|
+
readonly name: "Turkish Lira";
|
|
155
|
+
};
|
|
156
|
+
readonly TWD: {
|
|
157
|
+
readonly code: "TWD";
|
|
158
|
+
readonly decimals: 2;
|
|
159
|
+
readonly name: "New Taiwan Dollar";
|
|
160
|
+
};
|
|
161
|
+
readonly USD: {
|
|
162
|
+
readonly code: "USD";
|
|
163
|
+
readonly decimals: 2;
|
|
164
|
+
readonly name: "US Dollar";
|
|
165
|
+
};
|
|
166
|
+
readonly VND: {
|
|
167
|
+
readonly code: "VND";
|
|
168
|
+
readonly decimals: 0;
|
|
169
|
+
readonly name: "Vietnamese Dong";
|
|
170
|
+
};
|
|
171
|
+
readonly ZAR: {
|
|
172
|
+
readonly code: "ZAR";
|
|
173
|
+
readonly decimals: 2;
|
|
174
|
+
readonly name: "South African Rand";
|
|
175
|
+
};
|
|
176
|
+
};
|
|
177
|
+
type CurrencyCode = keyof typeof CURRENCIES;
|
|
178
|
+
type CurrencyDef = (typeof CURRENCIES)[CurrencyCode];
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Immutable, BigInt-precision monetary value.
|
|
182
|
+
*
|
|
183
|
+
* The generic parameter `C` brands the currency at the type level so
|
|
184
|
+
* cross-currency arithmetic is a **compile-time error**.
|
|
185
|
+
*
|
|
186
|
+
* @example
|
|
187
|
+
* const price = money('19.99', 'USD')
|
|
188
|
+
* const tax = money('1.60', 'USD')
|
|
189
|
+
* price.add(tax).format() // "$21.59"
|
|
190
|
+
* price.add(money('5', 'EUR')) // TypeError: currency mismatch
|
|
191
|
+
*/
|
|
192
|
+
declare class Money<C extends CurrencyCode = CurrencyCode> {
|
|
193
|
+
#private;
|
|
194
|
+
constructor(amount: string | number | bigint, currency: C);
|
|
195
|
+
/** Create from raw minor units (e.g. cents). Useful when reading from a database. */
|
|
196
|
+
static fromMinorUnits<C extends CurrencyCode>(units: bigint, currency: C): Money<C>;
|
|
197
|
+
/** Raw amount in minor units (e.g. cents). Safe for storage and wire transfer. */
|
|
198
|
+
get minorUnits(): bigint;
|
|
199
|
+
get currency(): C;
|
|
200
|
+
/** Human-readable decimal string, e.g. "19.99". */
|
|
201
|
+
get decimalValue(): string;
|
|
202
|
+
add(other: Money<C>): Money<C>;
|
|
203
|
+
subtract(other: Money<C>): Money<C>;
|
|
204
|
+
/**
|
|
205
|
+
* Multiply by a scalar. Uses half-up rounding to the currency's minor unit.
|
|
206
|
+
* @example money('10.00', 'USD').multiply(1.1) // $11.00
|
|
207
|
+
* @example money('10.00', 'USD').multiply('1.5') // $15.00
|
|
208
|
+
*/
|
|
209
|
+
multiply(factor: number | string): Money<C>;
|
|
210
|
+
/**
|
|
211
|
+
* Divide by a scalar. Uses half-up rounding to the currency's minor unit.
|
|
212
|
+
* @example money('10.00', 'USD').divide(3) // $3.33
|
|
213
|
+
*/
|
|
214
|
+
divide(divisor: number | string): Money<C>;
|
|
215
|
+
/**
|
|
216
|
+
* Allocate proportionally. The sum of all parts always equals the original —
|
|
217
|
+
* any rounding remainder is distributed to the first buckets.
|
|
218
|
+
*
|
|
219
|
+
* @example
|
|
220
|
+
* money('10.00', 'USD').allocate([1, 1, 1])
|
|
221
|
+
* // [Money('3.34'), Money('3.33'), Money('3.33')]
|
|
222
|
+
*/
|
|
223
|
+
allocate(ratios: readonly number[]): Money<C>[];
|
|
224
|
+
equals(other: Money<C>): boolean;
|
|
225
|
+
greaterThan(other: Money<C>): boolean;
|
|
226
|
+
lessThan(other: Money<C>): boolean;
|
|
227
|
+
greaterThanOrEqual(other: Money<C>): boolean;
|
|
228
|
+
lessThanOrEqual(other: Money<C>): boolean;
|
|
229
|
+
isZero(): boolean;
|
|
230
|
+
isPositive(): boolean;
|
|
231
|
+
isNegative(): boolean;
|
|
232
|
+
abs(): Money<C>;
|
|
233
|
+
negate(): Money<C>;
|
|
234
|
+
/**
|
|
235
|
+
* Format using the platform's `Intl.NumberFormat` — no custom format strings.
|
|
236
|
+
*
|
|
237
|
+
* @example
|
|
238
|
+
* money('19.99', 'USD').format() // "$19.99"
|
|
239
|
+
* money('19.99', 'EUR').format('de-DE') // "19,99 €"
|
|
240
|
+
*/
|
|
241
|
+
format(locale?: string): string;
|
|
242
|
+
/** Safe to store in JSON — no float representation. */
|
|
243
|
+
toJSON(): {
|
|
244
|
+
amount: string;
|
|
245
|
+
currency: C;
|
|
246
|
+
minorUnits: string;
|
|
247
|
+
};
|
|
248
|
+
toString(): string;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
declare class MonetaError extends Error {
|
|
252
|
+
constructor(message: string);
|
|
253
|
+
}
|
|
254
|
+
declare class CurrencyMismatchError extends MonetaError {
|
|
255
|
+
readonly expected: string;
|
|
256
|
+
readonly received: string;
|
|
257
|
+
constructor(expected: string, received: string);
|
|
258
|
+
}
|
|
259
|
+
declare class InvalidAmountError extends MonetaError {
|
|
260
|
+
readonly input: unknown;
|
|
261
|
+
constructor(input: unknown);
|
|
262
|
+
}
|
|
263
|
+
declare class DivisionByZeroError extends MonetaError {
|
|
264
|
+
constructor();
|
|
265
|
+
}
|
|
266
|
+
declare class UnknownCurrencyError extends MonetaError {
|
|
267
|
+
readonly currency: string;
|
|
268
|
+
constructor(currency: string);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* Create a Money value. This is the primary entry point.
|
|
273
|
+
*
|
|
274
|
+
* @example
|
|
275
|
+
* import { money } from 'moneta'
|
|
276
|
+
*
|
|
277
|
+
* const price = money('19.99', 'USD')
|
|
278
|
+
* const tax = money('1.60', 'USD')
|
|
279
|
+
* price.add(tax).format() // "$21.59"
|
|
280
|
+
*/
|
|
281
|
+
declare function money<C extends CurrencyCode>(amount: string | number | bigint, currency: C): Money<C>;
|
|
282
|
+
|
|
283
|
+
export { CURRENCIES, type CurrencyCode, type CurrencyDef, CurrencyMismatchError, DivisionByZeroError, InvalidAmountError, MonetaError, Money, UnknownCurrencyError, money };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ISO 4217 currency definitions.
|
|
3
|
+
* Each entry contains the decimal precision used for minor-unit storage.
|
|
4
|
+
*/
|
|
5
|
+
declare const CURRENCIES: {
|
|
6
|
+
readonly AED: {
|
|
7
|
+
readonly code: "AED";
|
|
8
|
+
readonly decimals: 2;
|
|
9
|
+
readonly name: "UAE Dirham";
|
|
10
|
+
};
|
|
11
|
+
readonly AUD: {
|
|
12
|
+
readonly code: "AUD";
|
|
13
|
+
readonly decimals: 2;
|
|
14
|
+
readonly name: "Australian Dollar";
|
|
15
|
+
};
|
|
16
|
+
readonly BRL: {
|
|
17
|
+
readonly code: "BRL";
|
|
18
|
+
readonly decimals: 2;
|
|
19
|
+
readonly name: "Brazilian Real";
|
|
20
|
+
};
|
|
21
|
+
readonly CAD: {
|
|
22
|
+
readonly code: "CAD";
|
|
23
|
+
readonly decimals: 2;
|
|
24
|
+
readonly name: "Canadian Dollar";
|
|
25
|
+
};
|
|
26
|
+
readonly CHF: {
|
|
27
|
+
readonly code: "CHF";
|
|
28
|
+
readonly decimals: 2;
|
|
29
|
+
readonly name: "Swiss Franc";
|
|
30
|
+
};
|
|
31
|
+
readonly CNY: {
|
|
32
|
+
readonly code: "CNY";
|
|
33
|
+
readonly decimals: 2;
|
|
34
|
+
readonly name: "Chinese Yuan";
|
|
35
|
+
};
|
|
36
|
+
readonly CZK: {
|
|
37
|
+
readonly code: "CZK";
|
|
38
|
+
readonly decimals: 2;
|
|
39
|
+
readonly name: "Czech Koruna";
|
|
40
|
+
};
|
|
41
|
+
readonly DKK: {
|
|
42
|
+
readonly code: "DKK";
|
|
43
|
+
readonly decimals: 2;
|
|
44
|
+
readonly name: "Danish Krone";
|
|
45
|
+
};
|
|
46
|
+
readonly EUR: {
|
|
47
|
+
readonly code: "EUR";
|
|
48
|
+
readonly decimals: 2;
|
|
49
|
+
readonly name: "Euro";
|
|
50
|
+
};
|
|
51
|
+
readonly GBP: {
|
|
52
|
+
readonly code: "GBP";
|
|
53
|
+
readonly decimals: 2;
|
|
54
|
+
readonly name: "British Pound";
|
|
55
|
+
};
|
|
56
|
+
readonly HKD: {
|
|
57
|
+
readonly code: "HKD";
|
|
58
|
+
readonly decimals: 2;
|
|
59
|
+
readonly name: "Hong Kong Dollar";
|
|
60
|
+
};
|
|
61
|
+
readonly HUF: {
|
|
62
|
+
readonly code: "HUF";
|
|
63
|
+
readonly decimals: 2;
|
|
64
|
+
readonly name: "Hungarian Forint";
|
|
65
|
+
};
|
|
66
|
+
readonly IDR: {
|
|
67
|
+
readonly code: "IDR";
|
|
68
|
+
readonly decimals: 2;
|
|
69
|
+
readonly name: "Indonesian Rupiah";
|
|
70
|
+
};
|
|
71
|
+
readonly ILS: {
|
|
72
|
+
readonly code: "ILS";
|
|
73
|
+
readonly decimals: 2;
|
|
74
|
+
readonly name: "Israeli New Shekel";
|
|
75
|
+
};
|
|
76
|
+
readonly INR: {
|
|
77
|
+
readonly code: "INR";
|
|
78
|
+
readonly decimals: 2;
|
|
79
|
+
readonly name: "Indian Rupee";
|
|
80
|
+
};
|
|
81
|
+
readonly JPY: {
|
|
82
|
+
readonly code: "JPY";
|
|
83
|
+
readonly decimals: 0;
|
|
84
|
+
readonly name: "Japanese Yen";
|
|
85
|
+
};
|
|
86
|
+
readonly KRW: {
|
|
87
|
+
readonly code: "KRW";
|
|
88
|
+
readonly decimals: 0;
|
|
89
|
+
readonly name: "South Korean Won";
|
|
90
|
+
};
|
|
91
|
+
readonly KWD: {
|
|
92
|
+
readonly code: "KWD";
|
|
93
|
+
readonly decimals: 3;
|
|
94
|
+
readonly name: "Kuwaiti Dinar";
|
|
95
|
+
};
|
|
96
|
+
readonly MXN: {
|
|
97
|
+
readonly code: "MXN";
|
|
98
|
+
readonly decimals: 2;
|
|
99
|
+
readonly name: "Mexican Peso";
|
|
100
|
+
};
|
|
101
|
+
readonly MYR: {
|
|
102
|
+
readonly code: "MYR";
|
|
103
|
+
readonly decimals: 2;
|
|
104
|
+
readonly name: "Malaysian Ringgit";
|
|
105
|
+
};
|
|
106
|
+
readonly NOK: {
|
|
107
|
+
readonly code: "NOK";
|
|
108
|
+
readonly decimals: 2;
|
|
109
|
+
readonly name: "Norwegian Krone";
|
|
110
|
+
};
|
|
111
|
+
readonly NZD: {
|
|
112
|
+
readonly code: "NZD";
|
|
113
|
+
readonly decimals: 2;
|
|
114
|
+
readonly name: "New Zealand Dollar";
|
|
115
|
+
};
|
|
116
|
+
readonly PHP: {
|
|
117
|
+
readonly code: "PHP";
|
|
118
|
+
readonly decimals: 2;
|
|
119
|
+
readonly name: "Philippine Peso";
|
|
120
|
+
};
|
|
121
|
+
readonly PLN: {
|
|
122
|
+
readonly code: "PLN";
|
|
123
|
+
readonly decimals: 2;
|
|
124
|
+
readonly name: "Polish Zloty";
|
|
125
|
+
};
|
|
126
|
+
readonly RON: {
|
|
127
|
+
readonly code: "RON";
|
|
128
|
+
readonly decimals: 2;
|
|
129
|
+
readonly name: "Romanian Leu";
|
|
130
|
+
};
|
|
131
|
+
readonly SAR: {
|
|
132
|
+
readonly code: "SAR";
|
|
133
|
+
readonly decimals: 2;
|
|
134
|
+
readonly name: "Saudi Riyal";
|
|
135
|
+
};
|
|
136
|
+
readonly SEK: {
|
|
137
|
+
readonly code: "SEK";
|
|
138
|
+
readonly decimals: 2;
|
|
139
|
+
readonly name: "Swedish Krona";
|
|
140
|
+
};
|
|
141
|
+
readonly SGD: {
|
|
142
|
+
readonly code: "SGD";
|
|
143
|
+
readonly decimals: 2;
|
|
144
|
+
readonly name: "Singapore Dollar";
|
|
145
|
+
};
|
|
146
|
+
readonly THB: {
|
|
147
|
+
readonly code: "THB";
|
|
148
|
+
readonly decimals: 2;
|
|
149
|
+
readonly name: "Thai Baht";
|
|
150
|
+
};
|
|
151
|
+
readonly TRY: {
|
|
152
|
+
readonly code: "TRY";
|
|
153
|
+
readonly decimals: 2;
|
|
154
|
+
readonly name: "Turkish Lira";
|
|
155
|
+
};
|
|
156
|
+
readonly TWD: {
|
|
157
|
+
readonly code: "TWD";
|
|
158
|
+
readonly decimals: 2;
|
|
159
|
+
readonly name: "New Taiwan Dollar";
|
|
160
|
+
};
|
|
161
|
+
readonly USD: {
|
|
162
|
+
readonly code: "USD";
|
|
163
|
+
readonly decimals: 2;
|
|
164
|
+
readonly name: "US Dollar";
|
|
165
|
+
};
|
|
166
|
+
readonly VND: {
|
|
167
|
+
readonly code: "VND";
|
|
168
|
+
readonly decimals: 0;
|
|
169
|
+
readonly name: "Vietnamese Dong";
|
|
170
|
+
};
|
|
171
|
+
readonly ZAR: {
|
|
172
|
+
readonly code: "ZAR";
|
|
173
|
+
readonly decimals: 2;
|
|
174
|
+
readonly name: "South African Rand";
|
|
175
|
+
};
|
|
176
|
+
};
|
|
177
|
+
type CurrencyCode = keyof typeof CURRENCIES;
|
|
178
|
+
type CurrencyDef = (typeof CURRENCIES)[CurrencyCode];
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Immutable, BigInt-precision monetary value.
|
|
182
|
+
*
|
|
183
|
+
* The generic parameter `C` brands the currency at the type level so
|
|
184
|
+
* cross-currency arithmetic is a **compile-time error**.
|
|
185
|
+
*
|
|
186
|
+
* @example
|
|
187
|
+
* const price = money('19.99', 'USD')
|
|
188
|
+
* const tax = money('1.60', 'USD')
|
|
189
|
+
* price.add(tax).format() // "$21.59"
|
|
190
|
+
* price.add(money('5', 'EUR')) // TypeError: currency mismatch
|
|
191
|
+
*/
|
|
192
|
+
declare class Money<C extends CurrencyCode = CurrencyCode> {
|
|
193
|
+
#private;
|
|
194
|
+
constructor(amount: string | number | bigint, currency: C);
|
|
195
|
+
/** Create from raw minor units (e.g. cents). Useful when reading from a database. */
|
|
196
|
+
static fromMinorUnits<C extends CurrencyCode>(units: bigint, currency: C): Money<C>;
|
|
197
|
+
/** Raw amount in minor units (e.g. cents). Safe for storage and wire transfer. */
|
|
198
|
+
get minorUnits(): bigint;
|
|
199
|
+
get currency(): C;
|
|
200
|
+
/** Human-readable decimal string, e.g. "19.99". */
|
|
201
|
+
get decimalValue(): string;
|
|
202
|
+
add(other: Money<C>): Money<C>;
|
|
203
|
+
subtract(other: Money<C>): Money<C>;
|
|
204
|
+
/**
|
|
205
|
+
* Multiply by a scalar. Uses half-up rounding to the currency's minor unit.
|
|
206
|
+
* @example money('10.00', 'USD').multiply(1.1) // $11.00
|
|
207
|
+
* @example money('10.00', 'USD').multiply('1.5') // $15.00
|
|
208
|
+
*/
|
|
209
|
+
multiply(factor: number | string): Money<C>;
|
|
210
|
+
/**
|
|
211
|
+
* Divide by a scalar. Uses half-up rounding to the currency's minor unit.
|
|
212
|
+
* @example money('10.00', 'USD').divide(3) // $3.33
|
|
213
|
+
*/
|
|
214
|
+
divide(divisor: number | string): Money<C>;
|
|
215
|
+
/**
|
|
216
|
+
* Allocate proportionally. The sum of all parts always equals the original —
|
|
217
|
+
* any rounding remainder is distributed to the first buckets.
|
|
218
|
+
*
|
|
219
|
+
* @example
|
|
220
|
+
* money('10.00', 'USD').allocate([1, 1, 1])
|
|
221
|
+
* // [Money('3.34'), Money('3.33'), Money('3.33')]
|
|
222
|
+
*/
|
|
223
|
+
allocate(ratios: readonly number[]): Money<C>[];
|
|
224
|
+
equals(other: Money<C>): boolean;
|
|
225
|
+
greaterThan(other: Money<C>): boolean;
|
|
226
|
+
lessThan(other: Money<C>): boolean;
|
|
227
|
+
greaterThanOrEqual(other: Money<C>): boolean;
|
|
228
|
+
lessThanOrEqual(other: Money<C>): boolean;
|
|
229
|
+
isZero(): boolean;
|
|
230
|
+
isPositive(): boolean;
|
|
231
|
+
isNegative(): boolean;
|
|
232
|
+
abs(): Money<C>;
|
|
233
|
+
negate(): Money<C>;
|
|
234
|
+
/**
|
|
235
|
+
* Format using the platform's `Intl.NumberFormat` — no custom format strings.
|
|
236
|
+
*
|
|
237
|
+
* @example
|
|
238
|
+
* money('19.99', 'USD').format() // "$19.99"
|
|
239
|
+
* money('19.99', 'EUR').format('de-DE') // "19,99 €"
|
|
240
|
+
*/
|
|
241
|
+
format(locale?: string): string;
|
|
242
|
+
/** Safe to store in JSON — no float representation. */
|
|
243
|
+
toJSON(): {
|
|
244
|
+
amount: string;
|
|
245
|
+
currency: C;
|
|
246
|
+
minorUnits: string;
|
|
247
|
+
};
|
|
248
|
+
toString(): string;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
declare class MonetaError extends Error {
|
|
252
|
+
constructor(message: string);
|
|
253
|
+
}
|
|
254
|
+
declare class CurrencyMismatchError extends MonetaError {
|
|
255
|
+
readonly expected: string;
|
|
256
|
+
readonly received: string;
|
|
257
|
+
constructor(expected: string, received: string);
|
|
258
|
+
}
|
|
259
|
+
declare class InvalidAmountError extends MonetaError {
|
|
260
|
+
readonly input: unknown;
|
|
261
|
+
constructor(input: unknown);
|
|
262
|
+
}
|
|
263
|
+
declare class DivisionByZeroError extends MonetaError {
|
|
264
|
+
constructor();
|
|
265
|
+
}
|
|
266
|
+
declare class UnknownCurrencyError extends MonetaError {
|
|
267
|
+
readonly currency: string;
|
|
268
|
+
constructor(currency: string);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* Create a Money value. This is the primary entry point.
|
|
273
|
+
*
|
|
274
|
+
* @example
|
|
275
|
+
* import { money } from 'moneta'
|
|
276
|
+
*
|
|
277
|
+
* const price = money('19.99', 'USD')
|
|
278
|
+
* const tax = money('1.60', 'USD')
|
|
279
|
+
* price.add(tax).format() // "$21.59"
|
|
280
|
+
*/
|
|
281
|
+
declare function money<C extends CurrencyCode>(amount: string | number | bigint, currency: C): Money<C>;
|
|
282
|
+
|
|
283
|
+
export { CURRENCIES, type CurrencyCode, type CurrencyDef, CurrencyMismatchError, DivisionByZeroError, InvalidAmountError, MonetaError, Money, UnknownCurrencyError, money };
|