@storepass/money 0.0.1-alpha.5

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 Atlas Alpha
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,162 @@
1
+ # sp-money
2
+
3
+ Safe, consistent, deterministic money operations.
4
+
5
+ - BigInt-backed internal arithmetic
6
+ - Explicit currency metadata (`code`, `decimalPlaces`)
7
+ - Deterministic rounding/conversion behavior
8
+ - No floating-point drift in stored values
9
+
10
+ ## Install
11
+
12
+ Package name: `@atlas-alpha/sp-money`
13
+
14
+ ### Current publish target
15
+
16
+ This package is currently configured for GitHub Packages.
17
+
18
+ ```bash
19
+ # npm
20
+ npm install @atlas-alpha/sp-money
21
+
22
+ # pnpm
23
+ pnpm add @atlas-alpha/sp-money
24
+
25
+ # yarn
26
+ yarn add @atlas-alpha/sp-money
27
+
28
+ # bun
29
+ bun add @atlas-alpha/sp-money
30
+ ```
31
+
32
+ If your org requires explicit registry config:
33
+
34
+ ```ini
35
+ # .npmrc
36
+ @atlas-alpha:registry=https://npm.pkg.github.com
37
+ ```
38
+
39
+ ### Open-source transition
40
+
41
+ The project is being prepared for a public open-source release.
42
+
43
+ ## Quick Start
44
+
45
+ ```ts
46
+ import { Currency, Money } from "@atlas-alpha/sp-money";
47
+
48
+ const price = Money.fromMinor(Currency.USD, 1234); // $12.34
49
+ const fee = Money.fromNumber(Currency.USD, 1.25);
50
+ const total = price.add(fee);
51
+
52
+ console.log(total.toMinor()); // 1359
53
+ console.log(total.toNumber()); // 13.59
54
+ console.log(total.currency.code); // "USD"
55
+ ```
56
+
57
+ ## API Overview
58
+
59
+ ### Money creation
60
+
61
+ ```ts
62
+ import { Currency, Money } from "@atlas-alpha/sp-money";
63
+
64
+ Money.fromMinor(Currency.USD, 1234); // $12.34
65
+ Money.fromNumber(Currency.USD, 12.345, { rounding: "round" }); // $12.35
66
+ Money.fromNumber(Currency.USD, 12.34, { strict: true }); // exact only
67
+ ```
68
+
69
+ ### Arithmetic and comparison
70
+
71
+ ```ts
72
+ const a = Money.fromNumber(Currency.USD, 10);
73
+ const b = Money.fromNumber(Currency.USD, 2.5);
74
+
75
+ const sum = a.add(b); // 12.5
76
+ const diff = a.subtract(b); // 7.5
77
+
78
+ Money.compare(a, b); // 1
79
+ Money.greaterThan(a, b); // true
80
+ ```
81
+
82
+ ### Allocation
83
+
84
+ ```ts
85
+ const m = Money.fromNumber(Currency.USD, 10);
86
+ const parts = m.allocate(3);
87
+
88
+ parts.map((p) => p.toNumber()); // [3.34, 3.33, 3.33]
89
+ Money.sum(parts).toNumber(); // 10
90
+ ```
91
+
92
+ ### Conversion and rounding
93
+
94
+ ```ts
95
+ const usd = Money.fromNumber(Currency.USD, 10);
96
+ const eur = usd.convert(Currency.EUR, 0.92); // exchange rate required
97
+
98
+ const rounded = Money.fromNumber(Currency.USD, 12.37).round(0.05);
99
+ rounded.toNumber(); // 12.35
100
+ ```
101
+
102
+ ### Percent operations
103
+
104
+ ```ts
105
+ const subtotal = Money.fromNumber(Currency.USD, 100);
106
+
107
+ subtotal.percentOf(8.25).toNumber(); // 8.25
108
+ subtotal.incrementByPercent(8.25).toNumber(); // 108.25
109
+ subtotal.decrementByPercent(10).toNumber(); // 90
110
+ ```
111
+
112
+ ### Custom currencies
113
+
114
+ ```ts
115
+ import { defineCurrency, Money } from "@atlas-alpha/sp-money";
116
+
117
+ const XAU = defineCurrency("XAU", 4);
118
+ const gold = Money.fromNumber(XAU, 1.2345);
119
+ ```
120
+
121
+ ### Serialization
122
+
123
+ ```ts
124
+ const m = Money.fromNumber(Currency.USD, 12.34);
125
+ const json = m.toJSON();
126
+ // { amount: 1234, currency: "USD" }
127
+ ```
128
+
129
+ `amount` is always in minor units.
130
+
131
+ ## Notes and Guarantees
132
+
133
+ - Currency mismatch operations throw (for example, adding USD to EUR).
134
+ - Results that exceed JavaScript safe integer range throw.
135
+ - Rounding modes supported: `"floor" | "ceil" | "round" | "trunc"`.
136
+ - `Money` instances are immutable; operations return new instances.
137
+
138
+ ## Development
139
+
140
+ ```bash
141
+ bun install # install dependencies
142
+ bun test # run tests
143
+ bun run build # build ESM + CJS + d.ts into dist/
144
+ bun run check # biome check + auto-fix/write
145
+ bun run lint # lint only
146
+ bun run format # format only
147
+ ```
148
+
149
+ ## Releasing
150
+
151
+ ```bash
152
+ ./scripts/release.sh # bump prerelease (default)
153
+ ./scripts/release.sh patch # bump patch
154
+ ./scripts/release.sh minor # bump minor
155
+ ./scripts/release.sh major # bump major
156
+ ```
157
+
158
+ The release script bumps version, commits, tags, and pushes commit + tags.
159
+
160
+ ## License
161
+
162
+ MIT
@@ -0,0 +1,121 @@
1
+ export interface CurrencyDefinition {
2
+ code: string;
3
+ decimalPlaces: number;
4
+ }
5
+ export declare const Currency: {
6
+ readonly USD: {
7
+ readonly code: "USD";
8
+ readonly decimalPlaces: 2;
9
+ };
10
+ readonly USDH: {
11
+ readonly code: "USDH";
12
+ readonly decimalPlaces: 3;
13
+ };
14
+ readonly EUR: {
15
+ readonly code: "EUR";
16
+ readonly decimalPlaces: 2;
17
+ };
18
+ readonly GBP: {
19
+ readonly code: "GBP";
20
+ readonly decimalPlaces: 2;
21
+ };
22
+ readonly JPY: {
23
+ readonly code: "JPY";
24
+ readonly decimalPlaces: 0;
25
+ };
26
+ readonly CAD: {
27
+ readonly code: "CAD";
28
+ readonly decimalPlaces: 2;
29
+ };
30
+ readonly AUD: {
31
+ readonly code: "AUD";
32
+ readonly decimalPlaces: 2;
33
+ };
34
+ readonly NZD: {
35
+ readonly code: "NZD";
36
+ readonly decimalPlaces: 2;
37
+ };
38
+ readonly CHF: {
39
+ readonly code: "CHF";
40
+ readonly decimalPlaces: 2;
41
+ };
42
+ readonly SEK: {
43
+ readonly code: "SEK";
44
+ readonly decimalPlaces: 2;
45
+ };
46
+ readonly NOK: {
47
+ readonly code: "NOK";
48
+ readonly decimalPlaces: 2;
49
+ };
50
+ readonly DKK: {
51
+ readonly code: "DKK";
52
+ readonly decimalPlaces: 2;
53
+ };
54
+ readonly PLN: {
55
+ readonly code: "PLN";
56
+ readonly decimalPlaces: 2;
57
+ };
58
+ readonly CZK: {
59
+ readonly code: "CZK";
60
+ readonly decimalPlaces: 2;
61
+ };
62
+ readonly HUF: {
63
+ readonly code: "HUF";
64
+ readonly decimalPlaces: 2;
65
+ };
66
+ readonly BRL: {
67
+ readonly code: "BRL";
68
+ readonly decimalPlaces: 2;
69
+ };
70
+ readonly MXN: {
71
+ readonly code: "MXN";
72
+ readonly decimalPlaces: 2;
73
+ };
74
+ readonly ARS: {
75
+ readonly code: "ARS";
76
+ readonly decimalPlaces: 2;
77
+ };
78
+ readonly CLP: {
79
+ readonly code: "CLP";
80
+ readonly decimalPlaces: 0;
81
+ };
82
+ readonly INR: {
83
+ readonly code: "INR";
84
+ readonly decimalPlaces: 2;
85
+ };
86
+ readonly SGD: {
87
+ readonly code: "SGD";
88
+ readonly decimalPlaces: 2;
89
+ };
90
+ readonly HKD: {
91
+ readonly code: "HKD";
92
+ readonly decimalPlaces: 2;
93
+ };
94
+ readonly KRW: {
95
+ readonly code: "KRW";
96
+ readonly decimalPlaces: 0;
97
+ };
98
+ readonly CNY: {
99
+ readonly code: "CNY";
100
+ readonly decimalPlaces: 2;
101
+ };
102
+ readonly ZAR: {
103
+ readonly code: "ZAR";
104
+ readonly decimalPlaces: 2;
105
+ };
106
+ readonly AED: {
107
+ readonly code: "AED";
108
+ readonly decimalPlaces: 2;
109
+ };
110
+ readonly SAR: {
111
+ readonly code: "SAR";
112
+ readonly decimalPlaces: 2;
113
+ };
114
+ readonly BTC: {
115
+ readonly code: "BTC";
116
+ readonly decimalPlaces: 8;
117
+ };
118
+ };
119
+ export type CurrencyCode = keyof typeof Currency;
120
+ export type CurrencyType = (typeof Currency)[CurrencyCode];
121
+ export declare function defineCurrency(code: string, decimalPlaces: number): CurrencyDefinition;
package/dist/index.cjs ADDED
@@ -0,0 +1,423 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropNames = Object.getOwnPropertyNames;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __moduleCache = /* @__PURE__ */ new WeakMap;
6
+ var __toCommonJS = (from) => {
7
+ var entry = __moduleCache.get(from), desc;
8
+ if (entry)
9
+ return entry;
10
+ entry = __defProp({}, "__esModule", { value: true });
11
+ if (from && typeof from === "object" || typeof from === "function")
12
+ __getOwnPropNames(from).map((key) => !__hasOwnProp.call(entry, key) && __defProp(entry, key, {
13
+ get: () => from[key],
14
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
15
+ }));
16
+ __moduleCache.set(from, entry);
17
+ return entry;
18
+ };
19
+ var __export = (target, all) => {
20
+ for (var name in all)
21
+ __defProp(target, name, {
22
+ get: all[name],
23
+ enumerable: true,
24
+ configurable: true,
25
+ set: (newValue) => all[name] = () => newValue
26
+ });
27
+ };
28
+
29
+ // src/index.ts
30
+ var exports_src = {};
31
+ __export(exports_src, {
32
+ defineCurrency: () => defineCurrency,
33
+ Money: () => Money,
34
+ Currency: () => Currency
35
+ });
36
+ module.exports = __toCommonJS(exports_src);
37
+
38
+ // src/currency.ts
39
+ var Currency = {
40
+ USD: { code: "USD", decimalPlaces: 2 },
41
+ USDH: { code: "USDH", decimalPlaces: 3 },
42
+ EUR: { code: "EUR", decimalPlaces: 2 },
43
+ GBP: { code: "GBP", decimalPlaces: 2 },
44
+ JPY: { code: "JPY", decimalPlaces: 0 },
45
+ CAD: { code: "CAD", decimalPlaces: 2 },
46
+ AUD: { code: "AUD", decimalPlaces: 2 },
47
+ NZD: { code: "NZD", decimalPlaces: 2 },
48
+ CHF: { code: "CHF", decimalPlaces: 2 },
49
+ SEK: { code: "SEK", decimalPlaces: 2 },
50
+ NOK: { code: "NOK", decimalPlaces: 2 },
51
+ DKK: { code: "DKK", decimalPlaces: 2 },
52
+ PLN: { code: "PLN", decimalPlaces: 2 },
53
+ CZK: { code: "CZK", decimalPlaces: 2 },
54
+ HUF: { code: "HUF", decimalPlaces: 2 },
55
+ BRL: { code: "BRL", decimalPlaces: 2 },
56
+ MXN: { code: "MXN", decimalPlaces: 2 },
57
+ ARS: { code: "ARS", decimalPlaces: 2 },
58
+ CLP: { code: "CLP", decimalPlaces: 0 },
59
+ INR: { code: "INR", decimalPlaces: 2 },
60
+ SGD: { code: "SGD", decimalPlaces: 2 },
61
+ HKD: { code: "HKD", decimalPlaces: 2 },
62
+ KRW: { code: "KRW", decimalPlaces: 0 },
63
+ CNY: { code: "CNY", decimalPlaces: 2 },
64
+ ZAR: { code: "ZAR", decimalPlaces: 2 },
65
+ AED: { code: "AED", decimalPlaces: 2 },
66
+ SAR: { code: "SAR", decimalPlaces: 2 },
67
+ BTC: { code: "BTC", decimalPlaces: 8 }
68
+ };
69
+ function defineCurrency(code, decimalPlaces) {
70
+ if (!code || typeof code !== "string") {
71
+ throw new Error("Currency code must be a non-empty string");
72
+ }
73
+ if (!Number.isInteger(decimalPlaces) || decimalPlaces < 0) {
74
+ throw new Error("Decimal places must be a non-negative integer");
75
+ }
76
+ return Object.freeze({ code, decimalPlaces });
77
+ }
78
+ // src/money.ts
79
+ var INTERNAL_PRECISION = 20;
80
+ var INTERNAL_SCALE = 10n ** BigInt(INTERNAL_PRECISION);
81
+ function numberToDecimalString(n) {
82
+ if (!Number.isFinite(n)) {
83
+ throw new Error(`Cannot convert ${n} to decimal string`);
84
+ }
85
+ if (n === 0)
86
+ return "0";
87
+ const str = n.toString();
88
+ if (str.includes("e") || str.includes("E")) {
89
+ return n.toPrecision(17);
90
+ }
91
+ return str;
92
+ }
93
+ function parseDecimalStringToScaled(str) {
94
+ const negative = str.startsWith("-");
95
+ if (negative)
96
+ str = str.slice(1);
97
+ const eIndex = str.toLowerCase().indexOf("e");
98
+ if (eIndex !== -1) {
99
+ const mantissa = str.slice(0, eIndex);
100
+ const exponent = parseInt(str.slice(eIndex + 1), 10);
101
+ const scaledMantissa = parseDecimalStringToScaled(mantissa);
102
+ const sign = negative ? -1n : 1n;
103
+ if (exponent >= 0) {
104
+ return sign * scaledMantissa * 10n ** BigInt(exponent);
105
+ } else {
106
+ return sign * scaledMantissa / 10n ** BigInt(-exponent);
107
+ }
108
+ }
109
+ const dotIndex = str.indexOf(".");
110
+ let intPart;
111
+ let decPart;
112
+ if (dotIndex === -1) {
113
+ intPart = str || "0";
114
+ decPart = "";
115
+ } else {
116
+ intPart = str.slice(0, dotIndex) || "0";
117
+ decPart = str.slice(dotIndex + 1);
118
+ }
119
+ decPart = decPart.padEnd(INTERNAL_PRECISION, "0").slice(0, INTERNAL_PRECISION);
120
+ const scaledInt = BigInt(intPart) * INTERNAL_SCALE;
121
+ const scaledDec = BigInt(decPart);
122
+ const result = scaledInt + scaledDec;
123
+ return negative ? -result : result;
124
+ }
125
+ function roundScaledToMinor(scaled, targetDecimalPlaces, mode) {
126
+ const targetScale = 10n ** BigInt(targetDecimalPlaces);
127
+ const divisor = INTERNAL_SCALE / targetScale;
128
+ const quotient = scaled / divisor;
129
+ const remainder = scaled % divisor;
130
+ if (remainder === 0n)
131
+ return quotient;
132
+ const isNegative = scaled < 0n;
133
+ const absRemainder = isNegative ? -remainder : remainder;
134
+ const halfDivisor = divisor / 2n;
135
+ switch (mode) {
136
+ case "floor":
137
+ return isNegative ? quotient - 1n : quotient;
138
+ case "ceil":
139
+ return isNegative ? quotient : quotient + 1n;
140
+ case "trunc":
141
+ return quotient;
142
+ default:
143
+ if (absRemainder > halfDivisor || absRemainder === halfDivisor && !isNegative) {
144
+ return isNegative ? quotient - 1n : quotient + 1n;
145
+ }
146
+ return quotient;
147
+ }
148
+ }
149
+ function assertCurrenciesMatch(a, b, operation) {
150
+ if (a.code !== b.code || a.decimalPlaces !== b.decimalPlaces) {
151
+ throw new Error(`Cannot ${operation} ${a.code} and ${b.code}: currency mismatch`);
152
+ }
153
+ }
154
+ function assertSafeResult(value) {
155
+ if (value > BigInt(Number.MAX_SAFE_INTEGER) || value < BigInt(-Number.MAX_SAFE_INTEGER)) {
156
+ throw new Error("Result too large: exceeds safe integer range");
157
+ }
158
+ }
159
+
160
+ class Money {
161
+ #minor;
162
+ #currency;
163
+ constructor(minor, currency) {
164
+ this.#minor = minor;
165
+ this.#currency = currency;
166
+ }
167
+ get currency() {
168
+ return this.#currency;
169
+ }
170
+ static fromNumber(currency, value, options = {}) {
171
+ const { rounding = "round", strict } = options;
172
+ const multiplier = 10 ** currency.decimalPlaces;
173
+ const estimatedMinor = Math.abs(value) * multiplier;
174
+ if (estimatedMinor > Number.MAX_SAFE_INTEGER) {
175
+ throw new Error(`Value too large: ${value} exceeds safe integer range when converted to minor units`);
176
+ }
177
+ const valueStr = numberToDecimalString(value);
178
+ const scaledValue = parseDecimalStringToScaled(valueStr);
179
+ if (strict) {
180
+ const targetScale = 10n ** BigInt(currency.decimalPlaces);
181
+ const divisor = INTERNAL_SCALE / targetScale;
182
+ if (scaledValue % divisor !== 0n) {
183
+ throw new Error(`Precision loss: ${value} cannot be exactly represented with ${currency.decimalPlaces} decimal places`);
184
+ }
185
+ }
186
+ const minor = roundScaledToMinor(scaledValue, currency.decimalPlaces, rounding);
187
+ assertSafeResult(minor);
188
+ return new Money(minor, currency);
189
+ }
190
+ static fromMinor(currency, minor) {
191
+ return new Money(BigInt(minor), currency);
192
+ }
193
+ toNumber() {
194
+ const divisor = 10 ** this.#currency.decimalPlaces;
195
+ return Number(this.#minor) / divisor;
196
+ }
197
+ toMinor() {
198
+ return Number(this.#minor);
199
+ }
200
+ static add(a, b) {
201
+ assertCurrenciesMatch(a.#currency, b.#currency, "add");
202
+ const result = a.#minor + b.#minor;
203
+ assertSafeResult(result);
204
+ return new Money(result, a.#currency);
205
+ }
206
+ static subtract(a, b) {
207
+ assertCurrenciesMatch(a.#currency, b.#currency, "subtract");
208
+ const result = a.#minor - b.#minor;
209
+ assertSafeResult(result);
210
+ return new Money(result, a.#currency);
211
+ }
212
+ static sum(items) {
213
+ const first = items[0];
214
+ if (first === undefined) {
215
+ throw new Error("Cannot sum empty array: at least one Money value required");
216
+ }
217
+ const currency = first.#currency;
218
+ let total = 0n;
219
+ for (const item of items) {
220
+ assertCurrenciesMatch(item.#currency, currency, "sum");
221
+ total += item.#minor;
222
+ }
223
+ assertSafeResult(total);
224
+ return new Money(total, currency);
225
+ }
226
+ static equals(a, b) {
227
+ assertCurrenciesMatch(a.#currency, b.#currency, "compare");
228
+ return a.#minor === b.#minor;
229
+ }
230
+ static compare(a, b) {
231
+ assertCurrenciesMatch(a.#currency, b.#currency, "compare");
232
+ if (a.#minor < b.#minor)
233
+ return -1;
234
+ if (a.#minor > b.#minor)
235
+ return 1;
236
+ return 0;
237
+ }
238
+ static lessThan(a, b) {
239
+ assertCurrenciesMatch(a.#currency, b.#currency, "compare");
240
+ return a.#minor < b.#minor;
241
+ }
242
+ static greaterThan(a, b) {
243
+ assertCurrenciesMatch(a.#currency, b.#currency, "compare");
244
+ return a.#minor > b.#minor;
245
+ }
246
+ static lessThanOrEqual(a, b) {
247
+ assertCurrenciesMatch(a.#currency, b.#currency, "compare");
248
+ return a.#minor <= b.#minor;
249
+ }
250
+ static greaterThanOrEqual(a, b) {
251
+ assertCurrenciesMatch(a.#currency, b.#currency, "compare");
252
+ return a.#minor >= b.#minor;
253
+ }
254
+ add(other) {
255
+ return Money.add(this, other);
256
+ }
257
+ subtract(other) {
258
+ return Money.subtract(this, other);
259
+ }
260
+ equals(other) {
261
+ return Money.equals(this, other);
262
+ }
263
+ compare(other) {
264
+ return Money.compare(this, other);
265
+ }
266
+ lessThan(other) {
267
+ return Money.lessThan(this, other);
268
+ }
269
+ greaterThan(other) {
270
+ return Money.greaterThan(this, other);
271
+ }
272
+ lessThanOrEqual(other) {
273
+ return Money.lessThanOrEqual(this, other);
274
+ }
275
+ greaterThanOrEqual(other) {
276
+ return Money.greaterThanOrEqual(this, other);
277
+ }
278
+ isZero() {
279
+ return this.#minor === 0n;
280
+ }
281
+ isPositive() {
282
+ return this.#minor > 0n;
283
+ }
284
+ isNegative() {
285
+ return this.#minor < 0n;
286
+ }
287
+ static allocate(money, parts) {
288
+ if (!Number.isInteger(parts) || parts < 1) {
289
+ throw new Error("Allocation count must be a positive integer");
290
+ }
291
+ if (!Number.isSafeInteger(parts)) {
292
+ throw new Error("Allocation count must be a safe integer");
293
+ }
294
+ const partsBigInt = BigInt(parts);
295
+ const base = money.#minor / partsBigInt;
296
+ const remainder = money.#minor % partsBigInt;
297
+ const absRemainder = remainder < 0n ? -remainder : remainder;
298
+ const sign = remainder < 0n ? -1n : 1n;
299
+ const result = [];
300
+ for (let i = 0n;i < partsBigInt; i++) {
301
+ const extra = i < absRemainder ? sign : 0n;
302
+ result.push(new Money(base + extra, money.#currency));
303
+ }
304
+ return result;
305
+ }
306
+ allocate(parts) {
307
+ return Money.allocate(this, parts);
308
+ }
309
+ static convert(money, targetCurrency, rate, options = {}) {
310
+ if (!Number.isFinite(rate) || rate <= 0) {
311
+ throw new Error("Exchange rate must be a finite positive number");
312
+ }
313
+ const { rounding = "round" } = options;
314
+ const scaledRate = parseDecimalStringToScaled(numberToDecimalString(rate));
315
+ const sourceScale = 10n ** BigInt(money.#currency.decimalPlaces);
316
+ const scaledSource = money.#minor * (INTERNAL_SCALE / sourceScale);
317
+ const rawProduct = scaledSource * scaledRate / INTERNAL_SCALE;
318
+ const resultMinor = roundScaledToMinor(rawProduct, targetCurrency.decimalPlaces, rounding);
319
+ assertSafeResult(resultMinor);
320
+ return new Money(resultMinor, targetCurrency);
321
+ }
322
+ convert(targetCurrency, rate, options) {
323
+ return Money.convert(this, targetCurrency, rate, options);
324
+ }
325
+ static roundTo(money, increment, mode = "round") {
326
+ if (!Number.isFinite(increment) || increment <= 0) {
327
+ throw new Error("Rounding increment must be a finite positive number");
328
+ }
329
+ const scaledIncrement = parseDecimalStringToScaled(numberToDecimalString(increment));
330
+ const targetScale = 10n ** BigInt(money.#currency.decimalPlaces);
331
+ const divisor = INTERNAL_SCALE / targetScale;
332
+ if (scaledIncrement % divisor !== 0n) {
333
+ throw new Error(`Rounding increment ${increment} cannot be exactly represented with ${money.#currency.decimalPlaces} decimal places`);
334
+ }
335
+ const incrementMinor = scaledIncrement / divisor;
336
+ if (incrementMinor === 0n) {
337
+ throw new Error("Rounding increment is too small for this currency's precision");
338
+ }
339
+ const minor = money.#minor;
340
+ const remainder = minor % incrementMinor;
341
+ if (remainder === 0n) {
342
+ return new Money(minor, money.#currency);
343
+ }
344
+ const base = minor - remainder;
345
+ let rounded;
346
+ switch (mode) {
347
+ case "trunc":
348
+ rounded = base;
349
+ break;
350
+ case "floor":
351
+ rounded = minor < 0n ? base - incrementMinor : base;
352
+ break;
353
+ case "ceil":
354
+ rounded = minor < 0n ? base : base + incrementMinor;
355
+ break;
356
+ default: {
357
+ const absRemainder = remainder < 0n ? -remainder : remainder;
358
+ const cmp = absRemainder * 2n - incrementMinor;
359
+ if (cmp < 0n) {
360
+ rounded = base;
361
+ } else if (cmp > 0n) {
362
+ rounded = minor < 0n ? base - incrementMinor : base + incrementMinor;
363
+ } else {
364
+ rounded = minor < 0n ? base : base + incrementMinor;
365
+ }
366
+ break;
367
+ }
368
+ }
369
+ assertSafeResult(rounded);
370
+ return new Money(rounded, money.#currency);
371
+ }
372
+ round(increment, mode) {
373
+ return Money.roundTo(this, increment, mode);
374
+ }
375
+ static percentOf(money, percent, options = {}) {
376
+ const { rounding = "round" } = options;
377
+ const scaledPercent = parseDecimalStringToScaled(numberToDecimalString(percent));
378
+ const scaledHundred = 100n * INTERNAL_SCALE;
379
+ const currencyScale = 10n ** BigInt(money.#currency.decimalPlaces);
380
+ const scaledMoney = money.#minor * (INTERNAL_SCALE / currencyScale);
381
+ const rawResult = scaledMoney * scaledPercent / scaledHundred;
382
+ const resultMinor = roundScaledToMinor(rawResult, money.#currency.decimalPlaces, rounding);
383
+ assertSafeResult(resultMinor);
384
+ return new Money(resultMinor, money.#currency);
385
+ }
386
+ static incrementByPercent(money, percent, options = {}) {
387
+ const { rounding = "round" } = options;
388
+ const scaledPercent = parseDecimalStringToScaled(numberToDecimalString(percent));
389
+ const scaledHundred = 100n * INTERNAL_SCALE;
390
+ const currencyScale = 10n ** BigInt(money.#currency.decimalPlaces);
391
+ const scaledMoney = money.#minor * (INTERNAL_SCALE / currencyScale);
392
+ const rawResult = scaledMoney * (scaledHundred + scaledPercent) / scaledHundred;
393
+ const resultMinor = roundScaledToMinor(rawResult, money.#currency.decimalPlaces, rounding);
394
+ assertSafeResult(resultMinor);
395
+ return new Money(resultMinor, money.#currency);
396
+ }
397
+ static decrementByPercent(money, percent, options = {}) {
398
+ const { rounding = "round" } = options;
399
+ const scaledPercent = parseDecimalStringToScaled(numberToDecimalString(percent));
400
+ const scaledHundred = 100n * INTERNAL_SCALE;
401
+ const currencyScale = 10n ** BigInt(money.#currency.decimalPlaces);
402
+ const scaledMoney = money.#minor * (INTERNAL_SCALE / currencyScale);
403
+ const rawResult = scaledMoney * (scaledHundred - scaledPercent) / scaledHundred;
404
+ const resultMinor = roundScaledToMinor(rawResult, money.#currency.decimalPlaces, rounding);
405
+ assertSafeResult(resultMinor);
406
+ return new Money(resultMinor, money.#currency);
407
+ }
408
+ percentOf(percent, options) {
409
+ return Money.percentOf(this, percent, options);
410
+ }
411
+ incrementByPercent(percent, options) {
412
+ return Money.incrementByPercent(this, percent, options);
413
+ }
414
+ decrementByPercent(percent, options) {
415
+ return Money.decrementByPercent(this, percent, options);
416
+ }
417
+ toJSON() {
418
+ return {
419
+ amount: this.toMinor(),
420
+ currency: this.#currency.code
421
+ };
422
+ }
423
+ }
@@ -0,0 +1,2 @@
1
+ export { Currency, type CurrencyCode, type CurrencyDefinition, type CurrencyType, defineCurrency, } from "./currency";
2
+ export { type FromNumberOptions, Money, type RoundingMode } from "./money";
package/dist/index.js ADDED
@@ -0,0 +1,391 @@
1
+ // src/currency.ts
2
+ var Currency = {
3
+ USD: { code: "USD", decimalPlaces: 2 },
4
+ USDH: { code: "USDH", decimalPlaces: 3 },
5
+ EUR: { code: "EUR", decimalPlaces: 2 },
6
+ GBP: { code: "GBP", decimalPlaces: 2 },
7
+ JPY: { code: "JPY", decimalPlaces: 0 },
8
+ CAD: { code: "CAD", decimalPlaces: 2 },
9
+ AUD: { code: "AUD", decimalPlaces: 2 },
10
+ NZD: { code: "NZD", decimalPlaces: 2 },
11
+ CHF: { code: "CHF", decimalPlaces: 2 },
12
+ SEK: { code: "SEK", decimalPlaces: 2 },
13
+ NOK: { code: "NOK", decimalPlaces: 2 },
14
+ DKK: { code: "DKK", decimalPlaces: 2 },
15
+ PLN: { code: "PLN", decimalPlaces: 2 },
16
+ CZK: { code: "CZK", decimalPlaces: 2 },
17
+ HUF: { code: "HUF", decimalPlaces: 2 },
18
+ BRL: { code: "BRL", decimalPlaces: 2 },
19
+ MXN: { code: "MXN", decimalPlaces: 2 },
20
+ ARS: { code: "ARS", decimalPlaces: 2 },
21
+ CLP: { code: "CLP", decimalPlaces: 0 },
22
+ INR: { code: "INR", decimalPlaces: 2 },
23
+ SGD: { code: "SGD", decimalPlaces: 2 },
24
+ HKD: { code: "HKD", decimalPlaces: 2 },
25
+ KRW: { code: "KRW", decimalPlaces: 0 },
26
+ CNY: { code: "CNY", decimalPlaces: 2 },
27
+ ZAR: { code: "ZAR", decimalPlaces: 2 },
28
+ AED: { code: "AED", decimalPlaces: 2 },
29
+ SAR: { code: "SAR", decimalPlaces: 2 },
30
+ BTC: { code: "BTC", decimalPlaces: 8 }
31
+ };
32
+ function defineCurrency(code, decimalPlaces) {
33
+ if (!code || typeof code !== "string") {
34
+ throw new Error("Currency code must be a non-empty string");
35
+ }
36
+ if (!Number.isInteger(decimalPlaces) || decimalPlaces < 0) {
37
+ throw new Error("Decimal places must be a non-negative integer");
38
+ }
39
+ return Object.freeze({ code, decimalPlaces });
40
+ }
41
+ // src/money.ts
42
+ var INTERNAL_PRECISION = 20;
43
+ var INTERNAL_SCALE = 10n ** BigInt(INTERNAL_PRECISION);
44
+ function numberToDecimalString(n) {
45
+ if (!Number.isFinite(n)) {
46
+ throw new Error(`Cannot convert ${n} to decimal string`);
47
+ }
48
+ if (n === 0)
49
+ return "0";
50
+ const str = n.toString();
51
+ if (str.includes("e") || str.includes("E")) {
52
+ return n.toPrecision(17);
53
+ }
54
+ return str;
55
+ }
56
+ function parseDecimalStringToScaled(str) {
57
+ const negative = str.startsWith("-");
58
+ if (negative)
59
+ str = str.slice(1);
60
+ const eIndex = str.toLowerCase().indexOf("e");
61
+ if (eIndex !== -1) {
62
+ const mantissa = str.slice(0, eIndex);
63
+ const exponent = parseInt(str.slice(eIndex + 1), 10);
64
+ const scaledMantissa = parseDecimalStringToScaled(mantissa);
65
+ const sign = negative ? -1n : 1n;
66
+ if (exponent >= 0) {
67
+ return sign * scaledMantissa * 10n ** BigInt(exponent);
68
+ } else {
69
+ return sign * scaledMantissa / 10n ** BigInt(-exponent);
70
+ }
71
+ }
72
+ const dotIndex = str.indexOf(".");
73
+ let intPart;
74
+ let decPart;
75
+ if (dotIndex === -1) {
76
+ intPart = str || "0";
77
+ decPart = "";
78
+ } else {
79
+ intPart = str.slice(0, dotIndex) || "0";
80
+ decPart = str.slice(dotIndex + 1);
81
+ }
82
+ decPart = decPart.padEnd(INTERNAL_PRECISION, "0").slice(0, INTERNAL_PRECISION);
83
+ const scaledInt = BigInt(intPart) * INTERNAL_SCALE;
84
+ const scaledDec = BigInt(decPart);
85
+ const result = scaledInt + scaledDec;
86
+ return negative ? -result : result;
87
+ }
88
+ function roundScaledToMinor(scaled, targetDecimalPlaces, mode) {
89
+ const targetScale = 10n ** BigInt(targetDecimalPlaces);
90
+ const divisor = INTERNAL_SCALE / targetScale;
91
+ const quotient = scaled / divisor;
92
+ const remainder = scaled % divisor;
93
+ if (remainder === 0n)
94
+ return quotient;
95
+ const isNegative = scaled < 0n;
96
+ const absRemainder = isNegative ? -remainder : remainder;
97
+ const halfDivisor = divisor / 2n;
98
+ switch (mode) {
99
+ case "floor":
100
+ return isNegative ? quotient - 1n : quotient;
101
+ case "ceil":
102
+ return isNegative ? quotient : quotient + 1n;
103
+ case "trunc":
104
+ return quotient;
105
+ default:
106
+ if (absRemainder > halfDivisor || absRemainder === halfDivisor && !isNegative) {
107
+ return isNegative ? quotient - 1n : quotient + 1n;
108
+ }
109
+ return quotient;
110
+ }
111
+ }
112
+ function assertCurrenciesMatch(a, b, operation) {
113
+ if (a.code !== b.code || a.decimalPlaces !== b.decimalPlaces) {
114
+ throw new Error(`Cannot ${operation} ${a.code} and ${b.code}: currency mismatch`);
115
+ }
116
+ }
117
+ function assertSafeResult(value) {
118
+ if (value > BigInt(Number.MAX_SAFE_INTEGER) || value < BigInt(-Number.MAX_SAFE_INTEGER)) {
119
+ throw new Error("Result too large: exceeds safe integer range");
120
+ }
121
+ }
122
+
123
+ class Money {
124
+ #minor;
125
+ #currency;
126
+ constructor(minor, currency) {
127
+ this.#minor = minor;
128
+ this.#currency = currency;
129
+ }
130
+ get currency() {
131
+ return this.#currency;
132
+ }
133
+ static fromNumber(currency, value, options = {}) {
134
+ const { rounding = "round", strict } = options;
135
+ const multiplier = 10 ** currency.decimalPlaces;
136
+ const estimatedMinor = Math.abs(value) * multiplier;
137
+ if (estimatedMinor > Number.MAX_SAFE_INTEGER) {
138
+ throw new Error(`Value too large: ${value} exceeds safe integer range when converted to minor units`);
139
+ }
140
+ const valueStr = numberToDecimalString(value);
141
+ const scaledValue = parseDecimalStringToScaled(valueStr);
142
+ if (strict) {
143
+ const targetScale = 10n ** BigInt(currency.decimalPlaces);
144
+ const divisor = INTERNAL_SCALE / targetScale;
145
+ if (scaledValue % divisor !== 0n) {
146
+ throw new Error(`Precision loss: ${value} cannot be exactly represented with ${currency.decimalPlaces} decimal places`);
147
+ }
148
+ }
149
+ const minor = roundScaledToMinor(scaledValue, currency.decimalPlaces, rounding);
150
+ assertSafeResult(minor);
151
+ return new Money(minor, currency);
152
+ }
153
+ static fromMinor(currency, minor) {
154
+ return new Money(BigInt(minor), currency);
155
+ }
156
+ toNumber() {
157
+ const divisor = 10 ** this.#currency.decimalPlaces;
158
+ return Number(this.#minor) / divisor;
159
+ }
160
+ toMinor() {
161
+ return Number(this.#minor);
162
+ }
163
+ static add(a, b) {
164
+ assertCurrenciesMatch(a.#currency, b.#currency, "add");
165
+ const result = a.#minor + b.#minor;
166
+ assertSafeResult(result);
167
+ return new Money(result, a.#currency);
168
+ }
169
+ static subtract(a, b) {
170
+ assertCurrenciesMatch(a.#currency, b.#currency, "subtract");
171
+ const result = a.#minor - b.#minor;
172
+ assertSafeResult(result);
173
+ return new Money(result, a.#currency);
174
+ }
175
+ static sum(items) {
176
+ const first = items[0];
177
+ if (first === undefined) {
178
+ throw new Error("Cannot sum empty array: at least one Money value required");
179
+ }
180
+ const currency = first.#currency;
181
+ let total = 0n;
182
+ for (const item of items) {
183
+ assertCurrenciesMatch(item.#currency, currency, "sum");
184
+ total += item.#minor;
185
+ }
186
+ assertSafeResult(total);
187
+ return new Money(total, currency);
188
+ }
189
+ static equals(a, b) {
190
+ assertCurrenciesMatch(a.#currency, b.#currency, "compare");
191
+ return a.#minor === b.#minor;
192
+ }
193
+ static compare(a, b) {
194
+ assertCurrenciesMatch(a.#currency, b.#currency, "compare");
195
+ if (a.#minor < b.#minor)
196
+ return -1;
197
+ if (a.#minor > b.#minor)
198
+ return 1;
199
+ return 0;
200
+ }
201
+ static lessThan(a, b) {
202
+ assertCurrenciesMatch(a.#currency, b.#currency, "compare");
203
+ return a.#minor < b.#minor;
204
+ }
205
+ static greaterThan(a, b) {
206
+ assertCurrenciesMatch(a.#currency, b.#currency, "compare");
207
+ return a.#minor > b.#minor;
208
+ }
209
+ static lessThanOrEqual(a, b) {
210
+ assertCurrenciesMatch(a.#currency, b.#currency, "compare");
211
+ return a.#minor <= b.#minor;
212
+ }
213
+ static greaterThanOrEqual(a, b) {
214
+ assertCurrenciesMatch(a.#currency, b.#currency, "compare");
215
+ return a.#minor >= b.#minor;
216
+ }
217
+ add(other) {
218
+ return Money.add(this, other);
219
+ }
220
+ subtract(other) {
221
+ return Money.subtract(this, other);
222
+ }
223
+ equals(other) {
224
+ return Money.equals(this, other);
225
+ }
226
+ compare(other) {
227
+ return Money.compare(this, other);
228
+ }
229
+ lessThan(other) {
230
+ return Money.lessThan(this, other);
231
+ }
232
+ greaterThan(other) {
233
+ return Money.greaterThan(this, other);
234
+ }
235
+ lessThanOrEqual(other) {
236
+ return Money.lessThanOrEqual(this, other);
237
+ }
238
+ greaterThanOrEqual(other) {
239
+ return Money.greaterThanOrEqual(this, other);
240
+ }
241
+ isZero() {
242
+ return this.#minor === 0n;
243
+ }
244
+ isPositive() {
245
+ return this.#minor > 0n;
246
+ }
247
+ isNegative() {
248
+ return this.#minor < 0n;
249
+ }
250
+ static allocate(money, parts) {
251
+ if (!Number.isInteger(parts) || parts < 1) {
252
+ throw new Error("Allocation count must be a positive integer");
253
+ }
254
+ if (!Number.isSafeInteger(parts)) {
255
+ throw new Error("Allocation count must be a safe integer");
256
+ }
257
+ const partsBigInt = BigInt(parts);
258
+ const base = money.#minor / partsBigInt;
259
+ const remainder = money.#minor % partsBigInt;
260
+ const absRemainder = remainder < 0n ? -remainder : remainder;
261
+ const sign = remainder < 0n ? -1n : 1n;
262
+ const result = [];
263
+ for (let i = 0n;i < partsBigInt; i++) {
264
+ const extra = i < absRemainder ? sign : 0n;
265
+ result.push(new Money(base + extra, money.#currency));
266
+ }
267
+ return result;
268
+ }
269
+ allocate(parts) {
270
+ return Money.allocate(this, parts);
271
+ }
272
+ static convert(money, targetCurrency, rate, options = {}) {
273
+ if (!Number.isFinite(rate) || rate <= 0) {
274
+ throw new Error("Exchange rate must be a finite positive number");
275
+ }
276
+ const { rounding = "round" } = options;
277
+ const scaledRate = parseDecimalStringToScaled(numberToDecimalString(rate));
278
+ const sourceScale = 10n ** BigInt(money.#currency.decimalPlaces);
279
+ const scaledSource = money.#minor * (INTERNAL_SCALE / sourceScale);
280
+ const rawProduct = scaledSource * scaledRate / INTERNAL_SCALE;
281
+ const resultMinor = roundScaledToMinor(rawProduct, targetCurrency.decimalPlaces, rounding);
282
+ assertSafeResult(resultMinor);
283
+ return new Money(resultMinor, targetCurrency);
284
+ }
285
+ convert(targetCurrency, rate, options) {
286
+ return Money.convert(this, targetCurrency, rate, options);
287
+ }
288
+ static roundTo(money, increment, mode = "round") {
289
+ if (!Number.isFinite(increment) || increment <= 0) {
290
+ throw new Error("Rounding increment must be a finite positive number");
291
+ }
292
+ const scaledIncrement = parseDecimalStringToScaled(numberToDecimalString(increment));
293
+ const targetScale = 10n ** BigInt(money.#currency.decimalPlaces);
294
+ const divisor = INTERNAL_SCALE / targetScale;
295
+ if (scaledIncrement % divisor !== 0n) {
296
+ throw new Error(`Rounding increment ${increment} cannot be exactly represented with ${money.#currency.decimalPlaces} decimal places`);
297
+ }
298
+ const incrementMinor = scaledIncrement / divisor;
299
+ if (incrementMinor === 0n) {
300
+ throw new Error("Rounding increment is too small for this currency's precision");
301
+ }
302
+ const minor = money.#minor;
303
+ const remainder = minor % incrementMinor;
304
+ if (remainder === 0n) {
305
+ return new Money(minor, money.#currency);
306
+ }
307
+ const base = minor - remainder;
308
+ let rounded;
309
+ switch (mode) {
310
+ case "trunc":
311
+ rounded = base;
312
+ break;
313
+ case "floor":
314
+ rounded = minor < 0n ? base - incrementMinor : base;
315
+ break;
316
+ case "ceil":
317
+ rounded = minor < 0n ? base : base + incrementMinor;
318
+ break;
319
+ default: {
320
+ const absRemainder = remainder < 0n ? -remainder : remainder;
321
+ const cmp = absRemainder * 2n - incrementMinor;
322
+ if (cmp < 0n) {
323
+ rounded = base;
324
+ } else if (cmp > 0n) {
325
+ rounded = minor < 0n ? base - incrementMinor : base + incrementMinor;
326
+ } else {
327
+ rounded = minor < 0n ? base : base + incrementMinor;
328
+ }
329
+ break;
330
+ }
331
+ }
332
+ assertSafeResult(rounded);
333
+ return new Money(rounded, money.#currency);
334
+ }
335
+ round(increment, mode) {
336
+ return Money.roundTo(this, increment, mode);
337
+ }
338
+ static percentOf(money, percent, options = {}) {
339
+ const { rounding = "round" } = options;
340
+ const scaledPercent = parseDecimalStringToScaled(numberToDecimalString(percent));
341
+ const scaledHundred = 100n * INTERNAL_SCALE;
342
+ const currencyScale = 10n ** BigInt(money.#currency.decimalPlaces);
343
+ const scaledMoney = money.#minor * (INTERNAL_SCALE / currencyScale);
344
+ const rawResult = scaledMoney * scaledPercent / scaledHundred;
345
+ const resultMinor = roundScaledToMinor(rawResult, money.#currency.decimalPlaces, rounding);
346
+ assertSafeResult(resultMinor);
347
+ return new Money(resultMinor, money.#currency);
348
+ }
349
+ static incrementByPercent(money, percent, options = {}) {
350
+ const { rounding = "round" } = options;
351
+ const scaledPercent = parseDecimalStringToScaled(numberToDecimalString(percent));
352
+ const scaledHundred = 100n * INTERNAL_SCALE;
353
+ const currencyScale = 10n ** BigInt(money.#currency.decimalPlaces);
354
+ const scaledMoney = money.#minor * (INTERNAL_SCALE / currencyScale);
355
+ const rawResult = scaledMoney * (scaledHundred + scaledPercent) / scaledHundred;
356
+ const resultMinor = roundScaledToMinor(rawResult, money.#currency.decimalPlaces, rounding);
357
+ assertSafeResult(resultMinor);
358
+ return new Money(resultMinor, money.#currency);
359
+ }
360
+ static decrementByPercent(money, percent, options = {}) {
361
+ const { rounding = "round" } = options;
362
+ const scaledPercent = parseDecimalStringToScaled(numberToDecimalString(percent));
363
+ const scaledHundred = 100n * INTERNAL_SCALE;
364
+ const currencyScale = 10n ** BigInt(money.#currency.decimalPlaces);
365
+ const scaledMoney = money.#minor * (INTERNAL_SCALE / currencyScale);
366
+ const rawResult = scaledMoney * (scaledHundred - scaledPercent) / scaledHundred;
367
+ const resultMinor = roundScaledToMinor(rawResult, money.#currency.decimalPlaces, rounding);
368
+ assertSafeResult(resultMinor);
369
+ return new Money(resultMinor, money.#currency);
370
+ }
371
+ percentOf(percent, options) {
372
+ return Money.percentOf(this, percent, options);
373
+ }
374
+ incrementByPercent(percent, options) {
375
+ return Money.incrementByPercent(this, percent, options);
376
+ }
377
+ decrementByPercent(percent, options) {
378
+ return Money.decrementByPercent(this, percent, options);
379
+ }
380
+ toJSON() {
381
+ return {
382
+ amount: this.toMinor(),
383
+ currency: this.#currency.code
384
+ };
385
+ }
386
+ }
387
+ export {
388
+ defineCurrency,
389
+ Money,
390
+ Currency
391
+ };
@@ -0,0 +1,67 @@
1
+ import type { CurrencyDefinition } from "./currency";
2
+ export type RoundingMode = "floor" | "ceil" | "round" | "trunc";
3
+ export interface FromNumberOptions {
4
+ rounding?: RoundingMode;
5
+ strict?: boolean;
6
+ }
7
+ export declare class Money {
8
+ #private;
9
+ private constructor();
10
+ get currency(): CurrencyDefinition;
11
+ static fromNumber(currency: CurrencyDefinition, value: number, options?: FromNumberOptions): Money;
12
+ static fromMinor(currency: CurrencyDefinition, minor: number): Money;
13
+ toNumber(): number;
14
+ toMinor(): number;
15
+ static add(a: Money, b: Money): Money;
16
+ static subtract(a: Money, b: Money): Money;
17
+ static sum(items: Money[]): Money;
18
+ static equals(a: Money, b: Money): boolean;
19
+ static compare(a: Money, b: Money): number;
20
+ static lessThan(a: Money, b: Money): boolean;
21
+ static greaterThan(a: Money, b: Money): boolean;
22
+ static lessThanOrEqual(a: Money, b: Money): boolean;
23
+ static greaterThanOrEqual(a: Money, b: Money): boolean;
24
+ add(other: Money): Money;
25
+ subtract(other: Money): Money;
26
+ equals(other: Money): boolean;
27
+ compare(other: Money): number;
28
+ lessThan(other: Money): boolean;
29
+ greaterThan(other: Money): boolean;
30
+ lessThanOrEqual(other: Money): boolean;
31
+ greaterThanOrEqual(other: Money): boolean;
32
+ isZero(): boolean;
33
+ isPositive(): boolean;
34
+ isNegative(): boolean;
35
+ static allocate(money: Money, parts: number): Money[];
36
+ allocate(parts: number): Money[];
37
+ static convert(money: Money, targetCurrency: CurrencyDefinition, rate: number, options?: {
38
+ rounding?: RoundingMode;
39
+ }): Money;
40
+ convert(targetCurrency: CurrencyDefinition, rate: number, options?: {
41
+ rounding?: RoundingMode;
42
+ }): Money;
43
+ static roundTo(money: Money, increment: number, mode?: RoundingMode): Money;
44
+ round(increment: number, mode?: RoundingMode): Money;
45
+ static percentOf(money: Money, percent: number, options?: {
46
+ rounding?: RoundingMode;
47
+ }): Money;
48
+ static incrementByPercent(money: Money, percent: number, options?: {
49
+ rounding?: RoundingMode;
50
+ }): Money;
51
+ static decrementByPercent(money: Money, percent: number, options?: {
52
+ rounding?: RoundingMode;
53
+ }): Money;
54
+ percentOf(percent: number, options?: {
55
+ rounding?: RoundingMode;
56
+ }): Money;
57
+ incrementByPercent(percent: number, options?: {
58
+ rounding?: RoundingMode;
59
+ }): Money;
60
+ decrementByPercent(percent: number, options?: {
61
+ rounding?: RoundingMode;
62
+ }): Money;
63
+ toJSON(): {
64
+ amount: number;
65
+ currency: string;
66
+ };
67
+ }
package/package.json ADDED
@@ -0,0 +1,47 @@
1
+ {
2
+ "name": "@storepass/money",
3
+ "version": "0.0.1-alpha.5",
4
+ "description": "Money primitive library",
5
+ "main": "./dist/index.cjs",
6
+ "module": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "import": {
11
+ "types": "./dist/index.d.ts",
12
+ "default": "./dist/index.js"
13
+ },
14
+ "require": {
15
+ "types": "./dist/index.d.ts",
16
+ "default": "./dist/index.cjs"
17
+ }
18
+ }
19
+ },
20
+ "files": [
21
+ "dist"
22
+ ],
23
+ "scripts": {
24
+ "build": "bun run build:esm && bun run build:cjs && bun run build:types",
25
+ "build:esm": "bun build ./src/index.ts --outfile ./dist/index.js --format esm --target node",
26
+ "build:cjs": "bun build ./src/index.ts --outfile ./dist/index.cjs --format cjs --target node",
27
+ "build:types": "tsc -p tsconfig.build.json",
28
+ "test": "bun test",
29
+ "typecheck": "tsc --noEmit",
30
+ "check": "biome check --write .",
31
+ "format": "biome format --write .",
32
+ "lint": "biome lint .",
33
+ "lint:fix": "biome lint --write ."
34
+ },
35
+ "engines": {
36
+ "node": ">=16"
37
+ },
38
+ "publishConfig": {
39
+ "registry": "https://registry.npmjs.org",
40
+ "access": "public"
41
+ },
42
+ "devDependencies": {
43
+ "@biomejs/biome": "2.3.11",
44
+ "@types/bun": "latest",
45
+ "typescript": "^5.9.3"
46
+ }
47
+ }