soff-money 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.
Files changed (44) hide show
  1. package/README.md +75 -0
  2. package/dist/core/index.cjs +244 -0
  3. package/dist/core/index.cjs.map +1 -0
  4. package/dist/core/index.d.cts +181 -0
  5. package/dist/core/index.d.ts +181 -0
  6. package/dist/core/index.js +242 -0
  7. package/dist/core/index.js.map +1 -0
  8. package/dist/index.cjs +309 -0
  9. package/dist/index.cjs.map +1 -0
  10. package/dist/index.d.cts +6 -0
  11. package/dist/index.d.ts +6 -0
  12. package/dist/index.js +301 -0
  13. package/dist/index.js.map +1 -0
  14. package/dist/locales/ar.cjs +16 -0
  15. package/dist/locales/ar.cjs.map +1 -0
  16. package/dist/locales/ar.d.cts +9 -0
  17. package/dist/locales/ar.d.ts +9 -0
  18. package/dist/locales/ar.js +14 -0
  19. package/dist/locales/ar.js.map +1 -0
  20. package/dist/locales/br.cjs +16 -0
  21. package/dist/locales/br.cjs.map +1 -0
  22. package/dist/locales/br.d.cts +9 -0
  23. package/dist/locales/br.d.ts +9 -0
  24. package/dist/locales/br.js +14 -0
  25. package/dist/locales/br.js.map +1 -0
  26. package/dist/locales/co.cjs +21 -0
  27. package/dist/locales/co.cjs.map +1 -0
  28. package/dist/locales/co.d.cts +14 -0
  29. package/dist/locales/co.d.ts +14 -0
  30. package/dist/locales/co.js +18 -0
  31. package/dist/locales/co.js.map +1 -0
  32. package/dist/locales/mx.cjs +16 -0
  33. package/dist/locales/mx.cjs.map +1 -0
  34. package/dist/locales/mx.d.cts +9 -0
  35. package/dist/locales/mx.d.ts +9 -0
  36. package/dist/locales/mx.js +14 -0
  37. package/dist/locales/mx.js.map +1 -0
  38. package/dist/locales/us.cjs +16 -0
  39. package/dist/locales/us.cjs.map +1 -0
  40. package/dist/locales/us.d.cts +9 -0
  41. package/dist/locales/us.d.ts +9 -0
  42. package/dist/locales/us.js +14 -0
  43. package/dist/locales/us.js.map +1 -0
  44. package/package.json +135 -0
@@ -0,0 +1,181 @@
1
+ /**
2
+ * Currency configuration for a specific locale
3
+ */
4
+ interface Currency {
5
+ /** ISO 4217 currency code (e.g., 'COP', 'USD') */
6
+ code: string;
7
+ /** Currency symbol (e.g., '$', 'R$') */
8
+ symbol: string;
9
+ /** Number of decimal places (usually 2, but some currencies use 0) */
10
+ decimals: number;
11
+ /** Thousands separator (e.g., '.' for CO, ',' for US) */
12
+ thousandsSeparator: string;
13
+ /** Decimal separator (e.g., ',' for CO, '.' for US) */
14
+ decimalSeparator: string;
15
+ /** Symbol position: 'before' or 'after' the amount */
16
+ symbolPosition: 'before' | 'after';
17
+ /** Space between symbol and amount */
18
+ symbolSpacing: boolean;
19
+ }
20
+ /**
21
+ * Options for formatting money
22
+ */
23
+ interface FormatOptions {
24
+ /** Whether to show the currency symbol */
25
+ showSymbol?: boolean;
26
+ /** Whether to show decimal places */
27
+ showDecimals?: boolean;
28
+ /** Override the default symbol position */
29
+ symbolPosition?: 'before' | 'after';
30
+ }
31
+ /**
32
+ * Result of a money distribution operation
33
+ */
34
+ type DistributionResult = Money[];
35
+ /**
36
+ * Represents an immutable monetary value with safe integer arithmetic
37
+ */
38
+ interface IMoney {
39
+ /** The amount in the smallest unit (cents) */
40
+ readonly cents: number;
41
+ /** The currency configuration */
42
+ readonly currency: Currency;
43
+ /** Add another money value */
44
+ add(other: IMoney): IMoney;
45
+ /** Subtract another money value */
46
+ subtract(other: IMoney): IMoney;
47
+ /** Multiply by a factor */
48
+ multiply(factor: number): IMoney;
49
+ /** Divide by a divisor */
50
+ divide(divisor: number): IMoney;
51
+ /** Distribute evenly without losing cents */
52
+ distribute(parts: number): IMoney[];
53
+ /** Format for display */
54
+ format(options?: FormatOptions): string;
55
+ /** Get the decimal amount */
56
+ toDecimal(): number;
57
+ /** Check equality */
58
+ equals(other: IMoney): boolean;
59
+ /** Check if greater than */
60
+ greaterThan(other: IMoney): boolean;
61
+ /** Check if less than */
62
+ lessThan(other: IMoney): boolean;
63
+ /** Check if zero */
64
+ isZero(): boolean;
65
+ /** Check if positive */
66
+ isPositive(): boolean;
67
+ /** Check if negative */
68
+ isNegative(): boolean;
69
+ }
70
+
71
+ /**
72
+ * Immutable Money class that uses integer arithmetic for precision
73
+ */
74
+ declare class Money implements IMoney {
75
+ readonly cents: number;
76
+ readonly currency: Currency;
77
+ private constructor();
78
+ /**
79
+ * Create money from cents (smallest unit)
80
+ */
81
+ static fromCents(cents: number, currency: Currency): Money;
82
+ /**
83
+ * Create money from a decimal amount
84
+ * @example Money.fromDecimal(100.50, USD) creates $100.50
85
+ */
86
+ static fromDecimal(amount: number, currency: Currency): Money;
87
+ /**
88
+ * Create zero money
89
+ */
90
+ static zero(currency: Currency): Money;
91
+ /**
92
+ * Add another money value (must be same currency)
93
+ */
94
+ add(other: IMoney): Money;
95
+ /**
96
+ * Subtract another money value (must be same currency)
97
+ */
98
+ subtract(other: IMoney): Money;
99
+ /**
100
+ * Multiply by a factor (rounds to nearest cent)
101
+ */
102
+ multiply(factor: number): Money;
103
+ /**
104
+ * Divide by a divisor (rounds to nearest cent)
105
+ */
106
+ divide(divisor: number): Money;
107
+ /**
108
+ * Distribute money evenly without losing cents
109
+ * The remainder is distributed to the first parts
110
+ * @example $100 / 3 = [$33.34, $33.33, $33.33]
111
+ */
112
+ distribute(parts: number): Money[];
113
+ /**
114
+ * Distribute money according to ratios
115
+ * @example $100 with ratios [1, 2, 2] = [$20, $40, $40]
116
+ */
117
+ distributeByRatios(ratios: number[]): Money[];
118
+ /**
119
+ * Format money for display
120
+ */
121
+ format(options?: FormatOptions): string;
122
+ /**
123
+ * Get the decimal representation
124
+ */
125
+ toDecimal(): number;
126
+ /**
127
+ * Check equality with another money value
128
+ */
129
+ equals(other: IMoney): boolean;
130
+ /**
131
+ * Check if greater than another money value
132
+ */
133
+ greaterThan(other: IMoney): boolean;
134
+ /**
135
+ * Check if less than another money value
136
+ */
137
+ lessThan(other: IMoney): boolean;
138
+ /**
139
+ * Check if greater than or equal
140
+ */
141
+ greaterThanOrEqual(other: IMoney): boolean;
142
+ /**
143
+ * Check if less than or equal
144
+ */
145
+ lessThanOrEqual(other: IMoney): boolean;
146
+ /**
147
+ * Check if zero
148
+ */
149
+ isZero(): boolean;
150
+ /**
151
+ * Check if positive
152
+ */
153
+ isPositive(): boolean;
154
+ /**
155
+ * Check if negative
156
+ */
157
+ isNegative(): boolean;
158
+ /**
159
+ * Get absolute value
160
+ */
161
+ abs(): Money;
162
+ /**
163
+ * Negate the value
164
+ */
165
+ negate(): Money;
166
+ /**
167
+ * Convert to JSON-serializable object
168
+ */
169
+ toJSON(): {
170
+ cents: number;
171
+ currency: string;
172
+ };
173
+ /**
174
+ * String representation
175
+ */
176
+ toString(): string;
177
+ private formatInteger;
178
+ private assertSameCurrency;
179
+ }
180
+
181
+ export { type Currency, type DistributionResult, type FormatOptions, type IMoney, Money };
@@ -0,0 +1,242 @@
1
+ // src/core/money.ts
2
+ var Money = class _Money {
3
+ constructor(cents, currency) {
4
+ if (!Number.isInteger(cents)) {
5
+ throw new Error("Money must be created with an integer number of cents");
6
+ }
7
+ this.cents = cents;
8
+ this.currency = currency;
9
+ Object.freeze(this);
10
+ }
11
+ /**
12
+ * Create money from cents (smallest unit)
13
+ */
14
+ static fromCents(cents, currency) {
15
+ return new _Money(Math.round(cents), currency);
16
+ }
17
+ /**
18
+ * Create money from a decimal amount
19
+ * @example Money.fromDecimal(100.50, USD) creates $100.50
20
+ */
21
+ static fromDecimal(amount, currency) {
22
+ const multiplier = Math.pow(10, currency.decimals);
23
+ const cents = Math.round(amount * multiplier);
24
+ return new _Money(cents, currency);
25
+ }
26
+ /**
27
+ * Create zero money
28
+ */
29
+ static zero(currency) {
30
+ return new _Money(0, currency);
31
+ }
32
+ /**
33
+ * Add another money value (must be same currency)
34
+ */
35
+ add(other) {
36
+ this.assertSameCurrency(other);
37
+ return new _Money(this.cents + other.cents, this.currency);
38
+ }
39
+ /**
40
+ * Subtract another money value (must be same currency)
41
+ */
42
+ subtract(other) {
43
+ this.assertSameCurrency(other);
44
+ return new _Money(this.cents - other.cents, this.currency);
45
+ }
46
+ /**
47
+ * Multiply by a factor (rounds to nearest cent)
48
+ */
49
+ multiply(factor) {
50
+ return new _Money(Math.round(this.cents * factor), this.currency);
51
+ }
52
+ /**
53
+ * Divide by a divisor (rounds to nearest cent)
54
+ */
55
+ divide(divisor) {
56
+ if (divisor === 0) {
57
+ throw new Error("Cannot divide by zero");
58
+ }
59
+ return new _Money(Math.round(this.cents / divisor), this.currency);
60
+ }
61
+ /**
62
+ * Distribute money evenly without losing cents
63
+ * The remainder is distributed to the first parts
64
+ * @example $100 / 3 = [$33.34, $33.33, $33.33]
65
+ */
66
+ distribute(parts) {
67
+ if (parts <= 0 || !Number.isInteger(parts)) {
68
+ throw new Error("Parts must be a positive integer");
69
+ }
70
+ const quotient = Math.floor(this.cents / parts);
71
+ const remainder = this.cents % parts;
72
+ const result = [];
73
+ for (let i = 0; i < parts; i++) {
74
+ const extra = i < remainder ? 1 : 0;
75
+ result.push(new _Money(quotient + extra, this.currency));
76
+ }
77
+ return result;
78
+ }
79
+ /**
80
+ * Distribute money according to ratios
81
+ * @example $100 with ratios [1, 2, 2] = [$20, $40, $40]
82
+ */
83
+ distributeByRatios(ratios) {
84
+ if (ratios.length === 0) {
85
+ throw new Error("Ratios array cannot be empty");
86
+ }
87
+ const total = ratios.reduce((sum, r) => sum + r, 0);
88
+ if (total <= 0) {
89
+ throw new Error("Sum of ratios must be positive");
90
+ }
91
+ let remaining = this.cents;
92
+ const result = [];
93
+ for (let i = 0; i < ratios.length; i++) {
94
+ if (i === ratios.length - 1) {
95
+ result.push(new _Money(remaining, this.currency));
96
+ } else {
97
+ const share = Math.round(this.cents * ratios[i] / total);
98
+ result.push(new _Money(share, this.currency));
99
+ remaining -= share;
100
+ }
101
+ }
102
+ return result;
103
+ }
104
+ /**
105
+ * Format money for display
106
+ */
107
+ format(options = {}) {
108
+ const { showSymbol = true, showDecimals = true, symbolPosition } = options;
109
+ const decimal = this.toDecimal();
110
+ const absValue = Math.abs(decimal);
111
+ const isNegative = decimal < 0;
112
+ let formatted;
113
+ if (showDecimals && this.currency.decimals > 0) {
114
+ const [intPart, decPart] = absValue.toFixed(this.currency.decimals).split(".");
115
+ const intFormatted = this.formatInteger(intPart);
116
+ formatted = `${intFormatted}${this.currency.decimalSeparator}${decPart}`;
117
+ } else {
118
+ formatted = this.formatInteger(Math.round(absValue).toString());
119
+ }
120
+ if (isNegative) {
121
+ formatted = `-${formatted}`;
122
+ }
123
+ if (showSymbol) {
124
+ const pos = symbolPosition ?? this.currency.symbolPosition;
125
+ const space = this.currency.symbolSpacing ? " " : "";
126
+ if (pos === "before") {
127
+ formatted = `${this.currency.symbol}${space}${formatted}`;
128
+ } else {
129
+ formatted = `${formatted}${space}${this.currency.symbol}`;
130
+ }
131
+ }
132
+ return formatted;
133
+ }
134
+ /**
135
+ * Get the decimal representation
136
+ */
137
+ toDecimal() {
138
+ return this.cents / Math.pow(10, this.currency.decimals);
139
+ }
140
+ /**
141
+ * Check equality with another money value
142
+ */
143
+ equals(other) {
144
+ return this.cents === other.cents && this.currency.code === other.currency.code;
145
+ }
146
+ /**
147
+ * Check if greater than another money value
148
+ */
149
+ greaterThan(other) {
150
+ this.assertSameCurrency(other);
151
+ return this.cents > other.cents;
152
+ }
153
+ /**
154
+ * Check if less than another money value
155
+ */
156
+ lessThan(other) {
157
+ this.assertSameCurrency(other);
158
+ return this.cents < other.cents;
159
+ }
160
+ /**
161
+ * Check if greater than or equal
162
+ */
163
+ greaterThanOrEqual(other) {
164
+ this.assertSameCurrency(other);
165
+ return this.cents >= other.cents;
166
+ }
167
+ /**
168
+ * Check if less than or equal
169
+ */
170
+ lessThanOrEqual(other) {
171
+ this.assertSameCurrency(other);
172
+ return this.cents <= other.cents;
173
+ }
174
+ /**
175
+ * Check if zero
176
+ */
177
+ isZero() {
178
+ return this.cents === 0;
179
+ }
180
+ /**
181
+ * Check if positive
182
+ */
183
+ isPositive() {
184
+ return this.cents > 0;
185
+ }
186
+ /**
187
+ * Check if negative
188
+ */
189
+ isNegative() {
190
+ return this.cents < 0;
191
+ }
192
+ /**
193
+ * Get absolute value
194
+ */
195
+ abs() {
196
+ return new _Money(Math.abs(this.cents), this.currency);
197
+ }
198
+ /**
199
+ * Negate the value
200
+ */
201
+ negate() {
202
+ return new _Money(-this.cents, this.currency);
203
+ }
204
+ /**
205
+ * Convert to JSON-serializable object
206
+ */
207
+ toJSON() {
208
+ return {
209
+ cents: this.cents,
210
+ currency: this.currency.code
211
+ };
212
+ }
213
+ /**
214
+ * String representation
215
+ */
216
+ toString() {
217
+ return this.format();
218
+ }
219
+ formatInteger(intStr) {
220
+ const parts = [];
221
+ let remaining = intStr;
222
+ while (remaining.length > 3) {
223
+ parts.unshift(remaining.slice(-3));
224
+ remaining = remaining.slice(0, -3);
225
+ }
226
+ if (remaining) {
227
+ parts.unshift(remaining);
228
+ }
229
+ return parts.join(this.currency.thousandsSeparator);
230
+ }
231
+ assertSameCurrency(other) {
232
+ if (this.currency.code !== other.currency.code) {
233
+ throw new Error(
234
+ `Cannot perform operation with different currencies: ${this.currency.code} and ${other.currency.code}`
235
+ );
236
+ }
237
+ }
238
+ };
239
+
240
+ export { Money };
241
+ //# sourceMappingURL=index.js.map
242
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/core/money.ts"],"names":[],"mappings":";AAKO,IAAM,KAAA,GAAN,MAAM,MAAA,CAAwB;AAAA,EAI3B,WAAA,CAAY,OAAe,QAAA,EAAoB;AACrD,IAAA,IAAI,CAAC,MAAA,CAAO,SAAA,CAAU,KAAK,CAAA,EAAG;AAC5B,MAAA,MAAM,IAAI,MAAM,uDAAuD,CAAA;AAAA,IACzE;AACA,IAAA,IAAA,CAAK,KAAA,GAAQ,KAAA;AACb,IAAA,IAAA,CAAK,QAAA,GAAW,QAAA;AAChB,IAAA,MAAA,CAAO,OAAO,IAA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,MAAA,CAAM,CAAC,IAAA,CAAK,KAAA,EAAO,KAAK,QAAQ,CAAA;AAAA,EAC7C;AAAA;AAAA;AAAA;AAAA,EAKA,MAAA,GAA8C;AAC5C,IAAA,OAAO;AAAA,MACL,OAAO,IAAA,CAAK,KAAA;AAAA,MACZ,QAAA,EAAU,KAAK,QAAA,CAAS;AAAA,KAC1B;AAAA,EACF;AAAA;AAAA;AAAA;AAAA,EAKA,QAAA,GAAmB;AACjB,IAAA,OAAO,KAAK,MAAA,EAAO;AAAA,EACrB;AAAA,EAEQ,cAAc,MAAA,EAAwB;AAC5C,IAAA,MAAM,QAAkB,EAAC;AACzB,IAAA,IAAI,SAAA,GAAY,MAAA;AAEhB,IAAA,OAAO,SAAA,CAAU,SAAS,CAAA,EAAG;AAC3B,MAAA,KAAA,CAAM,OAAA,CAAQ,SAAA,CAAU,KAAA,CAAM,EAAE,CAAC,CAAA;AACjC,MAAA,SAAA,GAAY,SAAA,CAAU,KAAA,CAAM,CAAA,EAAG,EAAE,CAAA;AAAA,IACnC;AAEA,IAAA,IAAI,SAAA,EAAW;AACb,MAAA,KAAA,CAAM,QAAQ,SAAS,CAAA;AAAA,IACzB;AAEA,IAAA,OAAO,KAAA,CAAM,IAAA,CAAK,IAAA,CAAK,QAAA,CAAS,kBAAkB,CAAA;AAAA,EACpD;AAAA,EAEQ,mBAAmB,KAAA,EAAqB;AAC9C,IAAA,IAAI,IAAA,CAAK,QAAA,CAAS,IAAA,KAAS,KAAA,CAAM,SAAS,IAAA,EAAM;AAC9C,MAAA,MAAM,IAAI,KAAA;AAAA,QACR,uDAAuD,IAAA,CAAK,QAAA,CAAS,IAAI,CAAA,KAAA,EAAQ,KAAA,CAAM,SAAS,IAAI,CAAA;AAAA,OACtG;AAAA,IACF;AAAA,EACF;AACF","file":"index.js","sourcesContent":["import type { Currency, FormatOptions, IMoney } from './types.js';\n\n/**\n * Immutable Money class that uses integer arithmetic for precision\n */\nexport class Money implements IMoney {\n readonly cents: number;\n readonly currency: Currency;\n\n private constructor(cents: number, currency: Currency) {\n if (!Number.isInteger(cents)) {\n throw new Error('Money must be created with an integer number of cents');\n }\n this.cents = cents;\n this.currency = currency;\n Object.freeze(this);\n }\n\n /**\n * Create money from cents (smallest unit)\n */\n static fromCents(cents: number, currency: Currency): Money {\n return new Money(Math.round(cents), currency);\n }\n\n /**\n * Create money from a decimal amount\n * @example Money.fromDecimal(100.50, USD) creates $100.50\n */\n static fromDecimal(amount: number, currency: Currency): Money {\n const multiplier = Math.pow(10, currency.decimals);\n const cents = Math.round(amount * multiplier);\n return new Money(cents, currency);\n }\n\n /**\n * Create zero money\n */\n static zero(currency: Currency): Money {\n return new Money(0, currency);\n }\n\n /**\n * Add another money value (must be same currency)\n */\n add(other: IMoney): Money {\n this.assertSameCurrency(other);\n return new Money(this.cents + other.cents, this.currency);\n }\n\n /**\n * Subtract another money value (must be same currency)\n */\n subtract(other: IMoney): Money {\n this.assertSameCurrency(other);\n return new Money(this.cents - other.cents, this.currency);\n }\n\n /**\n * Multiply by a factor (rounds to nearest cent)\n */\n multiply(factor: number): Money {\n return new Money(Math.round(this.cents * factor), this.currency);\n }\n\n /**\n * Divide by a divisor (rounds to nearest cent)\n */\n divide(divisor: number): Money {\n if (divisor === 0) {\n throw new Error('Cannot divide by zero');\n }\n return new Money(Math.round(this.cents / divisor), this.currency);\n }\n\n /**\n * Distribute money evenly without losing cents\n * The remainder is distributed to the first parts\n * @example $100 / 3 = [$33.34, $33.33, $33.33]\n */\n distribute(parts: number): Money[] {\n if (parts <= 0 || !Number.isInteger(parts)) {\n throw new Error('Parts must be a positive integer');\n }\n\n const quotient = Math.floor(this.cents / parts);\n const remainder = this.cents % parts;\n\n const result: Money[] = [];\n for (let i = 0; i < parts; i++) {\n // Add 1 cent to the first 'remainder' parts\n const extra = i < remainder ? 1 : 0;\n result.push(new Money(quotient + extra, this.currency));\n }\n\n return result;\n }\n\n /**\n * Distribute money according to ratios\n * @example $100 with ratios [1, 2, 2] = [$20, $40, $40]\n */\n distributeByRatios(ratios: number[]): Money[] {\n if (ratios.length === 0) {\n throw new Error('Ratios array cannot be empty');\n }\n\n const total = ratios.reduce((sum, r) => sum + r, 0);\n if (total <= 0) {\n throw new Error('Sum of ratios must be positive');\n }\n\n let remaining = this.cents;\n const result: Money[] = [];\n\n for (let i = 0; i < ratios.length; i++) {\n if (i === ratios.length - 1) {\n // Last part gets whatever is remaining to avoid rounding errors\n result.push(new Money(remaining, this.currency));\n } else {\n const share = Math.round((this.cents * ratios[i]) / total);\n result.push(new Money(share, this.currency));\n remaining -= share;\n }\n }\n\n return result;\n }\n\n /**\n * Format money for display\n */\n format(options: FormatOptions = {}): string {\n const { showSymbol = true, showDecimals = true, symbolPosition } = options;\n\n const decimal = this.toDecimal();\n const absValue = Math.abs(decimal);\n const isNegative = decimal < 0;\n\n // Format the number\n let formatted: string;\n if (showDecimals && this.currency.decimals > 0) {\n const [intPart, decPart] = absValue.toFixed(this.currency.decimals).split('.');\n const intFormatted = this.formatInteger(intPart);\n formatted = `${intFormatted}${this.currency.decimalSeparator}${decPart}`;\n } else {\n formatted = this.formatInteger(Math.round(absValue).toString());\n }\n\n // Add negative sign\n if (isNegative) {\n formatted = `-${formatted}`;\n }\n\n // Add symbol\n if (showSymbol) {\n const pos = symbolPosition ?? this.currency.symbolPosition;\n const space = this.currency.symbolSpacing ? ' ' : '';\n\n if (pos === 'before') {\n formatted = `${this.currency.symbol}${space}${formatted}`;\n } else {\n formatted = `${formatted}${space}${this.currency.symbol}`;\n }\n }\n\n return formatted;\n }\n\n /**\n * Get the decimal representation\n */\n toDecimal(): number {\n return this.cents / Math.pow(10, this.currency.decimals);\n }\n\n /**\n * Check equality with another money value\n */\n equals(other: IMoney): boolean {\n return this.cents === other.cents && this.currency.code === other.currency.code;\n }\n\n /**\n * Check if greater than another money value\n */\n greaterThan(other: IMoney): boolean {\n this.assertSameCurrency(other);\n return this.cents > other.cents;\n }\n\n /**\n * Check if less than another money value\n */\n lessThan(other: IMoney): boolean {\n this.assertSameCurrency(other);\n return this.cents < other.cents;\n }\n\n /**\n * Check if greater than or equal\n */\n greaterThanOrEqual(other: IMoney): boolean {\n this.assertSameCurrency(other);\n return this.cents >= other.cents;\n }\n\n /**\n * Check if less than or equal\n */\n lessThanOrEqual(other: IMoney): boolean {\n this.assertSameCurrency(other);\n return this.cents <= other.cents;\n }\n\n /**\n * Check if zero\n */\n isZero(): boolean {\n return this.cents === 0;\n }\n\n /**\n * Check if positive\n */\n isPositive(): boolean {\n return this.cents > 0;\n }\n\n /**\n * Check if negative\n */\n isNegative(): boolean {\n return this.cents < 0;\n }\n\n /**\n * Get absolute value\n */\n abs(): Money {\n return new Money(Math.abs(this.cents), this.currency);\n }\n\n /**\n * Negate the value\n */\n negate(): Money {\n return new Money(-this.cents, this.currency);\n }\n\n /**\n * Convert to JSON-serializable object\n */\n toJSON(): { cents: number; currency: string } {\n return {\n cents: this.cents,\n currency: this.currency.code,\n };\n }\n\n /**\n * String representation\n */\n toString(): string {\n return this.format();\n }\n\n private formatInteger(intStr: string): string {\n const parts: string[] = [];\n let remaining = intStr;\n\n while (remaining.length > 3) {\n parts.unshift(remaining.slice(-3));\n remaining = remaining.slice(0, -3);\n }\n\n if (remaining) {\n parts.unshift(remaining);\n }\n\n return parts.join(this.currency.thousandsSeparator);\n }\n\n private assertSameCurrency(other: IMoney): void {\n if (this.currency.code !== other.currency.code) {\n throw new Error(\n `Cannot perform operation with different currencies: ${this.currency.code} and ${other.currency.code}`\n );\n }\n }\n}\n"]}