@f-o-t/money 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.
@@ -0,0 +1,904 @@
1
+ import { z } from "zod";
2
+ /**
3
+ * Schema for validating ISO 4217 currency codes
4
+ *
5
+ * - Exactly 3 characters
6
+ * - Uppercase letters only
7
+ */
8
+ declare const CurrencyCodeSchema: z.ZodString;
9
+ /**
10
+ * Schema for Currency definition
11
+ *
12
+ * Used for registering custom currencies.
13
+ */
14
+ declare const CurrencySchema: z.ZodObject<{
15
+ code: z.ZodString;
16
+ numericCode: z.ZodNumber;
17
+ name: z.ZodString;
18
+ decimalPlaces: z.ZodNumber;
19
+ symbol: z.ZodOptional<z.ZodString>;
20
+ subunitName: z.ZodOptional<z.ZodString>;
21
+ }, z.core.$strip>;
22
+ /**
23
+ * Currency type inferred from schema
24
+ */
25
+ type Currency = z.infer<typeof CurrencySchema>;
26
+ /**
27
+ * Schema for validating decimal amount strings
28
+ *
29
+ * Accepts:
30
+ * - Positive numbers: "123.45", "100", "0.5"
31
+ * - Negative numbers: "-123.45", "-100"
32
+ * - Integer strings: "100", "-50"
33
+ */
34
+ declare const AmountStringSchema: z.ZodString;
35
+ /**
36
+ * Schema for Money JSON representation
37
+ *
38
+ * Used for API requests/responses and JSON serialization.
39
+ *
40
+ * @example
41
+ * { amount: "123.45", currency: "USD" }
42
+ */
43
+ declare const MoneySchema: z.ZodObject<{
44
+ amount: z.ZodString;
45
+ currency: z.ZodString;
46
+ }, z.core.$strip>;
47
+ /**
48
+ * MoneyJSON type - serializable representation of Money
49
+ */
50
+ type MoneyJSON = z.infer<typeof MoneySchema>;
51
+ /**
52
+ * Schema for database Money storage
53
+ *
54
+ * Identical to MoneySchema but with a distinct type for clarity.
55
+ */
56
+ declare const DatabaseMoneySchema: z.ZodObject<{
57
+ amount: z.ZodString;
58
+ currency: z.ZodString;
59
+ }, z.core.$strip>;
60
+ /**
61
+ * DatabaseMoney type for database storage
62
+ */
63
+ type DatabaseMoney = z.infer<typeof DatabaseMoneySchema>;
64
+ /**
65
+ * Schema for user input of money amounts
66
+ *
67
+ * More permissive than AmountStringSchema - accepts both strings and numbers.
68
+ */
69
+ declare const MoneyInputSchema: z.ZodObject<{
70
+ amount: z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>;
71
+ currency: z.ZodString;
72
+ }, z.core.$strip>;
73
+ /**
74
+ * MoneyInput type for flexible user input
75
+ */
76
+ type MoneyInput = z.infer<typeof MoneyInputSchema>;
77
+ /**
78
+ * Schema for the internal Money representation
79
+ *
80
+ * Note: This schema is for validation only. The actual Money type uses BigInt
81
+ * which is handled separately since Zod's bigint requires different parsing.
82
+ */
83
+ declare const MoneyInternalSchema: z.ZodObject<{
84
+ amount: z.ZodBigInt;
85
+ currency: z.ZodString;
86
+ scale: z.ZodNumber;
87
+ }, z.core.$strip>;
88
+ /**
89
+ * Core Money type representing a monetary value with currency
90
+ *
91
+ * @property amount - The amount in minor units (e.g., cents) as a BigInt
92
+ * @property currency - ISO 4217 currency code (e.g., "USD", "BRL", "JPY")
93
+ * @property scale - Number of decimal places for this currency
94
+ */
95
+ type Money = z.infer<typeof MoneyInternalSchema>;
96
+ /**
97
+ * Schema for money formatting options
98
+ */
99
+ declare const FormatOptionsSchema: z.ZodObject<{
100
+ locale: z.ZodOptional<z.ZodString>;
101
+ notation: z.ZodOptional<z.ZodEnum<{
102
+ standard: "standard";
103
+ compact: "compact";
104
+ }>>;
105
+ signDisplay: z.ZodOptional<z.ZodEnum<{
106
+ never: "never";
107
+ auto: "auto";
108
+ always: "always";
109
+ exceptZero: "exceptZero";
110
+ }>>;
111
+ currencyDisplay: z.ZodOptional<z.ZodEnum<{
112
+ symbol: "symbol";
113
+ code: "code";
114
+ name: "name";
115
+ narrowSymbol: "narrowSymbol";
116
+ }>>;
117
+ hideSymbol: z.ZodOptional<z.ZodBoolean>;
118
+ minimumFractionDigits: z.ZodOptional<z.ZodNumber>;
119
+ maximumFractionDigits: z.ZodOptional<z.ZodNumber>;
120
+ }, z.core.$strip>;
121
+ /**
122
+ * FormatOptions type for formatting configuration
123
+ */
124
+ type FormatOptions = z.infer<typeof FormatOptionsSchema>;
125
+ /**
126
+ * Schema for allocation ratios
127
+ *
128
+ * - Non-empty array
129
+ * - Non-negative numbers
130
+ * - At least one non-zero value
131
+ */
132
+ declare const AllocationRatiosSchema: z.ZodArray<z.ZodNumber>;
133
+ /**
134
+ * AllocationRatios type
135
+ */
136
+ type AllocationRatios = z.infer<typeof AllocationRatiosSchema>;
137
+ /**
138
+ * @deprecated Use Currency instead
139
+ */
140
+ type CurrencyInput = Currency;
141
+ /**
142
+ * Rounding mode for amount parsing
143
+ */
144
+ type RoundingMode = "truncate" | "round";
145
+ /**
146
+ * Assert that two Money values have the same currency and scale
147
+ * @throws CurrencyMismatchError if currencies don't match
148
+ * @throws ScaleMismatchError if scales don't match
149
+ */
150
+ declare function assertSameCurrency(a: Money, b: Money): void;
151
+ /**
152
+ * Assert all Money values in an array have the same currency and scale
153
+ * @throws CurrencyMismatchError if currencies don't match
154
+ * @throws ScaleMismatchError if scales don't match
155
+ */
156
+ declare function assertAllSameCurrency(moneys: Money[]): void;
157
+ /**
158
+ * Create an immutable Money object
159
+ *
160
+ * @param amount - Amount in minor units as BigInt
161
+ * @param currency - ISO 4217 currency code (uppercase)
162
+ * @param scale - Number of decimal places for this currency
163
+ * @returns Frozen Money object
164
+ */
165
+ declare function createMoney(amount: bigint, currency: string, scale: number): Money;
166
+ /**
167
+ * Parse a decimal string to minor units
168
+ *
169
+ * @param amountStr - Decimal string (e.g., "123.45", "100", "-50.5")
170
+ * @param scale - Target scale (decimal places)
171
+ * @param roundingMode - How to handle excess decimal places (default: "truncate")
172
+ * @returns Amount in minor units as BigInt
173
+ */
174
+ declare function parseDecimalToMinorUnits(amountStr: string, scale: number, roundingMode?: RoundingMode): bigint;
175
+ /**
176
+ * Convert minor units to decimal string
177
+ *
178
+ * @param amount - Amount in minor units
179
+ * @param scale - Number of decimal places
180
+ * @returns Decimal string representation
181
+ */
182
+ declare function minorUnitsToDecimal(amount: bigint, scale: number): string;
183
+ /**
184
+ * Create Money from a decimal amount
185
+ *
186
+ * By default, excess decimal places are TRUNCATED, not rounded.
187
+ * Use roundingMode parameter or ofRounded() for rounding behavior.
188
+ *
189
+ * IMPORTANT: Always pass strings for amounts to avoid floating-point precision issues.
190
+ * Passing numbers like (0.1 + 0.2) will produce incorrect results due to JavaScript
191
+ * floating-point arithmetic limitations.
192
+ *
193
+ * @param amount - Decimal amount as STRING (preferred) or number
194
+ * @param currency - ISO 4217 currency code
195
+ * @param roundingMode - How to handle excess decimal places (default: "truncate")
196
+ * @returns Money instance
197
+ *
198
+ * @example
199
+ * // CORRECT - String amounts are precise
200
+ * of("123.45", "USD")
201
+ * of("0.1", "USD")
202
+ *
203
+ * // AVOID - Number literals work but strings are safer
204
+ * of(123.45, "USD")
205
+ *
206
+ * // WRONG - Computed numbers lose precision
207
+ * of(0.1 + 0.2, "USD") // Results in 0.30000000000000004
208
+ */
209
+ declare function of(amount: number | string, currency: string, roundingMode?: RoundingMode): Money;
210
+ /**
211
+ * Create Money from a decimal amount with rounding
212
+ *
213
+ * Convenience function that rounds excess decimal places using banker's rounding.
214
+ * Equivalent to: of(amount, currency, "round")
215
+ *
216
+ * @param amount - Decimal amount as string or number
217
+ * @param currency - ISO 4217 currency code
218
+ * @returns Money instance with rounded amount
219
+ *
220
+ * @example
221
+ * ofRounded("10.999", "USD") // $11.00
222
+ * ofRounded("10.995", "USD") // $11.00 (rounds to even)
223
+ * ofRounded("10.994", "USD") // $10.99
224
+ */
225
+ declare function ofRounded(amount: number | string, currency: string): Money;
226
+ /**
227
+ * Create Money from minor units (cents, pence, etc.)
228
+ *
229
+ * Use this when you already have the amount in the smallest currency unit.
230
+ *
231
+ * @param minorUnits - Amount in minor units (e.g., cents)
232
+ * @param currency - ISO 4217 currency code
233
+ * @returns Money instance
234
+ *
235
+ * @example
236
+ * fromMinorUnits(12345, "USD") // $123.45
237
+ * fromMinorUnits(12345n, "USD") // Same, using BigInt
238
+ * fromMinorUnits(100, "JPY") // ¥100 (no decimal places)
239
+ */
240
+ declare function fromMinorUnits(minorUnits: number | bigint, currency: string): Money;
241
+ /**
242
+ * Create zero Money for a currency
243
+ *
244
+ * @param currency - ISO 4217 currency code
245
+ * @returns Money instance with zero amount
246
+ *
247
+ * @example
248
+ * zero("USD") // $0.00
249
+ * zero("JPY") // ¥0
250
+ */
251
+ declare function zero(currency: string): Money;
252
+ /**
253
+ * Create Money from major units (dollars, reais, etc.)
254
+ *
255
+ * Convenience alias for `of` to make intent clearer.
256
+ *
257
+ * @param amount - Amount in major units
258
+ * @param currency - ISO 4217 currency code
259
+ * @returns Money instance
260
+ */
261
+ declare function fromMajorUnits(amount: number | string, currency: string): Money;
262
+ /**
263
+ * Banker's rounding (round half to even)
264
+ *
265
+ * When the value is exactly halfway between two values, rounds to the nearest even number.
266
+ * This prevents systematic bias that would occur with traditional rounding.
267
+ *
268
+ * Halfway detection: remainder * 2 == divisor (works for both odd and even divisors)
269
+ *
270
+ * Examples:
271
+ * - 25 / 10 = 2.5 → 2 (rounds down to even)
272
+ * - 35 / 10 = 3.5 → 4 (rounds up to even)
273
+ * - 24 / 10 = 2.4 → 2 (normal round down)
274
+ * - 26 / 10 = 2.6 → 3 (normal round up)
275
+ * - 21 / 6 = 3.5 → 4 (halfway, round to even)
276
+ * - 15 / 7 = 2.14... → 2 (not halfway, round down)
277
+ *
278
+ * @param value - The value to round (numerator)
279
+ * @param divisor - The divisor to round by
280
+ * @returns Rounded quotient
281
+ */
282
+ declare function bankersRound(value: bigint, divisor: bigint): bigint;
283
+ /**
284
+ * Extended precision for intermediate calculations
285
+ * Using 18 decimal places to handle even ETH-level precision
286
+ */
287
+ declare const EXTENDED_PRECISION = 18;
288
+ /**
289
+ * Scale factor for extended precision calculations
290
+ */
291
+ declare const PRECISION_FACTOR: bigint;
292
+ /**
293
+ * ISO 4217 currency definitions
294
+ *
295
+ * Includes:
296
+ * - Major world currencies (USD, EUR, GBP, etc.)
297
+ * - Zero decimal currencies (JPY, KRW, VND)
298
+ * - Three decimal currencies (KWD, BHD, OMR)
299
+ * - Popular cryptocurrencies (BTC, ETH)
300
+ */
301
+ declare const ISO_4217_CURRENCIES: Record<string, Currency>;
302
+ /**
303
+ * Get a currency by its code
304
+ *
305
+ * @param code - ISO 4217 currency code (case-insensitive)
306
+ * @returns Currency definition
307
+ * @throws UnknownCurrencyError if currency is not found
308
+ */
309
+ declare function getCurrency(code: string): Currency;
310
+ /**
311
+ * Register a custom currency
312
+ *
313
+ * Can be used to add new currencies or override existing ones.
314
+ *
315
+ * @param currency - Currency definition to register
316
+ */
317
+ declare function registerCurrency(currency: Currency): void;
318
+ /**
319
+ * Check if a currency code is registered
320
+ *
321
+ * @param code - ISO 4217 currency code (case-insensitive)
322
+ * @returns true if currency exists
323
+ */
324
+ declare function hasCurrency(code: string): boolean;
325
+ /**
326
+ * Get all registered currencies (both ISO and custom)
327
+ *
328
+ * @returns Record of all currencies keyed by code
329
+ */
330
+ declare function getAllCurrencies(): Record<string, Currency>;
331
+ /**
332
+ * Clear all custom currencies (useful for testing)
333
+ */
334
+ declare function clearCustomCurrencies(): void;
335
+ /**
336
+ * Base error class for all money-related errors
337
+ */
338
+ declare class MoneyError extends Error {
339
+ constructor(message: string);
340
+ }
341
+ /**
342
+ * Error thrown when attempting operations on different currencies
343
+ */
344
+ declare class CurrencyMismatchError extends MoneyError {
345
+ readonly currencyA: string;
346
+ readonly currencyB: string;
347
+ constructor(message: string, currencyA: string, currencyB: string);
348
+ static create(currencyA: string, currencyB: string): CurrencyMismatchError;
349
+ }
350
+ /**
351
+ * Error thrown when an invalid amount is provided
352
+ */
353
+ declare class InvalidAmountError extends MoneyError {
354
+ constructor(message: string);
355
+ }
356
+ /**
357
+ * Error thrown when attempting to divide by zero
358
+ */
359
+ declare class DivisionByZeroError extends MoneyError {
360
+ constructor(message?: string);
361
+ }
362
+ /**
363
+ * Error thrown when a currency code is not found in the registry
364
+ */
365
+ declare class UnknownCurrencyError extends MoneyError {
366
+ readonly currencyCode?: string | undefined;
367
+ constructor(message: string, currencyCode?: string | undefined);
368
+ }
369
+ /**
370
+ * Error thrown when a value exceeds safe integer range
371
+ */
372
+ declare class OverflowError extends MoneyError {
373
+ constructor(message?: string);
374
+ }
375
+ /**
376
+ * Error thrown when Money values have inconsistent scales for the same currency
377
+ */
378
+ declare class ScaleMismatchError extends MoneyError {
379
+ readonly currency: string;
380
+ readonly scaleA: number;
381
+ readonly scaleB: number;
382
+ constructor(message: string, currency: string, scaleA: number, scaleB: number);
383
+ static create(currency: string, scaleA: number, scaleB: number): ScaleMismatchError;
384
+ }
385
+ /**
386
+ * Format Money as a localized currency string
387
+ *
388
+ * Uses Intl.NumberFormat for locale-aware formatting.
389
+ *
390
+ * @param money - Money value to format
391
+ * @param locale - Locale for formatting (e.g., "en-US", "pt-BR")
392
+ * @param options - Formatting options
393
+ * @returns Formatted currency string
394
+ *
395
+ * @example
396
+ * format(of("1234.56", "USD"), "en-US") // "$1,234.56"
397
+ * format(of("1234.56", "BRL"), "pt-BR") // "R$ 1.234,56"
398
+ * format(of("1234", "JPY"), "ja-JP") // "¥1,234"
399
+ * format(of("1234567.89", "USD"), "en-US", { notation: "compact" }) // "$1.2M"
400
+ */
401
+ declare function format(money: Money, locale?: string, options?: FormatOptions): string;
402
+ /**
403
+ * Format Money as a compact string (e.g., 1.2K, 3.4M)
404
+ *
405
+ * @param money - Money value to format
406
+ * @param locale - Locale for formatting
407
+ * @returns Compact formatted string
408
+ *
409
+ * @example
410
+ * formatCompact(of("1234567.89", "USD"), "en-US") // "$1.2M"
411
+ * formatCompact(of("1234.56", "BRL"), "pt-BR") // "R$ 1,23 mil"
412
+ */
413
+ declare function formatCompact(money: Money, locale?: string): string;
414
+ /**
415
+ * Convert Money to decimal string representation
416
+ *
417
+ * Always includes the appropriate number of decimal places.
418
+ * Useful for JSON serialization or display without locale formatting.
419
+ *
420
+ * @param money - Money value
421
+ * @returns Decimal string (e.g., "123.45", "-50.00", "100")
422
+ *
423
+ * @example
424
+ * toDecimal(of("123.45", "USD")) // "123.45"
425
+ * toDecimal(of("-50", "USD")) // "-50.00"
426
+ * toDecimal(of("100", "JPY")) // "100"
427
+ */
428
+ declare function toDecimal(money: Money): string;
429
+ /**
430
+ * Format Money without currency symbol
431
+ *
432
+ * @param money - Money value
433
+ * @param locale - Locale for number formatting
434
+ * @returns Formatted number without currency symbol
435
+ *
436
+ * @example
437
+ * formatAmount(of("1234.56", "USD"), "en-US") // "1,234.56"
438
+ * formatAmount(of("1234.56", "BRL"), "pt-BR") // "1.234,56"
439
+ */
440
+ declare function formatAmount(money: Money, locale?: string): string;
441
+ /**
442
+ * Parse a formatted currency string into Money
443
+ *
444
+ * Handles locale-specific formatting including:
445
+ * - Different decimal separators (. or ,)
446
+ * - Different grouping separators (, or . or space)
447
+ * - Currency symbols and codes
448
+ * - Negative amounts in various formats
449
+ *
450
+ * @param formatted - Formatted currency string (e.g., "R$ 1.234,56", "$1,234.56")
451
+ * @param locale - Locale used for formatting (e.g., "pt-BR", "en-US")
452
+ * @param currency - ISO 4217 currency code
453
+ * @returns Money instance
454
+ *
455
+ * @example
456
+ * parse("R$ 1.234,56", "pt-BR", "BRL") // 1234.56 BRL
457
+ * parse("$1,234.56", "en-US", "USD") // 1234.56 USD
458
+ * parse("(€1.234,56)", "de-DE", "EUR") // -1234.56 EUR
459
+ * parse("-¥1,234", "ja-JP", "JPY") // -1234 JPY
460
+ */
461
+ declare function parse(formatted: string, locale: string, currency: string): Money;
462
+ /**
463
+ * Sum an array of Money values
464
+ *
465
+ * @param moneys - Array of Money values (must all be same currency)
466
+ * @returns Sum of all values
467
+ * @throws CurrencyMismatchError if currencies don't match
468
+ * @throws InvalidAmountError if array is empty
469
+ *
470
+ * @example
471
+ * sum([of("10.00", "USD"), of("20.00", "USD"), of("30.00", "USD")]) // $60.00
472
+ */
473
+ declare function sum(moneys: Money[]): Money;
474
+ /**
475
+ * Sum an array of Money values, returning zero if empty
476
+ *
477
+ * @param moneys - Array of Money values
478
+ * @param currency - Currency to use for zero if array is empty
479
+ * @returns Sum of all values, or zero of the specified currency
480
+ *
481
+ * @example
482
+ * sumOrZero([of("10.00", "USD")], "USD") // $10.00
483
+ * sumOrZero([], "USD") // $0.00
484
+ */
485
+ declare function sumOrZero(moneys: Money[], currency: string): Money;
486
+ /**
487
+ * Find the minimum Money value
488
+ *
489
+ * @param moneys - Array of Money values (must all be same currency)
490
+ * @returns Minimum value
491
+ * @throws CurrencyMismatchError if currencies don't match
492
+ * @throws InvalidAmountError if array is empty
493
+ *
494
+ * @example
495
+ * min([of("10.00", "USD"), of("5.00", "USD"), of("20.00", "USD")]) // $5.00
496
+ */
497
+ declare function min(moneys: Money[]): Money;
498
+ /**
499
+ * Find the maximum Money value
500
+ *
501
+ * @param moneys - Array of Money values (must all be same currency)
502
+ * @returns Maximum value
503
+ * @throws CurrencyMismatchError if currencies don't match
504
+ * @throws InvalidAmountError if array is empty
505
+ *
506
+ * @example
507
+ * max([of("10.00", "USD"), of("5.00", "USD"), of("20.00", "USD")]) // $20.00
508
+ */
509
+ declare function max(moneys: Money[]): Money;
510
+ /**
511
+ * Calculate the average of Money values
512
+ *
513
+ * Uses banker's rounding for the result.
514
+ *
515
+ * @param moneys - Array of Money values (must all be same currency)
516
+ * @returns Average value with banker's rounding
517
+ * @throws CurrencyMismatchError if currencies don't match
518
+ * @throws InvalidAmountError if array is empty
519
+ *
520
+ * @example
521
+ * average([of("10.00", "USD"), of("20.00", "USD"), of("30.00", "USD")]) // $20.00
522
+ * average([of("10.00", "USD"), of("10.00", "USD"), of("10.00", "USD")]) // $10.00
523
+ * average([of("1.00", "USD"), of("2.00", "USD")]) // $1.50
524
+ */
525
+ declare function average(moneys: Money[]): Money;
526
+ /**
527
+ * Calculate median of Money values
528
+ *
529
+ * For even-length arrays, returns the average of the two middle values.
530
+ *
531
+ * @param moneys - Array of Money values (must all be same currency)
532
+ * @returns Median value
533
+ * @throws CurrencyMismatchError if currencies don't match
534
+ * @throws InvalidAmountError if array is empty
535
+ *
536
+ * @example
537
+ * median([of("1.00", "USD"), of("2.00", "USD"), of("3.00", "USD")]) // $2.00
538
+ * median([of("1.00", "USD"), of("2.00", "USD"), of("3.00", "USD"), of("4.00", "USD")]) // $2.50
539
+ */
540
+ declare function median(moneys: Money[]): Money;
541
+ /**
542
+ * Allocate Money according to ratios using the Largest Remainder Method
543
+ *
544
+ * This algorithm distributes money while guaranteeing that:
545
+ * 1. sum(allocated) === original (no cents are lost or gained)
546
+ * 2. Allocation is fair (remainders are distributed to largest fractional parts)
547
+ * 3. Result is deterministic (same input always produces same output)
548
+ *
549
+ * Algorithm:
550
+ * 1. Calculate each allocation's ideal fractional share
551
+ * 2. Round down to get initial integer amounts
552
+ * 3. Calculate remainders (fractional parts lost)
553
+ * 4. Sort by remainder descending
554
+ * 5. Distribute remaining cents one by one, starting with largest remainders
555
+ *
556
+ * @param money - Money to allocate
557
+ * @param ratios - Proportions to allocate (e.g., [60, 25, 15] or [1, 1, 1])
558
+ * @returns Array of Money values that sum to the original
559
+ * @throws InvalidAmountError if ratios are invalid
560
+ *
561
+ * @example
562
+ * allocate(of("100.00", "USD"), [60, 25, 15]) // [$60.00, $25.00, $15.00]
563
+ * allocate(of("100.00", "USD"), [1, 1, 1]) // [$33.34, $33.33, $33.33]
564
+ * allocate(of("10.00", "USD"), [1, 1, 1]) // [$3.34, $3.33, $3.33]
565
+ * allocate(of("7", "JPY"), [1, 1, 1]) // [¥3, ¥2, ¥2]
566
+ */
567
+ declare function allocate(money: Money, ratios: number[]): Money[];
568
+ /**
569
+ * Split Money equally among n recipients
570
+ *
571
+ * @param money - Money to split
572
+ * @param count - Number of equal parts
573
+ * @returns Array of Money values
574
+ *
575
+ * @example
576
+ * split(of("100.00", "USD"), 3) // [$33.34, $33.33, $33.33]
577
+ * split(of("10.00", "USD"), 4) // [$2.50, $2.50, $2.50, $2.50]
578
+ */
579
+ declare function split(money: Money, count: number): Money[];
580
+ /**
581
+ * Add two Money values
582
+ *
583
+ * @param a - First Money value
584
+ * @param b - Second Money value (must be same currency)
585
+ * @returns Sum of a and b
586
+ * @throws CurrencyMismatchError if currencies don't match
587
+ *
588
+ * @example
589
+ * add(of("10.50", "USD"), of("5.25", "USD")) // $15.75
590
+ */
591
+ declare function add(a: Money, b: Money): Money;
592
+ /**
593
+ * Subtract one Money value from another
594
+ *
595
+ * @param a - Money value to subtract from
596
+ * @param b - Money value to subtract (must be same currency)
597
+ * @returns Difference of a - b
598
+ * @throws CurrencyMismatchError if currencies don't match
599
+ *
600
+ * @example
601
+ * subtract(of("10.50", "USD"), of("5.25", "USD")) // $5.25
602
+ */
603
+ declare function subtract(a: Money, b: Money): Money;
604
+ /**
605
+ * Multiply Money by a factor
606
+ *
607
+ * Uses banker's rounding for the final result.
608
+ *
609
+ * @param money - Money value to multiply
610
+ * @param factor - Multiplication factor (number or string for precision)
611
+ * @returns Product with banker's rounding applied
612
+ *
613
+ * @example
614
+ * multiply(of("10.00", "USD"), 1.5) // $15.00
615
+ * multiply(of("10.00", "USD"), "1.5") // $15.00 (string for precision)
616
+ * multiply(of("33.33", "USD"), 3) // $99.99
617
+ */
618
+ declare function multiply(money: Money, factor: number | string): Money;
619
+ /**
620
+ * Divide Money by a divisor
621
+ *
622
+ * Uses banker's rounding for the final result.
623
+ *
624
+ * @param money - Money value to divide
625
+ * @param divisor - Division factor (number or string for precision)
626
+ * @returns Quotient with banker's rounding applied
627
+ * @throws DivisionByZeroError if divisor is zero
628
+ *
629
+ * @example
630
+ * divide(of("10.00", "USD"), 3) // $3.33 (banker's rounding)
631
+ * divide(of("100.00", "USD"), 4) // $25.00
632
+ */
633
+ declare function divide(money: Money, divisor: number | string): Money;
634
+ /**
635
+ * Calculate a percentage of Money
636
+ *
637
+ * @param money - Money value
638
+ * @param percent - Percentage (e.g., 15 for 15%)
639
+ * @returns Percentage amount with banker's rounding
640
+ *
641
+ * @example
642
+ * percentage(of("100.00", "USD"), 15) // $15.00
643
+ * percentage(of("200.00", "USD"), 7.5) // $15.00
644
+ */
645
+ declare function percentage(money: Money, percent: number): Money;
646
+ /**
647
+ * Negate a Money value
648
+ *
649
+ * @param money - Money value to negate
650
+ * @returns Money with opposite sign
651
+ *
652
+ * @example
653
+ * negate(of("10.00", "USD")) // -$10.00
654
+ * negate(of("-5.00", "USD")) // $5.00
655
+ */
656
+ declare function negate(money: Money): Money;
657
+ /**
658
+ * Get absolute value of Money
659
+ *
660
+ * @param money - Money value
661
+ * @returns Money with positive amount
662
+ *
663
+ * @example
664
+ * absolute(of("-10.00", "USD")) // $10.00
665
+ * absolute(of("10.00", "USD")) // $10.00
666
+ */
667
+ declare function absolute(money: Money): Money;
668
+ /**
669
+ * Money equals operator
670
+ */
671
+ declare const moneyEqualsOperator: import("@f-o-t/condition-evaluator").CustomOperatorConfig<"money_eq", unknown, unknown>;
672
+ /**
673
+ * Money not equals operator
674
+ */
675
+ declare const moneyNotEqualsOperator: import("@f-o-t/condition-evaluator").CustomOperatorConfig<"money_neq", unknown, unknown>;
676
+ /**
677
+ * Money greater than operator
678
+ */
679
+ declare const moneyGreaterThanOperator: import("@f-o-t/condition-evaluator").CustomOperatorConfig<"money_gt", unknown, unknown>;
680
+ /**
681
+ * Money greater than or equal operator
682
+ */
683
+ declare const moneyGreaterThanOrEqualOperator: import("@f-o-t/condition-evaluator").CustomOperatorConfig<"money_gte", unknown, unknown>;
684
+ /**
685
+ * Money less than operator
686
+ */
687
+ declare const moneyLessThanOperator: import("@f-o-t/condition-evaluator").CustomOperatorConfig<"money_lt", unknown, unknown>;
688
+ /**
689
+ * Money less than or equal operator
690
+ */
691
+ declare const moneyLessThanOrEqualOperator: import("@f-o-t/condition-evaluator").CustomOperatorConfig<"money_lte", unknown, unknown>;
692
+ /**
693
+ * Money between operator (inclusive)
694
+ */
695
+ declare const moneyBetweenOperator: import("@f-o-t/condition-evaluator").CustomOperatorConfig<"money_between", unknown, unknown>;
696
+ /**
697
+ * Money is positive operator
698
+ */
699
+ declare const moneyPositiveOperator: import("@f-o-t/condition-evaluator").CustomOperatorConfig<"money_positive", unknown, unknown>;
700
+ /**
701
+ * Money is negative operator
702
+ */
703
+ declare const moneyNegativeOperator: import("@f-o-t/condition-evaluator").CustomOperatorConfig<"money_negative", unknown, unknown>;
704
+ /**
705
+ * Money is zero operator
706
+ */
707
+ declare const moneyZeroOperator: import("@f-o-t/condition-evaluator").CustomOperatorConfig<"money_zero", unknown, unknown>;
708
+ /**
709
+ * Check if two Money values are equal
710
+ *
711
+ * @example
712
+ * equals(of("10.00", "USD"), of("10.00", "USD")) // true
713
+ */
714
+ declare function equals(a: Money, b: Money): boolean;
715
+ /**
716
+ * Check if first Money is greater than second
717
+ *
718
+ * @example
719
+ * greaterThan(of("20.00", "USD"), of("10.00", "USD")) // true
720
+ */
721
+ declare function greaterThan(a: Money, b: Money): boolean;
722
+ /**
723
+ * Check if first Money is greater than or equal to second
724
+ */
725
+ declare function greaterThanOrEqual(a: Money, b: Money): boolean;
726
+ /**
727
+ * Check if first Money is less than second
728
+ *
729
+ * @example
730
+ * lessThan(of("10.00", "USD"), of("20.00", "USD")) // true
731
+ */
732
+ declare function lessThan(a: Money, b: Money): boolean;
733
+ /**
734
+ * Check if first Money is less than or equal to second
735
+ */
736
+ declare function lessThanOrEqual(a: Money, b: Money): boolean;
737
+ /**
738
+ * Check if Money is positive (amount > 0)
739
+ *
740
+ * @example
741
+ * isPositive(of("10.00", "USD")) // true
742
+ * isPositive(of("-10.00", "USD")) // false
743
+ */
744
+ declare function isPositive(money: Money): boolean;
745
+ /**
746
+ * Check if Money is negative (amount < 0)
747
+ *
748
+ * @example
749
+ * isNegative(of("-10.00", "USD")) // true
750
+ * isNegative(of("10.00", "USD")) // false
751
+ */
752
+ declare function isNegative(money: Money): boolean;
753
+ /**
754
+ * Check if Money is zero (amount === 0)
755
+ *
756
+ * @example
757
+ * isZero(of("0.00", "USD")) // true
758
+ */
759
+ declare function isZero(money: Money): boolean;
760
+ /**
761
+ * Compare two Money values
762
+ * @returns -1 if a < b, 0 if a === b, 1 if a > b
763
+ *
764
+ * @example
765
+ * compare(of("10.00", "USD"), of("20.00", "USD")) // -1
766
+ * compare(of("20.00", "USD"), of("10.00", "USD")) // 1
767
+ * compare(of("10.00", "USD"), of("10.00", "USD")) // 0
768
+ */
769
+ declare function compare(a: Money, b: Money): -1 | 0 | 1;
770
+ /**
771
+ * Convert Money to minor units as a number
772
+ *
773
+ * Use this when you need to store the amount in a database column
774
+ * that doesn't support BigInt, or when interfacing with APIs that
775
+ * expect number types.
776
+ *
777
+ * @param money - Money value
778
+ * @returns Amount in minor units (cents) as a number
779
+ * @throws OverflowError if the value exceeds Number.MAX_SAFE_INTEGER
780
+ *
781
+ * @example
782
+ * toMinorUnits(of("123.45", "USD")) // 12345
783
+ * toMinorUnits(of("100", "JPY")) // 100
784
+ */
785
+ declare function toMinorUnits(money: Money): number;
786
+ /**
787
+ * Convert Money to minor units as a BigInt
788
+ *
789
+ * Use this when you need the raw BigInt value without any conversion.
790
+ *
791
+ * @param money - Money value
792
+ * @returns Amount in minor units (cents) as BigInt
793
+ *
794
+ * @example
795
+ * toMinorUnitsBigInt(of("123.45", "USD")) // 12345n
796
+ */
797
+ declare function toMinorUnitsBigInt(money: Money): bigint;
798
+ /**
799
+ * Convert Money to major units as a number
800
+ *
801
+ * @deprecated Use toMajorUnitsString() for precision-safe conversion.
802
+ * This function may lose precision for large amounts.
803
+ *
804
+ * @param money - Money value
805
+ * @returns Amount in major units (dollars, reais, etc.) as a number
806
+ *
807
+ * @example
808
+ * toMajorUnits(of("123.45", "USD")) // 123.45
809
+ * toMajorUnits(of("100", "JPY")) // 100
810
+ */
811
+ declare function toMajorUnits(money: Money): number;
812
+ /**
813
+ * Convert Money to major units as a string (precision-safe)
814
+ *
815
+ * Use this for precise decimal representation without precision loss.
816
+ *
817
+ * @param money - Money value
818
+ * @returns Amount in major units as a string (e.g., "123.45")
819
+ *
820
+ * @example
821
+ * toMajorUnitsString(of("123.45", "USD")) // "123.45"
822
+ * toMajorUnitsString(of("999999999999999.99", "USD")) // "999999999999999.99"
823
+ */
824
+ declare function toMajorUnitsString(money: Money): string;
825
+ /**
826
+ * Convert Money to a string of minor units
827
+ *
828
+ * Useful for database storage or APIs that accept string representations.
829
+ *
830
+ * @param money - Money value
831
+ * @returns Amount in minor units as a string
832
+ *
833
+ * @example
834
+ * toMinorUnitsString(of("123.45", "USD")) // "12345"
835
+ */
836
+ declare function toMinorUnitsString(money: Money): string;
837
+ /**
838
+ * Convert Money to JSON representation
839
+ *
840
+ * Stores amount as a decimal string to preserve precision.
841
+ *
842
+ * @param money - Money value
843
+ * @returns JSON-serializable object
844
+ *
845
+ * @example
846
+ * toJSON(of("123.45", "USD"))
847
+ * // { amount: "123.45", currency: "USD" }
848
+ */
849
+ declare function toJSON(money: Money): MoneyJSON;
850
+ /**
851
+ * Create Money from JSON representation
852
+ *
853
+ * @param json - JSON object with amount and currency
854
+ * @returns Money instance
855
+ *
856
+ * @example
857
+ * fromJSON({ amount: "123.45", currency: "USD" })
858
+ * // Money with $123.45
859
+ */
860
+ declare function fromJSON(json: MoneyJSON): Money;
861
+ /**
862
+ * Convert Money to database-friendly format
863
+ *
864
+ * Stores amount as a string to preserve precision in databases
865
+ * that don't support BigInt natively.
866
+ *
867
+ * @param money - Money value
868
+ * @returns Database-friendly object
869
+ *
870
+ * @example
871
+ * toDatabase(of("123.45", "USD"))
872
+ * // { amount: "123.45", currency: "USD" }
873
+ */
874
+ declare function toDatabase(money: Money): DatabaseMoney;
875
+ /**
876
+ * Create Money from database format
877
+ *
878
+ * @param data - Database object with amount and currency
879
+ * @returns Money instance
880
+ */
881
+ declare function fromDatabase(data: DatabaseMoney): Money;
882
+ /**
883
+ * Serialize Money to a compact string format
884
+ *
885
+ * Format: "AMOUNT CURRENCY" (e.g., "123.45 USD")
886
+ *
887
+ * @param money - Money value
888
+ * @returns Compact string representation
889
+ *
890
+ * @example
891
+ * serialize(of("123.45", "USD")) // "123.45 USD"
892
+ */
893
+ declare function serialize(money: Money): string;
894
+ /**
895
+ * Deserialize Money from compact string format
896
+ *
897
+ * @param str - String in format "AMOUNT CURRENCY"
898
+ * @returns Money instance
899
+ *
900
+ * @example
901
+ * deserialize("123.45 USD") // Money with $123.45
902
+ */
903
+ declare function deserialize(str: string): Money;
904
+ export { zero, toMinorUnitsString, toMinorUnitsBigInt, toMinorUnits, toMajorUnitsString, toMajorUnits, toJSON, toDecimal, toDatabase, sumOrZero, sum, subtract, split, serialize, registerCurrency, percentage, parseDecimalToMinorUnits, parse, ofRounded, of, negate, multiply, moneyZeroOperator, moneyPositiveOperator, moneyNotEqualsOperator, moneyNegativeOperator, moneyLessThanOrEqualOperator, moneyLessThanOperator, moneyGreaterThanOrEqualOperator, moneyGreaterThanOperator, moneyEqualsOperator, moneyBetweenOperator, minorUnitsToDecimal, min, median, max, lessThanOrEqual, lessThan, isZero, isPositive, isNegative, hasCurrency, greaterThanOrEqual, greaterThan, getCurrency, getAllCurrencies, fromMinorUnits, fromMajorUnits, fromJSON, fromDatabase, formatCompact, formatAmount, format, equals, divide, deserialize, createMoney, compare, clearCustomCurrencies, bankersRound, average, assertSameCurrency, assertAllSameCurrency, allocate, add, absolute, UnknownCurrencyError, ScaleMismatchError, RoundingMode, PRECISION_FACTOR, OverflowError, MoneySchema, MoneyJSON, MoneyInternalSchema, MoneyInputSchema, MoneyInput, MoneyError, Money, InvalidAmountError, ISO_4217_CURRENCIES, FormatOptionsSchema, FormatOptions, EXTENDED_PRECISION, DivisionByZeroError, DatabaseMoneySchema, DatabaseMoney, CurrencySchema, CurrencyMismatchError, CurrencyInput, CurrencyCodeSchema, Currency, AmountStringSchema, AllocationRatiosSchema, AllocationRatios };