@puvaanraaj/moneta 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +160 -0
- package/dist/index.cjs +314 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +283 -0
- package/dist/index.d.ts +283 -0
- package/dist/index.js +305 -0
- package/dist/index.js.map +1 -0
- package/package.json +57 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,305 @@
|
|
|
1
|
+
// src/currencies.ts
|
|
2
|
+
var CURRENCIES = {
|
|
3
|
+
AED: { code: "AED", decimals: 2, name: "UAE Dirham" },
|
|
4
|
+
AUD: { code: "AUD", decimals: 2, name: "Australian Dollar" },
|
|
5
|
+
BRL: { code: "BRL", decimals: 2, name: "Brazilian Real" },
|
|
6
|
+
CAD: { code: "CAD", decimals: 2, name: "Canadian Dollar" },
|
|
7
|
+
CHF: { code: "CHF", decimals: 2, name: "Swiss Franc" },
|
|
8
|
+
CNY: { code: "CNY", decimals: 2, name: "Chinese Yuan" },
|
|
9
|
+
CZK: { code: "CZK", decimals: 2, name: "Czech Koruna" },
|
|
10
|
+
DKK: { code: "DKK", decimals: 2, name: "Danish Krone" },
|
|
11
|
+
EUR: { code: "EUR", decimals: 2, name: "Euro" },
|
|
12
|
+
GBP: { code: "GBP", decimals: 2, name: "British Pound" },
|
|
13
|
+
HKD: { code: "HKD", decimals: 2, name: "Hong Kong Dollar" },
|
|
14
|
+
HUF: { code: "HUF", decimals: 2, name: "Hungarian Forint" },
|
|
15
|
+
IDR: { code: "IDR", decimals: 2, name: "Indonesian Rupiah" },
|
|
16
|
+
ILS: { code: "ILS", decimals: 2, name: "Israeli New Shekel" },
|
|
17
|
+
INR: { code: "INR", decimals: 2, name: "Indian Rupee" },
|
|
18
|
+
JPY: { code: "JPY", decimals: 0, name: "Japanese Yen" },
|
|
19
|
+
KRW: { code: "KRW", decimals: 0, name: "South Korean Won" },
|
|
20
|
+
KWD: { code: "KWD", decimals: 3, name: "Kuwaiti Dinar" },
|
|
21
|
+
MXN: { code: "MXN", decimals: 2, name: "Mexican Peso" },
|
|
22
|
+
MYR: { code: "MYR", decimals: 2, name: "Malaysian Ringgit" },
|
|
23
|
+
NOK: { code: "NOK", decimals: 2, name: "Norwegian Krone" },
|
|
24
|
+
NZD: { code: "NZD", decimals: 2, name: "New Zealand Dollar" },
|
|
25
|
+
PHP: { code: "PHP", decimals: 2, name: "Philippine Peso" },
|
|
26
|
+
PLN: { code: "PLN", decimals: 2, name: "Polish Zloty" },
|
|
27
|
+
RON: { code: "RON", decimals: 2, name: "Romanian Leu" },
|
|
28
|
+
SAR: { code: "SAR", decimals: 2, name: "Saudi Riyal" },
|
|
29
|
+
SEK: { code: "SEK", decimals: 2, name: "Swedish Krona" },
|
|
30
|
+
SGD: { code: "SGD", decimals: 2, name: "Singapore Dollar" },
|
|
31
|
+
THB: { code: "THB", decimals: 2, name: "Thai Baht" },
|
|
32
|
+
TRY: { code: "TRY", decimals: 2, name: "Turkish Lira" },
|
|
33
|
+
TWD: { code: "TWD", decimals: 2, name: "New Taiwan Dollar" },
|
|
34
|
+
USD: { code: "USD", decimals: 2, name: "US Dollar" },
|
|
35
|
+
VND: { code: "VND", decimals: 0, name: "Vietnamese Dong" },
|
|
36
|
+
ZAR: { code: "ZAR", decimals: 2, name: "South African Rand" }
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
// src/errors.ts
|
|
40
|
+
var MonetaError = class extends Error {
|
|
41
|
+
constructor(message) {
|
|
42
|
+
super(message);
|
|
43
|
+
this.name = "MonetaError";
|
|
44
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
var CurrencyMismatchError = class extends MonetaError {
|
|
48
|
+
expected;
|
|
49
|
+
received;
|
|
50
|
+
constructor(expected, received) {
|
|
51
|
+
super(
|
|
52
|
+
`Currency mismatch: cannot operate on ${expected} and ${received}. Convert to a common currency first.`
|
|
53
|
+
);
|
|
54
|
+
this.name = "CurrencyMismatchError";
|
|
55
|
+
this.expected = expected;
|
|
56
|
+
this.received = received;
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
var InvalidAmountError = class extends MonetaError {
|
|
60
|
+
input;
|
|
61
|
+
constructor(input) {
|
|
62
|
+
super(
|
|
63
|
+
`Invalid amount: "${String(input)}". Expected a numeric string (e.g. "19.99"), a number, or a bigint.`
|
|
64
|
+
);
|
|
65
|
+
this.name = "InvalidAmountError";
|
|
66
|
+
this.input = input;
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
var DivisionByZeroError = class extends MonetaError {
|
|
70
|
+
constructor() {
|
|
71
|
+
super("Division by zero is not allowed.");
|
|
72
|
+
this.name = "DivisionByZeroError";
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
var UnknownCurrencyError = class extends MonetaError {
|
|
76
|
+
currency;
|
|
77
|
+
constructor(currency) {
|
|
78
|
+
super(
|
|
79
|
+
`Unknown currency code: "${currency}". See the CurrencyCode type for supported currencies.`
|
|
80
|
+
);
|
|
81
|
+
this.name = "UnknownCurrencyError";
|
|
82
|
+
this.currency = currency;
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
// src/money.ts
|
|
87
|
+
var AMOUNT_PATTERN = /^-?\d+(\.\d+)?$/;
|
|
88
|
+
function parseToMinorUnits(input, decimals) {
|
|
89
|
+
if (typeof input === "bigint") return input;
|
|
90
|
+
const str = typeof input === "number" ? input.toFixed(decimals) : input.trim();
|
|
91
|
+
if (!AMOUNT_PATTERN.test(str)) {
|
|
92
|
+
throw new InvalidAmountError(str);
|
|
93
|
+
}
|
|
94
|
+
const negative = str.startsWith("-");
|
|
95
|
+
const abs = negative ? str.slice(1) : str;
|
|
96
|
+
const dotIdx = abs.indexOf(".");
|
|
97
|
+
let whole;
|
|
98
|
+
let fraction;
|
|
99
|
+
if (dotIdx === -1) {
|
|
100
|
+
whole = abs;
|
|
101
|
+
fraction = "0".repeat(decimals);
|
|
102
|
+
} else {
|
|
103
|
+
whole = abs.slice(0, dotIdx);
|
|
104
|
+
fraction = abs.slice(dotIdx + 1).padEnd(decimals, "0").slice(0, decimals);
|
|
105
|
+
}
|
|
106
|
+
const scale = 10n ** BigInt(decimals);
|
|
107
|
+
const result = BigInt(whole) * scale + BigInt(fraction);
|
|
108
|
+
return negative ? -result : result;
|
|
109
|
+
}
|
|
110
|
+
function minorUnitsToString(units, decimals) {
|
|
111
|
+
const negative = units < 0n;
|
|
112
|
+
const abs = negative ? -units : units;
|
|
113
|
+
const scale = 10n ** BigInt(decimals);
|
|
114
|
+
const whole = abs / scale;
|
|
115
|
+
const fraction = abs % scale;
|
|
116
|
+
const fractionStr = decimals > 0 ? `.${fraction.toString().padStart(decimals, "0")}` : "";
|
|
117
|
+
return `${negative ? "-" : ""}${whole}${fractionStr}`;
|
|
118
|
+
}
|
|
119
|
+
function roundDiv(numerator, denominator) {
|
|
120
|
+
if (denominator === 0n) throw new DivisionByZeroError();
|
|
121
|
+
const absN = numerator < 0n ? -numerator : numerator;
|
|
122
|
+
const absD = denominator < 0n ? -denominator : denominator;
|
|
123
|
+
const rounded = (absN + absD / 2n) / absD;
|
|
124
|
+
const negative = numerator < 0n !== denominator < 0n;
|
|
125
|
+
return negative ? -rounded : rounded;
|
|
126
|
+
}
|
|
127
|
+
function parseScalar(input) {
|
|
128
|
+
const f = typeof input === "number" ? input.toString() : input.trim();
|
|
129
|
+
if (!AMOUNT_PATTERN.test(f)) throw new InvalidAmountError(f);
|
|
130
|
+
const negative = f.startsWith("-");
|
|
131
|
+
const abs = negative ? f.slice(1) : f;
|
|
132
|
+
const dotIdx = abs.indexOf(".");
|
|
133
|
+
const fracLen = dotIdx === -1 ? 0 : abs.length - dotIdx - 1;
|
|
134
|
+
const precision = 10n ** BigInt(fracLen);
|
|
135
|
+
const parts = abs.split(".");
|
|
136
|
+
const whole = parts[0] ?? "";
|
|
137
|
+
const frac = parts[1] ?? "";
|
|
138
|
+
const unsigned = BigInt(whole) * precision + BigInt(frac.padEnd(fracLen, "0"));
|
|
139
|
+
return { factorBig: negative ? -unsigned : unsigned, precision };
|
|
140
|
+
}
|
|
141
|
+
var Money = class _Money {
|
|
142
|
+
#minorUnits;
|
|
143
|
+
#currency;
|
|
144
|
+
#decimals;
|
|
145
|
+
constructor(amount, currency) {
|
|
146
|
+
if (!(currency in CURRENCIES)) {
|
|
147
|
+
throw new UnknownCurrencyError(String(currency));
|
|
148
|
+
}
|
|
149
|
+
this.#currency = currency;
|
|
150
|
+
this.#decimals = CURRENCIES[currency].decimals;
|
|
151
|
+
this.#minorUnits = parseToMinorUnits(amount, this.#decimals);
|
|
152
|
+
}
|
|
153
|
+
// ─── Static factories ──────────────────────────────────────────────────────
|
|
154
|
+
/** Create from raw minor units (e.g. cents). Useful when reading from a database. */
|
|
155
|
+
static fromMinorUnits(units, currency) {
|
|
156
|
+
const decimals = CURRENCIES[currency].decimals;
|
|
157
|
+
return new _Money(minorUnitsToString(units, decimals), currency);
|
|
158
|
+
}
|
|
159
|
+
// ─── Accessors ─────────────────────────────────────────────────────────────
|
|
160
|
+
/** Raw amount in minor units (e.g. cents). Safe for storage and wire transfer. */
|
|
161
|
+
get minorUnits() {
|
|
162
|
+
return this.#minorUnits;
|
|
163
|
+
}
|
|
164
|
+
get currency() {
|
|
165
|
+
return this.#currency;
|
|
166
|
+
}
|
|
167
|
+
/** Human-readable decimal string, e.g. "19.99". */
|
|
168
|
+
get decimalValue() {
|
|
169
|
+
return minorUnitsToString(this.#minorUnits, this.#decimals);
|
|
170
|
+
}
|
|
171
|
+
// ─── Arithmetic ────────────────────────────────────────────────────────────
|
|
172
|
+
add(other) {
|
|
173
|
+
this.#assertSameCurrency(other);
|
|
174
|
+
return this.#fromUnits(this.#minorUnits + other.#minorUnits);
|
|
175
|
+
}
|
|
176
|
+
subtract(other) {
|
|
177
|
+
this.#assertSameCurrency(other);
|
|
178
|
+
return this.#fromUnits(this.#minorUnits - other.#minorUnits);
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Multiply by a scalar. Uses half-up rounding to the currency's minor unit.
|
|
182
|
+
* @example money('10.00', 'USD').multiply(1.1) // $11.00
|
|
183
|
+
* @example money('10.00', 'USD').multiply('1.5') // $15.00
|
|
184
|
+
*/
|
|
185
|
+
multiply(factor) {
|
|
186
|
+
const { factorBig, precision } = parseScalar(factor);
|
|
187
|
+
return this.#fromUnits(roundDiv(this.#minorUnits * factorBig, precision));
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Divide by a scalar. Uses half-up rounding to the currency's minor unit.
|
|
191
|
+
* @example money('10.00', 'USD').divide(3) // $3.33
|
|
192
|
+
*/
|
|
193
|
+
divide(divisor) {
|
|
194
|
+
const { factorBig, precision } = parseScalar(divisor);
|
|
195
|
+
if (factorBig === 0n) throw new DivisionByZeroError();
|
|
196
|
+
return this.#fromUnits(roundDiv(this.#minorUnits * precision, factorBig));
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Allocate proportionally. The sum of all parts always equals the original —
|
|
200
|
+
* any rounding remainder is distributed to the first buckets.
|
|
201
|
+
*
|
|
202
|
+
* @example
|
|
203
|
+
* money('10.00', 'USD').allocate([1, 1, 1])
|
|
204
|
+
* // [Money('3.34'), Money('3.33'), Money('3.33')]
|
|
205
|
+
*/
|
|
206
|
+
allocate(ratios) {
|
|
207
|
+
if (ratios.length === 0) throw new InvalidAmountError("ratios array must not be empty");
|
|
208
|
+
const total = ratios.reduce((a, b) => a + b, 0);
|
|
209
|
+
if (total <= 0) throw new InvalidAmountError("ratios must sum to a positive number");
|
|
210
|
+
const PRECISION = 1000000n;
|
|
211
|
+
const totalBig = BigInt(Math.round(total * Number(PRECISION)));
|
|
212
|
+
const shares = ratios.map((r) => {
|
|
213
|
+
const rBig = BigInt(Math.round(r * Number(PRECISION)));
|
|
214
|
+
return roundDiv(this.#minorUnits * rBig, totalBig);
|
|
215
|
+
});
|
|
216
|
+
let remainder = this.#minorUnits - shares.reduce((a, b) => a + b, 0n);
|
|
217
|
+
const direction = remainder > 0n ? 1n : -1n;
|
|
218
|
+
for (let i = 0; remainder !== 0n; i++) {
|
|
219
|
+
const idx = i % shares.length;
|
|
220
|
+
shares[idx] = (shares[idx] ?? 0n) + direction;
|
|
221
|
+
remainder -= direction;
|
|
222
|
+
}
|
|
223
|
+
return shares.map((u) => this.#fromUnits(u));
|
|
224
|
+
}
|
|
225
|
+
// ─── Comparison ────────────────────────────────────────────────────────────
|
|
226
|
+
equals(other) {
|
|
227
|
+
this.#assertSameCurrency(other);
|
|
228
|
+
return this.#minorUnits === other.#minorUnits;
|
|
229
|
+
}
|
|
230
|
+
greaterThan(other) {
|
|
231
|
+
this.#assertSameCurrency(other);
|
|
232
|
+
return this.#minorUnits > other.#minorUnits;
|
|
233
|
+
}
|
|
234
|
+
lessThan(other) {
|
|
235
|
+
this.#assertSameCurrency(other);
|
|
236
|
+
return this.#minorUnits < other.#minorUnits;
|
|
237
|
+
}
|
|
238
|
+
greaterThanOrEqual(other) {
|
|
239
|
+
return !this.lessThan(other);
|
|
240
|
+
}
|
|
241
|
+
lessThanOrEqual(other) {
|
|
242
|
+
return !this.greaterThan(other);
|
|
243
|
+
}
|
|
244
|
+
isZero() {
|
|
245
|
+
return this.#minorUnits === 0n;
|
|
246
|
+
}
|
|
247
|
+
isPositive() {
|
|
248
|
+
return this.#minorUnits > 0n;
|
|
249
|
+
}
|
|
250
|
+
isNegative() {
|
|
251
|
+
return this.#minorUnits < 0n;
|
|
252
|
+
}
|
|
253
|
+
abs() {
|
|
254
|
+
return this.#fromUnits(this.#minorUnits < 0n ? -this.#minorUnits : this.#minorUnits);
|
|
255
|
+
}
|
|
256
|
+
negate() {
|
|
257
|
+
return this.#fromUnits(-this.#minorUnits);
|
|
258
|
+
}
|
|
259
|
+
// ─── Formatting ────────────────────────────────────────────────────────────
|
|
260
|
+
/**
|
|
261
|
+
* Format using the platform's `Intl.NumberFormat` — no custom format strings.
|
|
262
|
+
*
|
|
263
|
+
* @example
|
|
264
|
+
* money('19.99', 'USD').format() // "$19.99"
|
|
265
|
+
* money('19.99', 'EUR').format('de-DE') // "19,99 €"
|
|
266
|
+
*/
|
|
267
|
+
format(locale) {
|
|
268
|
+
return new Intl.NumberFormat(locale, {
|
|
269
|
+
style: "currency",
|
|
270
|
+
currency: this.#currency,
|
|
271
|
+
minimumFractionDigits: this.#decimals,
|
|
272
|
+
maximumFractionDigits: this.#decimals
|
|
273
|
+
}).format(Number(this.decimalValue));
|
|
274
|
+
}
|
|
275
|
+
// ─── Serialisation ─────────────────────────────────────────────────────────
|
|
276
|
+
/** Safe to store in JSON — no float representation. */
|
|
277
|
+
toJSON() {
|
|
278
|
+
return {
|
|
279
|
+
amount: this.decimalValue,
|
|
280
|
+
currency: this.#currency,
|
|
281
|
+
minorUnits: this.#minorUnits.toString()
|
|
282
|
+
};
|
|
283
|
+
}
|
|
284
|
+
toString() {
|
|
285
|
+
return `${this.decimalValue} ${this.#currency}`;
|
|
286
|
+
}
|
|
287
|
+
// ─── Private helpers ───────────────────────────────────────────────────────
|
|
288
|
+
#fromUnits(units) {
|
|
289
|
+
return new _Money(minorUnitsToString(units, this.#decimals), this.#currency);
|
|
290
|
+
}
|
|
291
|
+
#assertSameCurrency(other) {
|
|
292
|
+
if (this.#currency !== other.#currency) {
|
|
293
|
+
throw new CurrencyMismatchError(this.#currency, other.#currency);
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
};
|
|
297
|
+
|
|
298
|
+
// src/index.ts
|
|
299
|
+
function money(amount, currency) {
|
|
300
|
+
return new Money(amount, currency);
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
export { CURRENCIES, CurrencyMismatchError, DivisionByZeroError, InvalidAmountError, MonetaError, Money, UnknownCurrencyError, money };
|
|
304
|
+
//# sourceMappingURL=index.js.map
|
|
305
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1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AI,IAAA,CAAK,YAAA,CAAa,MAAA,EAAQ;AAAA,MACnC,KAAA,EAAO,UAAA;AAAA,MACP,UAAU,IAAA,CAAK,SAAA;AAAA,MACf,uBAAuB,IAAA,CAAK,SAAA;AAAA,MAC5B,uBAAuB,IAAA,CAAK;AAAA,KAC7B,CAAA,CAAE,MAAA,CAAO,MAAA,CAAO,IAAA,CAAK,YAAY,CAAC,CAAA;AAAA,EACrC;AAAA;AAAA;AAAA,EAKA,MAAA,GAA8D;AAC5D,IAAA,OAAO;AAAA,MACL,QAAQ,IAAA,CAAK,YAAA;AAAA,MACb,UAAU,IAAA,CAAK,SAAA;AAAA,MACf,UAAA,EAAY,IAAA,CAAK,WAAA,CAAY,QAAA;AAAS,KACxC;AAAA,EACF;AAAA,EAEA,QAAA,GAAmB;AACjB,IAAA,OAAO,CAAA,EAAG,IAAA,CAAK,YAAY,CAAA,CAAA,EAAI,KAAK,SAAS,CAAA,CAAA;AAAA,EAC/C;AAAA;AAAA,EAIA,WAAW,KAAA,EAAyB;AAClC,IAAA,OAAO,IAAI,OAAS,kBAAA,CAAmB,KAAA,EAAO,KAAK,SAAS,CAAA,EAAG,KAAK,SAAS,CAAA;AAAA,EAC/E;AAAA,EAEA,oBAAoB,KAAA,EAAuB;AACzC,IAAA,IAAI,IAAA,CAAK,SAAA,KAAc,KAAA,CAAM,SAAA,EAAW;AACtC,MAAA,MAAM,IAAI,qBAAA,CAAsB,IAAA,CAAK,SAAA,EAAW,MAAM,SAAmB,CAAA;AAAA,IAC3E;AAAA,EACF;AACF;;;ACrRO,SAAS,KAAA,CACd,QACA,QAAA,EACU;AACV,EAAA,OAAO,IAAI,KAAA,CAAM,MAAA,EAAQ,QAAQ,CAAA;AACnC","file":"index.js","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"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@puvaanraaj/moneta",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Immutable, BigInt-precision monetary values for TypeScript. No float errors. Currency-safe by design.",
|
|
5
|
+
"keywords": ["money", "currency", "finance", "bigint", "typescript", "safe-money", "payments"],
|
|
6
|
+
"homepage": "https://github.com/PuvaanRaaj/moneta#readme",
|
|
7
|
+
"bugs": "https://github.com/PuvaanRaaj/moneta/issues",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "https://github.com/PuvaanRaaj/moneta.git"
|
|
11
|
+
},
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"author": "PuvaanRaaj",
|
|
14
|
+
"type": "module",
|
|
15
|
+
"exports": {
|
|
16
|
+
".": {
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
18
|
+
"import": "./dist/index.js",
|
|
19
|
+
"require": "./dist/index.cjs"
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"main": "./dist/index.cjs",
|
|
23
|
+
"module": "./dist/index.js",
|
|
24
|
+
"types": "./dist/index.d.ts",
|
|
25
|
+
"files": [
|
|
26
|
+
"dist",
|
|
27
|
+
"README.md",
|
|
28
|
+
"LICENSE"
|
|
29
|
+
],
|
|
30
|
+
"scripts": {
|
|
31
|
+
"build": "tsup",
|
|
32
|
+
"dev": "tsup --watch",
|
|
33
|
+
"test": "vitest run",
|
|
34
|
+
"test:watch": "vitest",
|
|
35
|
+
"test:coverage": "vitest run --coverage",
|
|
36
|
+
"typecheck": "tsc --noEmit",
|
|
37
|
+
"lint": "biome check src tests",
|
|
38
|
+
"lint:fix": "biome check --write src tests",
|
|
39
|
+
"format": "biome format --write src tests",
|
|
40
|
+
"audit": "npm audit --audit-level=high",
|
|
41
|
+
"prepublishOnly": "npm run typecheck && npm run lint && npm run test && npm run build"
|
|
42
|
+
},
|
|
43
|
+
"devDependencies": {
|
|
44
|
+
"@biomejs/biome": "1.9.4",
|
|
45
|
+
"@vitest/coverage-v8": "^3.0.0",
|
|
46
|
+
"tsup": "^8.3.0",
|
|
47
|
+
"typescript": "^5.7.0",
|
|
48
|
+
"vitest": "^3.0.0"
|
|
49
|
+
},
|
|
50
|
+
"engines": {
|
|
51
|
+
"node": ">=18.0.0"
|
|
52
|
+
},
|
|
53
|
+
"publishConfig": {
|
|
54
|
+
"access": "public",
|
|
55
|
+
"provenance": true
|
|
56
|
+
}
|
|
57
|
+
}
|