subunit-money 2.0.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.
@@ -0,0 +1,536 @@
1
+ {
2
+ "AED": {
3
+ "decimal_digits": 2
4
+ },
5
+ "AFN": {
6
+ "decimal_digits": 2
7
+ },
8
+ "ALL": {
9
+ "decimal_digits": 2
10
+ },
11
+ "AMD": {
12
+ "decimal_digits": 2
13
+ },
14
+ "AOA": {
15
+ "decimal_digits": 2
16
+ },
17
+ "ARS": {
18
+ "decimal_digits": 2
19
+ },
20
+ "AUD": {
21
+ "decimal_digits": 2
22
+ },
23
+ "AWG": {
24
+ "decimal_digits": 2
25
+ },
26
+ "AZN": {
27
+ "decimal_digits": 2
28
+ },
29
+ "BAM": {
30
+ "decimal_digits": 2
31
+ },
32
+ "BBD": {
33
+ "decimal_digits": 2
34
+ },
35
+ "BDT": {
36
+ "decimal_digits": 2
37
+ },
38
+ "BHD": {
39
+ "decimal_digits": 3
40
+ },
41
+ "BIF": {
42
+ "decimal_digits": 0
43
+ },
44
+ "BMD": {
45
+ "decimal_digits": 2
46
+ },
47
+ "BND": {
48
+ "decimal_digits": 2
49
+ },
50
+ "BOB": {
51
+ "decimal_digits": 2
52
+ },
53
+ "BOV": {
54
+ "decimal_digits": 2
55
+ },
56
+ "BRL": {
57
+ "decimal_digits": 2
58
+ },
59
+ "BSD": {
60
+ "decimal_digits": 2
61
+ },
62
+ "BTN": {
63
+ "decimal_digits": 2
64
+ },
65
+ "BWP": {
66
+ "decimal_digits": 2
67
+ },
68
+ "BYN": {
69
+ "decimal_digits": 2
70
+ },
71
+ "BZD": {
72
+ "decimal_digits": 2
73
+ },
74
+ "CAD": {
75
+ "decimal_digits": 2
76
+ },
77
+ "CDF": {
78
+ "decimal_digits": 2
79
+ },
80
+ "CHE": {
81
+ "decimal_digits": 2
82
+ },
83
+ "CHF": {
84
+ "decimal_digits": 2
85
+ },
86
+ "CHW": {
87
+ "decimal_digits": 2
88
+ },
89
+ "CLF": {
90
+ "decimal_digits": 4
91
+ },
92
+ "CLP": {
93
+ "decimal_digits": 0
94
+ },
95
+ "CNY": {
96
+ "decimal_digits": 2
97
+ },
98
+ "COP": {
99
+ "decimal_digits": 2
100
+ },
101
+ "COU": {
102
+ "decimal_digits": 2
103
+ },
104
+ "CRC": {
105
+ "decimal_digits": 2
106
+ },
107
+ "CUP": {
108
+ "decimal_digits": 2
109
+ },
110
+ "CVE": {
111
+ "decimal_digits": 2
112
+ },
113
+ "CZK": {
114
+ "decimal_digits": 2
115
+ },
116
+ "DJF": {
117
+ "decimal_digits": 0
118
+ },
119
+ "DKK": {
120
+ "decimal_digits": 2
121
+ },
122
+ "DOP": {
123
+ "decimal_digits": 2
124
+ },
125
+ "DZD": {
126
+ "decimal_digits": 2
127
+ },
128
+ "EGP": {
129
+ "decimal_digits": 2
130
+ },
131
+ "ERN": {
132
+ "decimal_digits": 2
133
+ },
134
+ "ETB": {
135
+ "decimal_digits": 2
136
+ },
137
+ "EUR": {
138
+ "decimal_digits": 2
139
+ },
140
+ "FJD": {
141
+ "decimal_digits": 2
142
+ },
143
+ "FKP": {
144
+ "decimal_digits": 2
145
+ },
146
+ "GBP": {
147
+ "decimal_digits": 2
148
+ },
149
+ "GEL": {
150
+ "decimal_digits": 2
151
+ },
152
+ "GHS": {
153
+ "decimal_digits": 2
154
+ },
155
+ "GIP": {
156
+ "decimal_digits": 2
157
+ },
158
+ "GMD": {
159
+ "decimal_digits": 2
160
+ },
161
+ "GNF": {
162
+ "decimal_digits": 0
163
+ },
164
+ "GTQ": {
165
+ "decimal_digits": 2
166
+ },
167
+ "GYD": {
168
+ "decimal_digits": 2
169
+ },
170
+ "HKD": {
171
+ "decimal_digits": 2
172
+ },
173
+ "HNL": {
174
+ "decimal_digits": 2
175
+ },
176
+ "HTG": {
177
+ "decimal_digits": 2
178
+ },
179
+ "HUF": {
180
+ "decimal_digits": 2
181
+ },
182
+ "IDR": {
183
+ "decimal_digits": 2
184
+ },
185
+ "ILS": {
186
+ "decimal_digits": 2
187
+ },
188
+ "INR": {
189
+ "decimal_digits": 2
190
+ },
191
+ "IQD": {
192
+ "decimal_digits": 3
193
+ },
194
+ "IRR": {
195
+ "decimal_digits": 2
196
+ },
197
+ "ISK": {
198
+ "decimal_digits": 0
199
+ },
200
+ "JMD": {
201
+ "decimal_digits": 2
202
+ },
203
+ "JOD": {
204
+ "decimal_digits": 3
205
+ },
206
+ "JPY": {
207
+ "decimal_digits": 0
208
+ },
209
+ "KES": {
210
+ "decimal_digits": 2
211
+ },
212
+ "KGS": {
213
+ "decimal_digits": 2
214
+ },
215
+ "KHR": {
216
+ "decimal_digits": 2
217
+ },
218
+ "KMF": {
219
+ "decimal_digits": 0
220
+ },
221
+ "KPW": {
222
+ "decimal_digits": 2
223
+ },
224
+ "KRW": {
225
+ "decimal_digits": 0
226
+ },
227
+ "KWD": {
228
+ "decimal_digits": 3
229
+ },
230
+ "KYD": {
231
+ "decimal_digits": 2
232
+ },
233
+ "KZT": {
234
+ "decimal_digits": 2
235
+ },
236
+ "LAK": {
237
+ "decimal_digits": 2
238
+ },
239
+ "LBP": {
240
+ "decimal_digits": 2
241
+ },
242
+ "LKR": {
243
+ "decimal_digits": 2
244
+ },
245
+ "LRD": {
246
+ "decimal_digits": 2
247
+ },
248
+ "LSL": {
249
+ "decimal_digits": 2
250
+ },
251
+ "LYD": {
252
+ "decimal_digits": 3
253
+ },
254
+ "MAD": {
255
+ "decimal_digits": 2
256
+ },
257
+ "MDL": {
258
+ "decimal_digits": 2
259
+ },
260
+ "MGA": {
261
+ "decimal_digits": 2
262
+ },
263
+ "MKD": {
264
+ "decimal_digits": 2
265
+ },
266
+ "MMK": {
267
+ "decimal_digits": 2
268
+ },
269
+ "MNT": {
270
+ "decimal_digits": 2
271
+ },
272
+ "MOP": {
273
+ "decimal_digits": 2
274
+ },
275
+ "MRU": {
276
+ "decimal_digits": 2
277
+ },
278
+ "MUR": {
279
+ "decimal_digits": 2
280
+ },
281
+ "MVR": {
282
+ "decimal_digits": 2
283
+ },
284
+ "MWK": {
285
+ "decimal_digits": 2
286
+ },
287
+ "MXN": {
288
+ "decimal_digits": 2
289
+ },
290
+ "MXV": {
291
+ "decimal_digits": 2
292
+ },
293
+ "MYR": {
294
+ "decimal_digits": 2
295
+ },
296
+ "MZN": {
297
+ "decimal_digits": 2
298
+ },
299
+ "NAD": {
300
+ "decimal_digits": 2
301
+ },
302
+ "NGN": {
303
+ "decimal_digits": 2
304
+ },
305
+ "NIO": {
306
+ "decimal_digits": 2
307
+ },
308
+ "NOK": {
309
+ "decimal_digits": 2
310
+ },
311
+ "NPR": {
312
+ "decimal_digits": 2
313
+ },
314
+ "NZD": {
315
+ "decimal_digits": 2
316
+ },
317
+ "OMR": {
318
+ "decimal_digits": 3
319
+ },
320
+ "PAB": {
321
+ "decimal_digits": 2
322
+ },
323
+ "PEN": {
324
+ "decimal_digits": 2
325
+ },
326
+ "PGK": {
327
+ "decimal_digits": 2
328
+ },
329
+ "PHP": {
330
+ "decimal_digits": 2
331
+ },
332
+ "PKR": {
333
+ "decimal_digits": 2
334
+ },
335
+ "PLN": {
336
+ "decimal_digits": 2
337
+ },
338
+ "PYG": {
339
+ "decimal_digits": 0
340
+ },
341
+ "QAR": {
342
+ "decimal_digits": 2
343
+ },
344
+ "RON": {
345
+ "decimal_digits": 2
346
+ },
347
+ "RSD": {
348
+ "decimal_digits": 2
349
+ },
350
+ "RUB": {
351
+ "decimal_digits": 2
352
+ },
353
+ "RWF": {
354
+ "decimal_digits": 0
355
+ },
356
+ "SAR": {
357
+ "decimal_digits": 2
358
+ },
359
+ "SBD": {
360
+ "decimal_digits": 2
361
+ },
362
+ "SCR": {
363
+ "decimal_digits": 2
364
+ },
365
+ "SDG": {
366
+ "decimal_digits": 2
367
+ },
368
+ "SEK": {
369
+ "decimal_digits": 2
370
+ },
371
+ "SGD": {
372
+ "decimal_digits": 2
373
+ },
374
+ "SHP": {
375
+ "decimal_digits": 2
376
+ },
377
+ "SLE": {
378
+ "decimal_digits": 2
379
+ },
380
+ "SOS": {
381
+ "decimal_digits": 2
382
+ },
383
+ "SRD": {
384
+ "decimal_digits": 2
385
+ },
386
+ "SSP": {
387
+ "decimal_digits": 2
388
+ },
389
+ "STN": {
390
+ "decimal_digits": 2
391
+ },
392
+ "SVC": {
393
+ "decimal_digits": 2
394
+ },
395
+ "SYP": {
396
+ "decimal_digits": 2
397
+ },
398
+ "SZL": {
399
+ "decimal_digits": 2
400
+ },
401
+ "THB": {
402
+ "decimal_digits": 2
403
+ },
404
+ "TJS": {
405
+ "decimal_digits": 2
406
+ },
407
+ "TMT": {
408
+ "decimal_digits": 2
409
+ },
410
+ "TND": {
411
+ "decimal_digits": 3
412
+ },
413
+ "TOP": {
414
+ "decimal_digits": 2
415
+ },
416
+ "TRY": {
417
+ "decimal_digits": 2
418
+ },
419
+ "TTD": {
420
+ "decimal_digits": 2
421
+ },
422
+ "TWD": {
423
+ "decimal_digits": 2
424
+ },
425
+ "TZS": {
426
+ "decimal_digits": 2
427
+ },
428
+ "UAH": {
429
+ "decimal_digits": 2
430
+ },
431
+ "UGX": {
432
+ "decimal_digits": 0
433
+ },
434
+ "USD": {
435
+ "decimal_digits": 2
436
+ },
437
+ "USN": {
438
+ "decimal_digits": 2
439
+ },
440
+ "UYI": {
441
+ "decimal_digits": 0
442
+ },
443
+ "UYU": {
444
+ "decimal_digits": 2
445
+ },
446
+ "UYW": {
447
+ "decimal_digits": 4
448
+ },
449
+ "UZS": {
450
+ "decimal_digits": 2
451
+ },
452
+ "VED": {
453
+ "decimal_digits": 2
454
+ },
455
+ "VES": {
456
+ "decimal_digits": 2
457
+ },
458
+ "VND": {
459
+ "decimal_digits": 0
460
+ },
461
+ "VUV": {
462
+ "decimal_digits": 0
463
+ },
464
+ "WST": {
465
+ "decimal_digits": 2
466
+ },
467
+ "XAD": {
468
+ "decimal_digits": 2
469
+ },
470
+ "XAF": {
471
+ "decimal_digits": 0
472
+ },
473
+ "XAG": {
474
+ "decimal_digits": 0
475
+ },
476
+ "XAU": {
477
+ "decimal_digits": 0
478
+ },
479
+ "XBA": {
480
+ "decimal_digits": 0
481
+ },
482
+ "XBB": {
483
+ "decimal_digits": 0
484
+ },
485
+ "XBC": {
486
+ "decimal_digits": 0
487
+ },
488
+ "XBD": {
489
+ "decimal_digits": 0
490
+ },
491
+ "XCD": {
492
+ "decimal_digits": 2
493
+ },
494
+ "XCG": {
495
+ "decimal_digits": 2
496
+ },
497
+ "XDR": {
498
+ "decimal_digits": 0
499
+ },
500
+ "XOF": {
501
+ "decimal_digits": 0
502
+ },
503
+ "XPD": {
504
+ "decimal_digits": 0
505
+ },
506
+ "XPF": {
507
+ "decimal_digits": 0
508
+ },
509
+ "XPT": {
510
+ "decimal_digits": 0
511
+ },
512
+ "XSU": {
513
+ "decimal_digits": 0
514
+ },
515
+ "XTS": {
516
+ "decimal_digits": 0
517
+ },
518
+ "XUA": {
519
+ "decimal_digits": 0
520
+ },
521
+ "XXX": {
522
+ "decimal_digits": 0
523
+ },
524
+ "YER": {
525
+ "decimal_digits": 2
526
+ },
527
+ "ZAR": {
528
+ "decimal_digits": 2
529
+ },
530
+ "ZMW": {
531
+ "decimal_digits": 2
532
+ },
533
+ "ZWG": {
534
+ "decimal_digits": 2
535
+ }
536
+ }
@@ -0,0 +1,38 @@
1
+ /**
2
+ * Currency registry and types.
3
+ * Manages ISO 4217 currency definitions and custom currencies.
4
+ */
5
+ export interface CurrencyDefinition {
6
+ code: string;
7
+ decimalDigits: number;
8
+ }
9
+ /**
10
+ * Register a new currency or update an existing one.
11
+ * @param code - ISO 4217 currency code (e.g., 'USD', 'EUR', 'BTC')
12
+ * @param decimalDigits - Number of decimal places (e.g., 2 for USD, 8 for BTC)
13
+ */
14
+ export declare function registerCurrency(code: string, decimalDigits: number): void;
15
+ /**
16
+ * Get a currency definition by code.
17
+ * @returns The currency definition, or undefined if not registered
18
+ */
19
+ export declare function getCurrency(code: string): CurrencyDefinition | undefined;
20
+ /**
21
+ * Check if a currency is registered.
22
+ */
23
+ export declare function hasCurrency(code: string): boolean;
24
+ /**
25
+ * Get all registered currencies, sorted by code.
26
+ */
27
+ export declare function getAllCurrencies(): CurrencyDefinition[];
28
+ /**
29
+ * Load currencies from the legacy currencymap.json format.
30
+ * @param map - Object with currency codes as keys and {decimal_digits: number} as values
31
+ */
32
+ export declare function loadCurrencyMap(map: Record<string, {
33
+ decimal_digits: number;
34
+ }>): void;
35
+ /**
36
+ * Clear all registered currencies. Useful for testing.
37
+ */
38
+ export declare function clearCurrencies(): void;
@@ -0,0 +1,56 @@
1
+ "use strict";
2
+ /**
3
+ * Currency registry and types.
4
+ * Manages ISO 4217 currency definitions and custom currencies.
5
+ */
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.registerCurrency = registerCurrency;
8
+ exports.getCurrency = getCurrency;
9
+ exports.hasCurrency = hasCurrency;
10
+ exports.getAllCurrencies = getAllCurrencies;
11
+ exports.loadCurrencyMap = loadCurrencyMap;
12
+ exports.clearCurrencies = clearCurrencies;
13
+ // Internal registry - mutable for registerCurrency()
14
+ const currencies = new Map();
15
+ /**
16
+ * Register a new currency or update an existing one.
17
+ * @param code - ISO 4217 currency code (e.g., 'USD', 'EUR', 'BTC')
18
+ * @param decimalDigits - Number of decimal places (e.g., 2 for USD, 8 for BTC)
19
+ */
20
+ function registerCurrency(code, decimalDigits) {
21
+ currencies.set(code, { code, decimalDigits });
22
+ }
23
+ /**
24
+ * Get a currency definition by code.
25
+ * @returns The currency definition, or undefined if not registered
26
+ */
27
+ function getCurrency(code) {
28
+ return currencies.get(code);
29
+ }
30
+ /**
31
+ * Check if a currency is registered.
32
+ */
33
+ function hasCurrency(code) {
34
+ return currencies.has(code);
35
+ }
36
+ /**
37
+ * Get all registered currencies, sorted by code.
38
+ */
39
+ function getAllCurrencies() {
40
+ return Array.from(currencies.values()).sort((a, b) => a.code.localeCompare(b.code));
41
+ }
42
+ /**
43
+ * Load currencies from the legacy currencymap.json format.
44
+ * @param map - Object with currency codes as keys and {decimal_digits: number} as values
45
+ */
46
+ function loadCurrencyMap(map) {
47
+ for (const [code, data] of Object.entries(map)) {
48
+ registerCurrency(code, data.decimal_digits);
49
+ }
50
+ }
51
+ /**
52
+ * Clear all registered currencies. Useful for testing.
53
+ */
54
+ function clearCurrencies() {
55
+ currencies.clear();
56
+ }
@@ -0,0 +1,52 @@
1
+ /**
2
+ * Custom error types for Money operations.
3
+ * All errors extend built-in Error types for proper instanceof checks.
4
+ */
5
+ /**
6
+ * Thrown when attempting operations between different currencies.
7
+ * @example
8
+ * new Money('USD', 10).add(new Money('EUR', 5)) // throws CurrencyMismatchError
9
+ */
10
+ export declare class CurrencyMismatchError extends TypeError {
11
+ readonly fromCurrency: string;
12
+ readonly toCurrency: string;
13
+ constructor(fromCurrency: string, toCurrency: string);
14
+ }
15
+ /**
16
+ * Thrown when using an unregistered currency code.
17
+ * @example
18
+ * new Money('FAKE', 10) // throws CurrencyUnknownError
19
+ */
20
+ export declare class CurrencyUnknownError extends TypeError {
21
+ readonly currency: string;
22
+ constructor(currency: string);
23
+ }
24
+ /**
25
+ * Thrown when an amount has more decimal places than the currency allows.
26
+ * @example
27
+ * new Money('USD', '1.234') // throws SubunitError (USD only allows 2 decimals)
28
+ */
29
+ export declare class SubunitError extends RangeError {
30
+ readonly currency: string;
31
+ readonly maxDecimals: number;
32
+ constructor(currency: string, maxDecimals: number);
33
+ }
34
+ /**
35
+ * Thrown when an amount cannot be parsed as a valid number.
36
+ * @example
37
+ * new Money('USD', 'abc') // throws AmountError
38
+ */
39
+ export declare class AmountError extends TypeError {
40
+ readonly amount: unknown;
41
+ constructor(amount: unknown);
42
+ }
43
+ /**
44
+ * Thrown when an exchange rate is not available.
45
+ * @example
46
+ * converter.convert(usdMoney, 'XYZ') // throws ExchangeRateError if no USD->XYZ rate
47
+ */
48
+ export declare class ExchangeRateError extends Error {
49
+ readonly fromCurrency: string;
50
+ readonly toCurrency: string;
51
+ constructor(fromCurrency: string, toCurrency: string);
52
+ }