@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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 PuvaanRaaj
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,160 @@
1
+ # moneta
2
+
3
+ > Immutable, BigInt-precision monetary values for TypeScript. No float errors. Currency-safe by design.
4
+
5
+ [![CI](https://github.com/PuvaanRaaj/moneta/actions/workflows/ci.yml/badge.svg)](https://github.com/PuvaanRaaj/moneta/actions/workflows/ci.yml)
6
+ [![npm](https://img.shields.io/npm/v/moneta)](https://www.npmjs.com/package/moneta)
7
+ [![npm bundle size](https://img.shields.io/bundlephobia/minzip/moneta)](https://bundlephobia.com/package/moneta)
8
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](LICENSE)
9
+
10
+ ## Why moneta?
11
+
12
+ Every JavaScript app that handles money eventually hits the float trap:
13
+
14
+ ```js
15
+ 0.1 + 0.2 === 0.3 // false — 0.30000000000000004
16
+ ```
17
+
18
+ `moneta` solves this permanently using **native BigInt** under the hood — no floats, ever.
19
+ It also catches **currency mismatches at the TypeScript type level**, not at runtime.
20
+
21
+ ```ts
22
+ const usd = money('10.00', 'USD')
23
+ const eur = money('5.00', 'EUR')
24
+
25
+ usd.add(eur) // TypeError — caught at compile time, not in production
26
+ ```
27
+
28
+ ## Install
29
+
30
+ ```bash
31
+ npm install moneta
32
+ ```
33
+
34
+ Requires Node ≥ 18. Zero runtime dependencies.
35
+
36
+ ## Quick start
37
+
38
+ ```ts
39
+ import { money } from 'moneta'
40
+
41
+ const price = money('19.99', 'USD')
42
+ const tax = money('1.60', 'USD')
43
+
44
+ price.add(tax).format('en-US') // "$21.59"
45
+ price.multiply(1.1).format('en-US') // "$21.99"
46
+ price.divide(3).format('en-US') // "$6.66"
47
+
48
+ // Allocate $10 in 3 parts — sum always equals original
49
+ money('10.00', 'USD').allocate([1, 1, 1])
50
+ // [Money('3.34 USD'), Money('3.33 USD'), Money('3.33 USD')]
51
+ ```
52
+
53
+ ## API
54
+
55
+ ### `money(amount, currency)`
56
+
57
+ The primary factory. Returns an immutable `Money<C>` instance.
58
+
59
+ ```ts
60
+ money('19.99', 'USD') // from string — recommended
61
+ money(19.99, 'USD') // from number — uses toFixed() internally
62
+ money(1999n, 'USD') // from bigint — treated as minor units (cents)
63
+ ```
64
+
65
+ ### `Money.fromMinorUnits(units, currency)`
66
+
67
+ Create from raw minor units — useful when reading from a database.
68
+
69
+ ```ts
70
+ Money.fromMinorUnits(1999n, 'USD') // $19.99
71
+ ```
72
+
73
+ ### Arithmetic
74
+
75
+ All operations return a **new** `Money` instance (immutable).
76
+
77
+ | Method | Description |
78
+ |---|---|
79
+ | `.add(other)` | Add two same-currency values |
80
+ | `.subtract(other)` | Subtract two same-currency values |
81
+ | `.multiply(factor)` | Multiply by a scalar (number or string) |
82
+ | `.divide(divisor)` | Divide by a scalar, half-up rounding |
83
+ | `.allocate(ratios)` | Proportional split — sum always exact |
84
+ | `.abs()` | Absolute value |
85
+ | `.negate()` | Flip sign |
86
+
87
+ ### Comparison
88
+
89
+ ```ts
90
+ a.equals(b)
91
+ a.greaterThan(b)
92
+ a.lessThan(b)
93
+ a.greaterThanOrEqual(b)
94
+ a.lessThanOrEqual(b)
95
+ a.isZero()
96
+ a.isPositive()
97
+ a.isNegative()
98
+ ```
99
+
100
+ ### Accessors
101
+
102
+ ```ts
103
+ m.currency // 'USD'
104
+ m.minorUnits // 1999n — safe for DB storage
105
+ m.decimalValue // '19.99'
106
+ ```
107
+
108
+ ### Formatting
109
+
110
+ ```ts
111
+ m.format() // uses default locale
112
+ m.format('de-DE') // "19,99 $"
113
+ m.format('ja-JP') // "$19.99"
114
+ ```
115
+
116
+ ### Serialisation
117
+
118
+ ```ts
119
+ m.toJSON()
120
+ // { amount: '19.99', currency: 'USD', minorUnits: '1999' }
121
+
122
+ JSON.stringify(m)
123
+ // '{"amount":"19.99","currency":"USD","minorUnits":"1999"}'
124
+
125
+ m.toString() // '19.99 USD'
126
+ ```
127
+
128
+ ## Supported currencies
129
+
130
+ 34 ISO 4217 currencies included: AED, AUD, BRL, CAD, CHF, CNY, CZK, DKK, EUR, GBP, HKD, HUF, IDR, ILS, INR, JPY, KRW, KWD, MXN, MYR, NOK, NZD, PHP, PLN, RON, SAR, SEK, SGD, THB, TRY, TWD, USD, VND, ZAR.
131
+
132
+ ## Error handling
133
+
134
+ All errors extend `MonetaError` and are typed:
135
+
136
+ ```ts
137
+ import {
138
+ CurrencyMismatchError,
139
+ DivisionByZeroError,
140
+ InvalidAmountError,
141
+ UnknownCurrencyError,
142
+ } from 'moneta'
143
+ ```
144
+
145
+ ## Security
146
+
147
+ - Zero runtime dependencies
148
+ - No `eval`, no dynamic code execution
149
+ - No install scripts — safe against supply-chain attacks
150
+ - Every npm release is published with **provenance** (SLSA Level 2)
151
+
152
+ ```bash
153
+ npm audit signatures # verify the package wasn't tampered with
154
+ ```
155
+
156
+ See [SECURITY.md](SECURITY.md) for the full policy and how to report vulnerabilities.
157
+
158
+ ## License
159
+
160
+ [MIT](LICENSE)
package/dist/index.cjs ADDED
@@ -0,0 +1,314 @@
1
+ 'use strict';
2
+
3
+ // src/currencies.ts
4
+ var CURRENCIES = {
5
+ AED: { code: "AED", decimals: 2, name: "UAE Dirham" },
6
+ AUD: { code: "AUD", decimals: 2, name: "Australian Dollar" },
7
+ BRL: { code: "BRL", decimals: 2, name: "Brazilian Real" },
8
+ CAD: { code: "CAD", decimals: 2, name: "Canadian Dollar" },
9
+ CHF: { code: "CHF", decimals: 2, name: "Swiss Franc" },
10
+ CNY: { code: "CNY", decimals: 2, name: "Chinese Yuan" },
11
+ CZK: { code: "CZK", decimals: 2, name: "Czech Koruna" },
12
+ DKK: { code: "DKK", decimals: 2, name: "Danish Krone" },
13
+ EUR: { code: "EUR", decimals: 2, name: "Euro" },
14
+ GBP: { code: "GBP", decimals: 2, name: "British Pound" },
15
+ HKD: { code: "HKD", decimals: 2, name: "Hong Kong Dollar" },
16
+ HUF: { code: "HUF", decimals: 2, name: "Hungarian Forint" },
17
+ IDR: { code: "IDR", decimals: 2, name: "Indonesian Rupiah" },
18
+ ILS: { code: "ILS", decimals: 2, name: "Israeli New Shekel" },
19
+ INR: { code: "INR", decimals: 2, name: "Indian Rupee" },
20
+ JPY: { code: "JPY", decimals: 0, name: "Japanese Yen" },
21
+ KRW: { code: "KRW", decimals: 0, name: "South Korean Won" },
22
+ KWD: { code: "KWD", decimals: 3, name: "Kuwaiti Dinar" },
23
+ MXN: { code: "MXN", decimals: 2, name: "Mexican Peso" },
24
+ MYR: { code: "MYR", decimals: 2, name: "Malaysian Ringgit" },
25
+ NOK: { code: "NOK", decimals: 2, name: "Norwegian Krone" },
26
+ NZD: { code: "NZD", decimals: 2, name: "New Zealand Dollar" },
27
+ PHP: { code: "PHP", decimals: 2, name: "Philippine Peso" },
28
+ PLN: { code: "PLN", decimals: 2, name: "Polish Zloty" },
29
+ RON: { code: "RON", decimals: 2, name: "Romanian Leu" },
30
+ SAR: { code: "SAR", decimals: 2, name: "Saudi Riyal" },
31
+ SEK: { code: "SEK", decimals: 2, name: "Swedish Krona" },
32
+ SGD: { code: "SGD", decimals: 2, name: "Singapore Dollar" },
33
+ THB: { code: "THB", decimals: 2, name: "Thai Baht" },
34
+ TRY: { code: "TRY", decimals: 2, name: "Turkish Lira" },
35
+ TWD: { code: "TWD", decimals: 2, name: "New Taiwan Dollar" },
36
+ USD: { code: "USD", decimals: 2, name: "US Dollar" },
37
+ VND: { code: "VND", decimals: 0, name: "Vietnamese Dong" },
38
+ ZAR: { code: "ZAR", decimals: 2, name: "South African Rand" }
39
+ };
40
+
41
+ // src/errors.ts
42
+ var MonetaError = class extends Error {
43
+ constructor(message) {
44
+ super(message);
45
+ this.name = "MonetaError";
46
+ Object.setPrototypeOf(this, new.target.prototype);
47
+ }
48
+ };
49
+ var CurrencyMismatchError = class extends MonetaError {
50
+ expected;
51
+ received;
52
+ constructor(expected, received) {
53
+ super(
54
+ `Currency mismatch: cannot operate on ${expected} and ${received}. Convert to a common currency first.`
55
+ );
56
+ this.name = "CurrencyMismatchError";
57
+ this.expected = expected;
58
+ this.received = received;
59
+ }
60
+ };
61
+ var InvalidAmountError = class extends MonetaError {
62
+ input;
63
+ constructor(input) {
64
+ super(
65
+ `Invalid amount: "${String(input)}". Expected a numeric string (e.g. "19.99"), a number, or a bigint.`
66
+ );
67
+ this.name = "InvalidAmountError";
68
+ this.input = input;
69
+ }
70
+ };
71
+ var DivisionByZeroError = class extends MonetaError {
72
+ constructor() {
73
+ super("Division by zero is not allowed.");
74
+ this.name = "DivisionByZeroError";
75
+ }
76
+ };
77
+ var UnknownCurrencyError = class extends MonetaError {
78
+ currency;
79
+ constructor(currency) {
80
+ super(
81
+ `Unknown currency code: "${currency}". See the CurrencyCode type for supported currencies.`
82
+ );
83
+ this.name = "UnknownCurrencyError";
84
+ this.currency = currency;
85
+ }
86
+ };
87
+
88
+ // src/money.ts
89
+ var AMOUNT_PATTERN = /^-?\d+(\.\d+)?$/;
90
+ function parseToMinorUnits(input, decimals) {
91
+ if (typeof input === "bigint") return input;
92
+ const str = typeof input === "number" ? input.toFixed(decimals) : input.trim();
93
+ if (!AMOUNT_PATTERN.test(str)) {
94
+ throw new InvalidAmountError(str);
95
+ }
96
+ const negative = str.startsWith("-");
97
+ const abs = negative ? str.slice(1) : str;
98
+ const dotIdx = abs.indexOf(".");
99
+ let whole;
100
+ let fraction;
101
+ if (dotIdx === -1) {
102
+ whole = abs;
103
+ fraction = "0".repeat(decimals);
104
+ } else {
105
+ whole = abs.slice(0, dotIdx);
106
+ fraction = abs.slice(dotIdx + 1).padEnd(decimals, "0").slice(0, decimals);
107
+ }
108
+ const scale = 10n ** BigInt(decimals);
109
+ const result = BigInt(whole) * scale + BigInt(fraction);
110
+ return negative ? -result : result;
111
+ }
112
+ function minorUnitsToString(units, decimals) {
113
+ const negative = units < 0n;
114
+ const abs = negative ? -units : units;
115
+ const scale = 10n ** BigInt(decimals);
116
+ const whole = abs / scale;
117
+ const fraction = abs % scale;
118
+ const fractionStr = decimals > 0 ? `.${fraction.toString().padStart(decimals, "0")}` : "";
119
+ return `${negative ? "-" : ""}${whole}${fractionStr}`;
120
+ }
121
+ function roundDiv(numerator, denominator) {
122
+ if (denominator === 0n) throw new DivisionByZeroError();
123
+ const absN = numerator < 0n ? -numerator : numerator;
124
+ const absD = denominator < 0n ? -denominator : denominator;
125
+ const rounded = (absN + absD / 2n) / absD;
126
+ const negative = numerator < 0n !== denominator < 0n;
127
+ return negative ? -rounded : rounded;
128
+ }
129
+ function parseScalar(input) {
130
+ const f = typeof input === "number" ? input.toString() : input.trim();
131
+ if (!AMOUNT_PATTERN.test(f)) throw new InvalidAmountError(f);
132
+ const negative = f.startsWith("-");
133
+ const abs = negative ? f.slice(1) : f;
134
+ const dotIdx = abs.indexOf(".");
135
+ const fracLen = dotIdx === -1 ? 0 : abs.length - dotIdx - 1;
136
+ const precision = 10n ** BigInt(fracLen);
137
+ const parts = abs.split(".");
138
+ const whole = parts[0] ?? "";
139
+ const frac = parts[1] ?? "";
140
+ const unsigned = BigInt(whole) * precision + BigInt(frac.padEnd(fracLen, "0"));
141
+ return { factorBig: negative ? -unsigned : unsigned, precision };
142
+ }
143
+ var Money = class _Money {
144
+ #minorUnits;
145
+ #currency;
146
+ #decimals;
147
+ constructor(amount, currency) {
148
+ if (!(currency in CURRENCIES)) {
149
+ throw new UnknownCurrencyError(String(currency));
150
+ }
151
+ this.#currency = currency;
152
+ this.#decimals = CURRENCIES[currency].decimals;
153
+ this.#minorUnits = parseToMinorUnits(amount, this.#decimals);
154
+ }
155
+ // ─── Static factories ──────────────────────────────────────────────────────
156
+ /** Create from raw minor units (e.g. cents). Useful when reading from a database. */
157
+ static fromMinorUnits(units, currency) {
158
+ const decimals = CURRENCIES[currency].decimals;
159
+ return new _Money(minorUnitsToString(units, decimals), currency);
160
+ }
161
+ // ─── Accessors ─────────────────────────────────────────────────────────────
162
+ /** Raw amount in minor units (e.g. cents). Safe for storage and wire transfer. */
163
+ get minorUnits() {
164
+ return this.#minorUnits;
165
+ }
166
+ get currency() {
167
+ return this.#currency;
168
+ }
169
+ /** Human-readable decimal string, e.g. "19.99". */
170
+ get decimalValue() {
171
+ return minorUnitsToString(this.#minorUnits, this.#decimals);
172
+ }
173
+ // ─── Arithmetic ────────────────────────────────────────────────────────────
174
+ add(other) {
175
+ this.#assertSameCurrency(other);
176
+ return this.#fromUnits(this.#minorUnits + other.#minorUnits);
177
+ }
178
+ subtract(other) {
179
+ this.#assertSameCurrency(other);
180
+ return this.#fromUnits(this.#minorUnits - other.#minorUnits);
181
+ }
182
+ /**
183
+ * Multiply by a scalar. Uses half-up rounding to the currency's minor unit.
184
+ * @example money('10.00', 'USD').multiply(1.1) // $11.00
185
+ * @example money('10.00', 'USD').multiply('1.5') // $15.00
186
+ */
187
+ multiply(factor) {
188
+ const { factorBig, precision } = parseScalar(factor);
189
+ return this.#fromUnits(roundDiv(this.#minorUnits * factorBig, precision));
190
+ }
191
+ /**
192
+ * Divide by a scalar. Uses half-up rounding to the currency's minor unit.
193
+ * @example money('10.00', 'USD').divide(3) // $3.33
194
+ */
195
+ divide(divisor) {
196
+ const { factorBig, precision } = parseScalar(divisor);
197
+ if (factorBig === 0n) throw new DivisionByZeroError();
198
+ return this.#fromUnits(roundDiv(this.#minorUnits * precision, factorBig));
199
+ }
200
+ /**
201
+ * Allocate proportionally. The sum of all parts always equals the original —
202
+ * any rounding remainder is distributed to the first buckets.
203
+ *
204
+ * @example
205
+ * money('10.00', 'USD').allocate([1, 1, 1])
206
+ * // [Money('3.34'), Money('3.33'), Money('3.33')]
207
+ */
208
+ allocate(ratios) {
209
+ if (ratios.length === 0) throw new InvalidAmountError("ratios array must not be empty");
210
+ const total = ratios.reduce((a, b) => a + b, 0);
211
+ if (total <= 0) throw new InvalidAmountError("ratios must sum to a positive number");
212
+ const PRECISION = 1000000n;
213
+ const totalBig = BigInt(Math.round(total * Number(PRECISION)));
214
+ const shares = ratios.map((r) => {
215
+ const rBig = BigInt(Math.round(r * Number(PRECISION)));
216
+ return roundDiv(this.#minorUnits * rBig, totalBig);
217
+ });
218
+ let remainder = this.#minorUnits - shares.reduce((a, b) => a + b, 0n);
219
+ const direction = remainder > 0n ? 1n : -1n;
220
+ for (let i = 0; remainder !== 0n; i++) {
221
+ const idx = i % shares.length;
222
+ shares[idx] = (shares[idx] ?? 0n) + direction;
223
+ remainder -= direction;
224
+ }
225
+ return shares.map((u) => this.#fromUnits(u));
226
+ }
227
+ // ─── Comparison ────────────────────────────────────────────────────────────
228
+ equals(other) {
229
+ this.#assertSameCurrency(other);
230
+ return this.#minorUnits === other.#minorUnits;
231
+ }
232
+ greaterThan(other) {
233
+ this.#assertSameCurrency(other);
234
+ return this.#minorUnits > other.#minorUnits;
235
+ }
236
+ lessThan(other) {
237
+ this.#assertSameCurrency(other);
238
+ return this.#minorUnits < other.#minorUnits;
239
+ }
240
+ greaterThanOrEqual(other) {
241
+ return !this.lessThan(other);
242
+ }
243
+ lessThanOrEqual(other) {
244
+ return !this.greaterThan(other);
245
+ }
246
+ isZero() {
247
+ return this.#minorUnits === 0n;
248
+ }
249
+ isPositive() {
250
+ return this.#minorUnits > 0n;
251
+ }
252
+ isNegative() {
253
+ return this.#minorUnits < 0n;
254
+ }
255
+ abs() {
256
+ return this.#fromUnits(this.#minorUnits < 0n ? -this.#minorUnits : this.#minorUnits);
257
+ }
258
+ negate() {
259
+ return this.#fromUnits(-this.#minorUnits);
260
+ }
261
+ // ─── Formatting ────────────────────────────────────────────────────────────
262
+ /**
263
+ * Format using the platform's `Intl.NumberFormat` — no custom format strings.
264
+ *
265
+ * @example
266
+ * money('19.99', 'USD').format() // "$19.99"
267
+ * money('19.99', 'EUR').format('de-DE') // "19,99 €"
268
+ */
269
+ format(locale) {
270
+ return new Intl.NumberFormat(locale, {
271
+ style: "currency",
272
+ currency: this.#currency,
273
+ minimumFractionDigits: this.#decimals,
274
+ maximumFractionDigits: this.#decimals
275
+ }).format(Number(this.decimalValue));
276
+ }
277
+ // ─── Serialisation ─────────────────────────────────────────────────────────
278
+ /** Safe to store in JSON — no float representation. */
279
+ toJSON() {
280
+ return {
281
+ amount: this.decimalValue,
282
+ currency: this.#currency,
283
+ minorUnits: this.#minorUnits.toString()
284
+ };
285
+ }
286
+ toString() {
287
+ return `${this.decimalValue} ${this.#currency}`;
288
+ }
289
+ // ─── Private helpers ───────────────────────────────────────────────────────
290
+ #fromUnits(units) {
291
+ return new _Money(minorUnitsToString(units, this.#decimals), this.#currency);
292
+ }
293
+ #assertSameCurrency(other) {
294
+ if (this.#currency !== other.#currency) {
295
+ throw new CurrencyMismatchError(this.#currency, other.#currency);
296
+ }
297
+ }
298
+ };
299
+
300
+ // src/index.ts
301
+ function money(amount, currency) {
302
+ return new Money(amount, currency);
303
+ }
304
+
305
+ exports.CURRENCIES = CURRENCIES;
306
+ exports.CurrencyMismatchError = CurrencyMismatchError;
307
+ exports.DivisionByZeroError = DivisionByZeroError;
308
+ exports.InvalidAmountError = InvalidAmountError;
309
+ exports.MonetaError = MonetaError;
310
+ exports.Money = Money;
311
+ exports.UnknownCurrencyError = UnknownCurrencyError;
312
+ exports.money = money;
313
+ //# sourceMappingURL=index.cjs.map
314
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/currencies.ts","../src/errors.ts","../src/money.ts","../src/index.ts"],"names":[],"mappings":";;;AAIO,IAAM,UAAA,GAAa;AAAA,EACxB,KAAK,EAAE,IAAA,EAAM,OAAO,QAAA,EAAU,CAAA,EAAG,MAAM,YAAA,EAAa;AAAA,EACpD,KAAK,EAAE,IAAA,EAAM,OAAO,QAAA,EAAU,CAAA,EAAG,MAAM,mBAAA,EAAoB;AAAA,EAC3D,KAAK,EAAE,IAAA,EAAM,OAAO,QAAA,EAAU,CAAA,EAAG,MAAM,gBAAA,EAAiB;AAAA,EACxD,KAAK,EAAE,IAAA,EAAM,OAAO,QAAA,EAAU,CAAA,EAAG,MAAM,iBAAA,EAAkB;AAAA,EACzD,KAAK,EAAE,IAAA,EAAM,OAAO,QAAA,EAAU,CAAA,EAAG,MAAM,aAAA,EAAc;AAAA,EACrD,KAAK,EAAE,IAAA,EAAM,OAAO,QAAA,EAAU,CAAA,EAAG,MAAM,cAAA,EAAe;AAAA,EACtD,KAAK,EAAE,IAAA,EAAM,OAAO,QAAA,EAAU,CAAA,EAAG,MAAM,cAAA,EAAe;AAAA,EACtD,KAAK,EAAE,IAAA,EAAM,OAAO,QAAA,EAAU,CAAA,EAAG,MAAM,cAAA,EAAe;AAAA,EACtD,KAAK,EAAE,IAAA,EAAM,OAAO,QAAA,EAAU,CAAA,EAAG,MAAM,MAAA,EAAO;AAAA,EAC9C,KAAK,EAAE,IAAA,EAAM,OAAO,QAAA,EAAU,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A,IAC3E;AAAA,EACF;AACF;;;ACrRO,SAAS,KAAA,CACd,QACA,QAAA,EACU;AACV,EAAA,OAAO,IAAI,KAAA,CAAM,MAAA,EAAQ,QAAQ,CAAA;AACnC","file":"index.cjs","sourcesContent":["/**\n * ISO 4217 currency definitions.\n * Each entry contains the decimal precision used for minor-unit storage.\n */\nexport const CURRENCIES = {\n AED: { code: 'AED', decimals: 2, name: 'UAE Dirham' },\n AUD: { code: 'AUD', decimals: 2, name: 'Australian Dollar' },\n BRL: { code: 'BRL', decimals: 2, name: 'Brazilian Real' },\n CAD: { code: 'CAD', decimals: 2, name: 'Canadian Dollar' },\n CHF: { code: 'CHF', decimals: 2, name: 'Swiss Franc' },\n CNY: { code: 'CNY', decimals: 2, name: 'Chinese Yuan' },\n CZK: { code: 'CZK', decimals: 2, name: 'Czech Koruna' },\n DKK: { code: 'DKK', decimals: 2, name: 'Danish Krone' },\n EUR: { code: 'EUR', decimals: 2, name: 'Euro' },\n GBP: { code: 'GBP', decimals: 2, name: 'British Pound' },\n HKD: { code: 'HKD', decimals: 2, name: 'Hong Kong Dollar' },\n HUF: { code: 'HUF', decimals: 2, name: 'Hungarian Forint' },\n IDR: { code: 'IDR', decimals: 2, name: 'Indonesian Rupiah' },\n ILS: { code: 'ILS', decimals: 2, name: 'Israeli New Shekel' },\n INR: { code: 'INR', decimals: 2, name: 'Indian Rupee' },\n JPY: { code: 'JPY', decimals: 0, name: 'Japanese Yen' },\n KRW: { code: 'KRW', decimals: 0, name: 'South Korean Won' },\n KWD: { code: 'KWD', decimals: 3, name: 'Kuwaiti Dinar' },\n MXN: { code: 'MXN', decimals: 2, name: 'Mexican Peso' },\n MYR: { code: 'MYR', decimals: 2, name: 'Malaysian Ringgit' },\n NOK: { code: 'NOK', decimals: 2, name: 'Norwegian Krone' },\n NZD: { code: 'NZD', decimals: 2, name: 'New Zealand Dollar' },\n PHP: { code: 'PHP', decimals: 2, name: 'Philippine Peso' },\n PLN: { code: 'PLN', decimals: 2, name: 'Polish Zloty' },\n RON: { code: 'RON', decimals: 2, name: 'Romanian Leu' },\n SAR: { code: 'SAR', decimals: 2, name: 'Saudi Riyal' },\n SEK: { code: 'SEK', decimals: 2, name: 'Swedish Krona' },\n SGD: { code: 'SGD', decimals: 2, name: 'Singapore Dollar' },\n THB: { code: 'THB', decimals: 2, name: 'Thai Baht' },\n TRY: { code: 'TRY', decimals: 2, name: 'Turkish Lira' },\n TWD: { code: 'TWD', decimals: 2, name: 'New Taiwan Dollar' },\n USD: { code: 'USD', decimals: 2, name: 'US Dollar' },\n VND: { code: 'VND', decimals: 0, name: 'Vietnamese Dong' },\n ZAR: { code: 'ZAR', decimals: 2, name: 'South African Rand' },\n} as const;\n\nexport type CurrencyCode = keyof typeof CURRENCIES;\nexport type CurrencyDef = (typeof CURRENCIES)[CurrencyCode];\n","export class MonetaError extends Error {\n constructor(message: string) {\n super(message);\n this.name = 'MonetaError';\n // Maintains proper prototype chain in transpiled ES5\n Object.setPrototypeOf(this, new.target.prototype);\n }\n}\n\nexport class CurrencyMismatchError extends MonetaError {\n readonly expected: string;\n readonly received: string;\n\n constructor(expected: string, received: string) {\n super(\n `Currency mismatch: cannot operate on ${expected} and ${received}. Convert to a common currency first.`,\n );\n this.name = 'CurrencyMismatchError';\n this.expected = expected;\n this.received = received;\n }\n}\n\nexport class InvalidAmountError extends MonetaError {\n readonly input: unknown;\n\n constructor(input: unknown) {\n super(\n `Invalid amount: \"${String(input)}\". Expected a numeric string (e.g. \"19.99\"), a number, or a bigint.`,\n );\n this.name = 'InvalidAmountError';\n this.input = input;\n }\n}\n\nexport class DivisionByZeroError extends MonetaError {\n constructor() {\n super('Division by zero is not allowed.');\n this.name = 'DivisionByZeroError';\n }\n}\n\nexport class UnknownCurrencyError extends MonetaError {\n readonly currency: string;\n\n constructor(currency: string) {\n super(\n `Unknown currency code: \"${currency}\". See the CurrencyCode type for supported currencies.`,\n );\n this.name = 'UnknownCurrencyError';\n this.currency = currency;\n }\n}\n","import { CURRENCIES, type CurrencyCode } from './currencies.js';\nimport {\n CurrencyMismatchError,\n DivisionByZeroError,\n InvalidAmountError,\n UnknownCurrencyError,\n} from './errors.js';\n\n// ─── Internal helpers ────────────────────────────────────────────────────────\n\nconst AMOUNT_PATTERN = /^-?\\d+(\\.\\d+)?$/;\n\n/**\n * Parse a decimal string or number into a BigInt in minor units.\n * \"19.99\" with decimals=2 → 1999n\n * \"-5.5\" with decimals=2 → -550n\n */\nfunction parseToMinorUnits(input: string | number | bigint, decimals: number): bigint {\n if (typeof input === 'bigint') return input;\n\n const str = typeof input === 'number' ? input.toFixed(decimals) : input.trim();\n\n if (!AMOUNT_PATTERN.test(str)) {\n throw new InvalidAmountError(str);\n }\n\n const negative = str.startsWith('-');\n const abs = negative ? str.slice(1) : str;\n const dotIdx = abs.indexOf('.');\n\n let whole: string;\n let fraction: string;\n\n if (dotIdx === -1) {\n whole = abs;\n fraction = '0'.repeat(decimals);\n } else {\n whole = abs.slice(0, dotIdx);\n fraction = abs\n .slice(dotIdx + 1)\n .padEnd(decimals, '0')\n .slice(0, decimals);\n }\n\n const scale = 10n ** BigInt(decimals);\n const result = BigInt(whole) * scale + BigInt(fraction);\n return negative ? -result : result;\n}\n\n/**\n * Convert minor units back to a decimal string for re-parsing.\n * 1999n with decimals=2 → \"19.99\"\n */\nfunction minorUnitsToString(units: bigint, decimals: number): string {\n const negative = units < 0n;\n const abs = negative ? -units : units;\n const scale = 10n ** BigInt(decimals);\n const whole = abs / scale;\n const fraction = abs % scale;\n const fractionStr = decimals > 0 ? `.${fraction.toString().padStart(decimals, '0')}` : '';\n return `${negative ? '-' : ''}${whole}${fractionStr}`;\n}\n\n/**\n * Half-up rounding division for BigInt: round(numerator / denominator).\n */\nfunction roundDiv(numerator: bigint, denominator: bigint): bigint {\n if (denominator === 0n) throw new DivisionByZeroError();\n const absN = numerator < 0n ? -numerator : numerator;\n const absD = denominator < 0n ? -denominator : denominator;\n const rounded = (absN + absD / 2n) / absD;\n const negative = numerator < 0n !== denominator < 0n;\n return negative ? -rounded : rounded;\n}\n\n/**\n * Parse a scalar factor string into (factorBig, precision) where\n * the true factor = factorBig / precision.\n */\nfunction parseScalar(input: number | string): { factorBig: bigint; precision: bigint } {\n const f = typeof input === 'number' ? input.toString() : input.trim();\n if (!AMOUNT_PATTERN.test(f)) throw new InvalidAmountError(f);\n const negative = f.startsWith('-');\n const abs = negative ? f.slice(1) : f;\n const dotIdx = abs.indexOf('.');\n const fracLen = dotIdx === -1 ? 0 : abs.length - dotIdx - 1;\n const precision = 10n ** BigInt(fracLen);\n const parts = abs.split('.');\n const whole = parts[0] ?? '';\n const frac = parts[1] ?? '';\n const unsigned = BigInt(whole) * precision + BigInt(frac.padEnd(fracLen, '0'));\n return { factorBig: negative ? -unsigned : unsigned, precision };\n}\n\n// ─── Money class ─────────────────────────────────────────────────────────────\n\n/**\n * Immutable, BigInt-precision monetary value.\n *\n * The generic parameter `C` brands the currency at the type level so\n * cross-currency arithmetic is a **compile-time error**.\n *\n * @example\n * const price = money('19.99', 'USD')\n * const tax = money('1.60', 'USD')\n * price.add(tax).format() // \"$21.59\"\n * price.add(money('5', 'EUR')) // TypeError: currency mismatch\n */\nexport class Money<C extends CurrencyCode = CurrencyCode> {\n readonly #minorUnits: bigint;\n readonly #currency: C;\n readonly #decimals: number;\n\n constructor(amount: string | number | bigint, currency: C) {\n if (!(currency in CURRENCIES)) {\n throw new UnknownCurrencyError(String(currency));\n }\n this.#currency = currency;\n this.#decimals = CURRENCIES[currency].decimals;\n this.#minorUnits = parseToMinorUnits(amount, this.#decimals);\n }\n\n // ─── Static factories ──────────────────────────────────────────────────────\n\n /** Create from raw minor units (e.g. cents). Useful when reading from a database. */\n static fromMinorUnits<C extends CurrencyCode>(units: bigint, currency: C): Money<C> {\n const decimals = CURRENCIES[currency].decimals;\n return new Money<C>(minorUnitsToString(units, decimals), currency);\n }\n\n // ─── Accessors ─────────────────────────────────────────────────────────────\n\n /** Raw amount in minor units (e.g. cents). Safe for storage and wire transfer. */\n get minorUnits(): bigint {\n return this.#minorUnits;\n }\n\n get currency(): C {\n return this.#currency;\n }\n\n /** Human-readable decimal string, e.g. \"19.99\". */\n get decimalValue(): string {\n return minorUnitsToString(this.#minorUnits, this.#decimals);\n }\n\n // ─── Arithmetic ────────────────────────────────────────────────────────────\n\n add(other: Money<C>): Money<C> {\n this.#assertSameCurrency(other);\n return this.#fromUnits(this.#minorUnits + other.#minorUnits);\n }\n\n subtract(other: Money<C>): Money<C> {\n this.#assertSameCurrency(other);\n return this.#fromUnits(this.#minorUnits - other.#minorUnits);\n }\n\n /**\n * Multiply by a scalar. Uses half-up rounding to the currency's minor unit.\n * @example money('10.00', 'USD').multiply(1.1) // $11.00\n * @example money('10.00', 'USD').multiply('1.5') // $15.00\n */\n multiply(factor: number | string): Money<C> {\n const { factorBig, precision } = parseScalar(factor);\n return this.#fromUnits(roundDiv(this.#minorUnits * factorBig, precision));\n }\n\n /**\n * Divide by a scalar. Uses half-up rounding to the currency's minor unit.\n * @example money('10.00', 'USD').divide(3) // $3.33\n */\n divide(divisor: number | string): Money<C> {\n const { factorBig, precision } = parseScalar(divisor);\n if (factorBig === 0n) throw new DivisionByZeroError();\n return this.#fromUnits(roundDiv(this.#minorUnits * precision, factorBig));\n }\n\n /**\n * Allocate proportionally. The sum of all parts always equals the original —\n * any rounding remainder is distributed to the first buckets.\n *\n * @example\n * money('10.00', 'USD').allocate([1, 1, 1])\n * // [Money('3.34'), Money('3.33'), Money('3.33')]\n */\n allocate(ratios: readonly number[]): Money<C>[] {\n if (ratios.length === 0) throw new InvalidAmountError('ratios array must not be empty');\n const total = ratios.reduce((a, b) => a + b, 0);\n if (total <= 0) throw new InvalidAmountError('ratios must sum to a positive number');\n\n // Use large integer precision to avoid float loss in ratio math\n const PRECISION = 1_000_000n;\n const totalBig = BigInt(Math.round(total * Number(PRECISION)));\n\n const shares = ratios.map((r) => {\n const rBig = BigInt(Math.round(r * Number(PRECISION)));\n return roundDiv(this.#minorUnits * rBig, totalBig);\n });\n\n // Distribute rounding remainder so that sum(shares) === this.#minorUnits\n let remainder = this.#minorUnits - shares.reduce((a, b) => a + b, 0n);\n const direction = remainder > 0n ? 1n : -1n;\n for (let i = 0; remainder !== 0n; i++) {\n const idx = i % shares.length;\n shares[idx] = (shares[idx] ?? 0n) + direction;\n remainder -= direction;\n }\n\n return shares.map((u) => this.#fromUnits(u));\n }\n\n // ─── Comparison ────────────────────────────────────────────────────────────\n\n equals(other: Money<C>): boolean {\n this.#assertSameCurrency(other);\n return this.#minorUnits === other.#minorUnits;\n }\n\n greaterThan(other: Money<C>): boolean {\n this.#assertSameCurrency(other);\n return this.#minorUnits > other.#minorUnits;\n }\n\n lessThan(other: Money<C>): boolean {\n this.#assertSameCurrency(other);\n return this.#minorUnits < other.#minorUnits;\n }\n\n greaterThanOrEqual(other: Money<C>): boolean {\n return !this.lessThan(other);\n }\n\n lessThanOrEqual(other: Money<C>): boolean {\n return !this.greaterThan(other);\n }\n\n isZero(): boolean {\n return this.#minorUnits === 0n;\n }\n\n isPositive(): boolean {\n return this.#minorUnits > 0n;\n }\n\n isNegative(): boolean {\n return this.#minorUnits < 0n;\n }\n\n abs(): Money<C> {\n return this.#fromUnits(this.#minorUnits < 0n ? -this.#minorUnits : this.#minorUnits);\n }\n\n negate(): Money<C> {\n return this.#fromUnits(-this.#minorUnits);\n }\n\n // ─── Formatting ────────────────────────────────────────────────────────────\n\n /**\n * Format using the platform's `Intl.NumberFormat` — no custom format strings.\n *\n * @example\n * money('19.99', 'USD').format() // \"$19.99\"\n * money('19.99', 'EUR').format('de-DE') // \"19,99 €\"\n */\n format(locale?: string): string {\n return new Intl.NumberFormat(locale, {\n style: 'currency',\n currency: this.#currency,\n minimumFractionDigits: this.#decimals,\n maximumFractionDigits: this.#decimals,\n }).format(Number(this.decimalValue));\n }\n\n // ─── Serialisation ─────────────────────────────────────────────────────────\n\n /** Safe to store in JSON — no float representation. */\n toJSON(): { amount: string; currency: C; minorUnits: string } {\n return {\n amount: this.decimalValue,\n currency: this.#currency,\n minorUnits: this.#minorUnits.toString(),\n };\n }\n\n toString(): string {\n return `${this.decimalValue} ${this.#currency}`;\n }\n\n // ─── Private helpers ───────────────────────────────────────────────────────\n\n #fromUnits(units: bigint): Money<C> {\n return new Money<C>(minorUnitsToString(units, this.#decimals), this.#currency);\n }\n\n #assertSameCurrency(other: Money<C>): void {\n if (this.#currency !== other.#currency) {\n throw new CurrencyMismatchError(this.#currency, other.#currency as string);\n }\n }\n}\n","export { Money } from './money.js';\nexport { CURRENCIES } from './currencies.js';\nexport type { CurrencyCode, CurrencyDef } from './currencies.js';\nexport {\n MonetaError,\n CurrencyMismatchError,\n DivisionByZeroError,\n InvalidAmountError,\n UnknownCurrencyError,\n} from './errors.js';\n\nimport type { CurrencyCode } from './currencies.js';\nimport { Money } from './money.js';\n\n/**\n * Create a Money value. This is the primary entry point.\n *\n * @example\n * import { money } from 'moneta'\n *\n * const price = money('19.99', 'USD')\n * const tax = money('1.60', 'USD')\n * price.add(tax).format() // \"$21.59\"\n */\nexport function money<C extends CurrencyCode>(\n amount: string | number | bigint,\n currency: C,\n): Money<C> {\n return new Money(amount, currency);\n}\n"]}