@amboras-dev/money 0.0.0-stage → 0.2.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/README.md +401 -2
- package/dist/ai.cjs +185 -0
- package/dist/ai.d.cts +97 -0
- package/dist/ai.d.ts +97 -0
- package/dist/ai.js +175 -0
- package/dist/allocate-CZRg4TOP.cjs +222 -0
- package/dist/allocate-X7_jAtOj.js +199 -0
- package/dist/amount-BV0O-aN8.cjs +241 -0
- package/dist/amount-BoYBMgK9.js +200 -0
- package/dist/arithmetic-BhNwisjb.js +132 -0
- package/dist/arithmetic-DjT4Kzgt.cjs +233 -0
- package/dist/billing.cjs +184 -0
- package/dist/billing.d.cts +81 -0
- package/dist/billing.d.ts +81 -0
- package/dist/billing.js +175 -0
- package/dist/commerce.cjs +536 -0
- package/dist/commerce.d.cts +293 -0
- package/dist/commerce.d.ts +293 -0
- package/dist/commerce.js +516 -0
- package/dist/format-BdPnijvn.js +99 -0
- package/dist/format-DLhno7eX.cjs +128 -0
- package/dist/fx.cjs +407 -0
- package/dist/fx.d.cts +157 -0
- package/dist/fx.d.ts +157 -0
- package/dist/fx.js +374 -0
- package/dist/index.cjs +352 -0
- package/dist/index.d.cts +300 -0
- package/dist/index.d.ts +300 -0
- package/dist/index.js +276 -0
- package/dist/input.cjs +163 -0
- package/dist/input.d.cts +203 -0
- package/dist/input.d.ts +203 -0
- package/dist/input.js +153 -0
- package/dist/iso-minor-CqkWDxHS.js +48 -0
- package/dist/iso-minor-wSwJN57T.cjs +65 -0
- package/dist/medusa-4K8N1pUc.d.cts +87 -0
- package/dist/medusa-DBXJXXvF.d.ts +87 -0
- package/dist/medusa-DqJY1qpj.js +116 -0
- package/dist/medusa-lRY9xlsf.cjs +175 -0
- package/dist/money-B-3aFjNJ.d.cts +248 -0
- package/dist/money-B-3aFjNJ.d.ts +248 -0
- package/dist/money-B3wH_Z4S.cjs +3990 -0
- package/dist/money-WhnGiI00.js +3583 -0
- package/dist/number-text-BWu67jUq.cjs +145 -0
- package/dist/number-text-DE-q4jO5.js +134 -0
- package/dist/own-D7544D3k.js +4 -0
- package/dist/own-oP9DXFWv.cjs +9 -0
- package/dist/percent-C0InC2t2.cjs +43 -0
- package/dist/percent-D1VB9xXG.js +20 -0
- package/dist/rate-CVeMii4a.cjs +217 -0
- package/dist/rate-DBPiYgT1.d.ts +63 -0
- package/dist/rate-DicStBue.js +152 -0
- package/dist/rate-tqK8ijG0.d.cts +63 -0
- package/dist/reporting-CG-ai2sM.d.cts +31 -0
- package/dist/reporting-tpGJbqYp.d.ts +31 -0
- package/dist/retail-1mu888J1.js +69 -0
- package/dist/retail-C_BPLvMj.d.cts +31 -0
- package/dist/retail-Cml8YSKg.cjs +86 -0
- package/dist/retail-DRUTTQoJ.d.ts +31 -0
- package/dist/rounding-DHBtNCHG.cjs +79 -0
- package/dist/rounding-g9rvye0k.js +50 -0
- package/dist/sql.cjs +290 -0
- package/dist/sql.d.cts +147 -0
- package/dist/sql.d.ts +147 -0
- package/dist/sql.js +271 -0
- package/dist/testing.cjs +61995 -0
- package/dist/testing.d.cts +125 -0
- package/dist/testing.d.ts +125 -0
- package/dist/testing.js +61986 -0
- package/dist/wire-Bhuut8cW.cjs +2097 -0
- package/dist/wire-CaUN9Vf_.d.ts +309 -0
- package/dist/wire-sW0yzqth.js +1888 -0
- package/dist/wire-xjYXv4go.d.cts +309 -0
- package/dist/wire.cjs +37 -0
- package/dist/wire.d.cts +2 -0
- package/dist/wire.d.ts +2 -0
- package/dist/wire.js +2 -0
- package/package.json +158 -3
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,300 @@
|
|
|
1
|
+
import { A as isCurrencyCode, C as currencyInfo, D as currencySymbol, E as currencyOptions, F as sameCurrency, I as storageDigits, L as storageScale, M as medusaCurrencyCode, N as medusaEpsilon, O as displayDigits, P as parseCurrencyCode, R as DecimalText, S as currencyCode, T as currencyName, _ as CurrencyCodeOptions, a as ReadStoredOptions, b as FractionDigits, c as ToMajorDecimalOptions, d as money, f as readStored, g as CurrencyCode, h as CURRENCIES, i as Money, j as localeForCurrency, k as firstCurrency, l as fromMajor, m as toMajorNumber, n as FromMajorOptions, o as StoredAmount, p as toMajorDecimal, r as MajorDecimal, s as StoredAmountOptions, t as FractionalMoney, u as isStoredAmount, v as CurrencyInfo, w as currencyLabel, x as StorageScale, y as CurrencyStatus, z as RoundingMode } from "./money-B-3aFjNJ.js";
|
|
2
|
+
import { a as isPositiveAmount, c as readCalculatedPrice, d as readStoredLiteral, f as toMedusaAmountInput, i as ReadStoredDecimalOptions, l as readStoredDecimal, n as MoneyByCurrency, o as moneyByCurrency, p as toMedusaRaw, r as PriceLike, s as moneyFor, t as MedusaRaw, u as readStoredField } from "./medusa-DBXJXXvF.js";
|
|
3
|
+
//#region src/allocate.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Where the rounding remainder goes:
|
|
6
|
+
* - `'largest-remainder'` (default; promotion parity engine): floor every share, then +1 unit
|
|
7
|
+
* to the largest fractional parts, ties to the lower index.
|
|
8
|
+
* - `'largest-weight'` (subscription offers): every share but the first largest-weight one is
|
|
9
|
+
* rounded half-up; that one absorbs the rest. A deficit it cannot absorb is taken from the
|
|
10
|
+
* others in descending weight order, so the sum stays exact and no share goes negative.
|
|
11
|
+
* - `'last'` (Shopify archive, inventory extra costs): floor every share; the last positive
|
|
12
|
+
* weight absorbs the remainder.
|
|
13
|
+
* - `'last-first'` (draft discounts): shares rounded half-up and capped at `caps`, the target
|
|
14
|
+
* being `min(total, sum(caps))`; the remainder then moves from the last share backwards,
|
|
15
|
+
* each kept within `[0, cap]`.
|
|
16
|
+
* - `'cumulative'` (cash on delivery): each share is the difference of the rounded running
|
|
17
|
+
* pro-rata total.
|
|
18
|
+
*/
|
|
19
|
+
type AllocationStrategy = 'largest-remainder' | 'largest-weight' | 'last' | 'last-first' | 'cumulative';
|
|
20
|
+
type AllocationWeight = number | DecimalText | Money;
|
|
21
|
+
interface AllocateOptions {
|
|
22
|
+
/** Default `'largest-remainder'`. */
|
|
23
|
+
strategy?: AllocationStrategy;
|
|
24
|
+
/** The unit shares are whole multiples of: `'storage'` (default), `'currency'`, or `{ step }` storage units (COD whole dong: 100). */
|
|
25
|
+
unit?: 'storage' | 'currency' | {
|
|
26
|
+
step: number;
|
|
27
|
+
};
|
|
28
|
+
/** Per-share caps for `'last-first'`; default: the weights, when they are Money. */
|
|
29
|
+
caps?: readonly Money[];
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* `total` split in proportion to `weights` so the shares add up to it exactly (see
|
|
33
|
+
* `AllocationStrategy` for where the remainder goes). Shares are whole `unit`s; `total`
|
|
34
|
+
* must be one (else `precision`: round it with `roundToUnit` first). Weights must be
|
|
35
|
+
* non-negative (else `amount_invalid`); all-zero weights give all-zero shares and need a
|
|
36
|
+
* zero total. A negative total is allocated by magnitude and negated.
|
|
37
|
+
*
|
|
38
|
+
* The DecimalText overload splits exact decimals at `scale` fraction digits (inventory extra
|
|
39
|
+
* costs at 4, sub-cent USD at 6); it has no caps, so `'last-first'` is Money-only.
|
|
40
|
+
*/
|
|
41
|
+
export declare function allocate(total: Money, weights: readonly AllocationWeight[], options?: AllocateOptions): Money[];
|
|
42
|
+
export declare function allocate(total: DecimalText, weights: readonly AllocationWeight[], options: Omit<AllocateOptions, 'unit' | 'caps'> & {
|
|
43
|
+
scale: number;
|
|
44
|
+
}): DecimalText[];
|
|
45
|
+
interface WaterfallResult {
|
|
46
|
+
readonly allocations: Money[];
|
|
47
|
+
readonly shortfall: Money;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Greedy capped allocation in the given order: `take_i = min(left, cap_i)`; `shortfall` is
|
|
51
|
+
* what could not be placed. With `allOrNothing`, a shortfall allocates nothing (all zeros,
|
|
52
|
+
* `shortfall = amount`). Negative caps or amount throw `amount_invalid`; all one currency.
|
|
53
|
+
*/
|
|
54
|
+
export declare function allocateWaterfall(caps: readonly Money[], amount: Money, options?: {
|
|
55
|
+
allOrNothing?: boolean;
|
|
56
|
+
}): WaterfallResult;
|
|
57
|
+
interface UnitPricePart {
|
|
58
|
+
readonly quantity: number;
|
|
59
|
+
readonly unitPrice: Money;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* A line total charged at one unit price, or split in two parts one `unit` apart when it does
|
|
63
|
+
* not divide by `quantity`, so `sum(quantity x unitPrice) === net` exactly (JPY 100000 x 3
|
|
64
|
+
* gives `2 @ 33333`, `1 @ 33334`). Zero-quantity parts are omitted. `quantity` must be a
|
|
65
|
+
* positive integer; `net` a whole `unit` (else `precision`).
|
|
66
|
+
*/
|
|
67
|
+
export declare function splitUnitPrice(net: Money, quantity: number, options?: {
|
|
68
|
+
unit?: 'storage' | 'currency';
|
|
69
|
+
}): UnitPricePart[];
|
|
70
|
+
//#endregion
|
|
71
|
+
//#region src/rounding.d.ts
|
|
72
|
+
/**
|
|
73
|
+
* - `'storage'`: one storage unit (USD 1 cent, JPY 1/100 yen).
|
|
74
|
+
* - `'currency'`: the smallest unit the currency stores, 10^-storageDigits major (JPY 1 yen
|
|
75
|
+
* = 100 storage units, USD and IDR 1 cent = 1, KWD and IQD 1 fils = 1). Keyed on storage
|
|
76
|
+
* digits, as Medusa `roundToDisplayUnit` is, so stored prices and checkout agree.
|
|
77
|
+
* - `'major'`: one whole major unit (USD 100, JPY 100, KWD 1000).
|
|
78
|
+
*/
|
|
79
|
+
type AmountUnit = 'storage' | 'currency' | 'major';
|
|
80
|
+
interface RoundToUnitOptions {
|
|
81
|
+
/** Default `'storage'`. */
|
|
82
|
+
unit?: AmountUnit;
|
|
83
|
+
/** Default `'half-up'`. */
|
|
84
|
+
rounding?: RoundingMode;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* A stored amount (possibly fractional: a percentage of a price, a pro-rated fee) rounded to
|
|
88
|
+
* a whole `unit` with `rounding`, as Money. Replaces `roundToStorageUnit`,
|
|
89
|
+
* `roundToDisplayUnit` (`unit: 'currency'`), `Math.floor(round2(x))` (`rounding: 'floor'`).
|
|
90
|
+
* A Money input must be in `currency`; a number must be finite; text is plain decimal.
|
|
91
|
+
*/
|
|
92
|
+
export declare function roundToUnit(value: Money | DecimalText | number, currency: string, options?: RoundToUnitOptions): Money;
|
|
93
|
+
/**
|
|
94
|
+
* A major-unit number rounded to the currency's display digits (default) or storage digits,
|
|
95
|
+
* half-up by default, as a number. For report outputs that already are majors; new code
|
|
96
|
+
* keeps Money and converts once.
|
|
97
|
+
*/
|
|
98
|
+
export declare function roundMajor(major: number | DecimalText, currency: string, options?: {
|
|
99
|
+
digits?: 'display' | 'storage';
|
|
100
|
+
rounding?: RoundingMode;
|
|
101
|
+
}): number;
|
|
102
|
+
//#endregion
|
|
103
|
+
//#region src/arithmetic.d.ts
|
|
104
|
+
/** An exact ratio of integers (`{ numerator: 1, denominator: 3 }`). */
|
|
105
|
+
interface Ratio {
|
|
106
|
+
readonly numerator: number | bigint;
|
|
107
|
+
readonly denominator: number | bigint;
|
|
108
|
+
}
|
|
109
|
+
export declare function add(a: Money, b: Money): Money;
|
|
110
|
+
export declare function subtract(a: Money, b: Money): Money;
|
|
111
|
+
/** The exact total of `items`, every one in `currency` (required, so an empty list has one). */
|
|
112
|
+
export declare function sum(items: readonly Money[], currency: string): Money;
|
|
113
|
+
/**
|
|
114
|
+
* `m x factor`, rounded once to a whole `unit` (default `'storage'`) with `rounding`
|
|
115
|
+
* (default half-up). A number is taken by its shortest decimal text (as JSON carries it);
|
|
116
|
+
* text is exact decimal; a Ratio is exact. Integer factors are exact. A non-finite factor or
|
|
117
|
+
* a zero denominator throws `amount_invalid`.
|
|
118
|
+
*/
|
|
119
|
+
export declare function multiply(m: Money, factor: number | DecimalText | Ratio, options?: RoundToUnitOptions): Money;
|
|
120
|
+
/** `m / divisor`, rounded once like `multiply`. Divisor 0 throws `amount_invalid`. */
|
|
121
|
+
export declare function divide(m: Money, divisor: number | DecimalText, options?: RoundToUnitOptions): Money;
|
|
122
|
+
/** `percent` percent of `m` (12.5 is an eighth), rounded once like `multiply` (default unit `'storage'`). */
|
|
123
|
+
export declare function percentOf(m: Money, percent: number | DecimalText, options?: RoundToUnitOptions): Money;
|
|
124
|
+
/** `-1`, `0` or `1`. Different currencies throw `currency_mismatch`. */
|
|
125
|
+
export declare function compare(a: Money, b: Money): -1 | 0 | 1;
|
|
126
|
+
/** Same currency and same amount. Different currencies are `false`, never a throw. */
|
|
127
|
+
export declare function equals(a: Money, b: Money): boolean;
|
|
128
|
+
export declare function isZero(m: Money): boolean;
|
|
129
|
+
export declare function isPositive(m: Money): boolean;
|
|
130
|
+
export declare function isNegative(m: Money): boolean;
|
|
131
|
+
/** The smallest of the amounts, all in one currency (the first of equals). */
|
|
132
|
+
export declare function min(first: Money, ...rest: Money[]): Money;
|
|
133
|
+
/** The largest of the amounts, all in one currency (the first of equals). */
|
|
134
|
+
export declare function max(first: Money, ...rest: Money[]): Money;
|
|
135
|
+
/** `m` limited to `[min, max]` (either optional, same currency). `min` above `max` throws `amount_invalid`. */
|
|
136
|
+
export declare function clamp(m: Money, bounds: {
|
|
137
|
+
min?: Money;
|
|
138
|
+
max?: Money;
|
|
139
|
+
}): Money;
|
|
140
|
+
/** What is left: `max(0, total - used)`. */
|
|
141
|
+
export declare function remaining(total: Money, used: Money): Money;
|
|
142
|
+
/** `|a - b| <= tolerance` (`<` with `inclusive: false`), all one currency. A negative tolerance throws `amount_invalid`. */
|
|
143
|
+
export declare function within(a: Money, b: Money, tolerance: Money, options?: {
|
|
144
|
+
inclusive?: boolean;
|
|
145
|
+
}): boolean;
|
|
146
|
+
/** `d = a - b`; `d` below `tolerance` counts as rounding and gives 0, else `d` (Stripe `refundSyncDelta`). */
|
|
147
|
+
export declare function excessOver(a: Money, b: Money, tolerance: Money): Money;
|
|
148
|
+
//#endregion
|
|
149
|
+
//#region src/country.d.ts
|
|
150
|
+
/** The current legal tender of an ISO 3166 country (`'US'` or `'us'`), or null when unknown. Never guesses USD. */
|
|
151
|
+
export declare function defaultCurrencyForCountry(country: string): CurrencyCode | null;
|
|
152
|
+
//#endregion
|
|
153
|
+
//#region src/decimal-text.d.ts
|
|
154
|
+
/** Anything the family takes as a value. */
|
|
155
|
+
type DecimalInput = DecimalText | string | number | bigint;
|
|
156
|
+
/** Canonical DecimalText of `input` (`'4300.00'` gives `'4300'`, `0.1 + 0.2` gives `'0.30000000000000004'`). See `decimalOf` for what is refused. */
|
|
157
|
+
export declare function parseDecimalText(input: unknown, options?: {
|
|
158
|
+
signed?: boolean;
|
|
159
|
+
}): DecimalText;
|
|
160
|
+
/**
|
|
161
|
+
* THE storage-literal grammar: `parseDecimal`'s `'plain'` text (decimal, optional fraction,
|
|
162
|
+
* no exponent, at most `MAX_DIGITS` characters). `signed` (default false) allows a leading
|
|
163
|
+
* `-`; `leadingZeros: false` refuses `'04300'`.
|
|
164
|
+
* `{ leadingZeros: false }` is AMOUNT_COLUMN_LITERAL, `{}` STORAGE_AMOUNT_LITERAL,
|
|
165
|
+
* `{ signed: true }` the oracle / iyzico / Authorize.Net grammar.
|
|
166
|
+
*/
|
|
167
|
+
export declare function isDecimalLiteral(text: unknown, options?: {
|
|
168
|
+
signed?: boolean;
|
|
169
|
+
leadingZeros?: boolean;
|
|
170
|
+
}): boolean;
|
|
171
|
+
/** Throws `amount_invalid` unless `value` is an unsigned decimal literal without leading zeros (the seam ledger rule). */
|
|
172
|
+
export declare function assertDecimalLiteral(value: unknown, label: string): asserts value is string;
|
|
173
|
+
/** Drops trailing fraction zeros and a trailing `.` (`'12.500'` gives `'12.5'`, `'12.00'` gives `'12'`). Text without a `.` is unchanged. */
|
|
174
|
+
export declare function trimDecimalZeros(text: string): string;
|
|
175
|
+
/** The exact sum (`[]` gives `'0'`). */
|
|
176
|
+
export declare function decimalSum(values: readonly DecimalInput[]): DecimalText;
|
|
177
|
+
/** `a - b`, exactly. */
|
|
178
|
+
export declare function decimalSubtract(a: DecimalText | string, b: DecimalText | string): DecimalText;
|
|
179
|
+
/** `a x b`, exactly. */
|
|
180
|
+
export declare function decimalMultiply(a: DecimalText | string | number, b: DecimalText | string | number): DecimalText;
|
|
181
|
+
/** `a / b` rounded once to `scale` fraction digits (`rounding` default half-up). `b` = 0 throws `amount_invalid`. */
|
|
182
|
+
export declare function decimalDivide(a: DecimalText | string | number, b: DecimalText | string | number, options: {
|
|
183
|
+
scale: number;
|
|
184
|
+
rounding?: RoundingMode;
|
|
185
|
+
}): DecimalText;
|
|
186
|
+
/** `a` rounded to `scale` fraction digits (`rounding` default half-up). */
|
|
187
|
+
export declare function decimalRound(a: DecimalText | string | number, options: {
|
|
188
|
+
scale: number;
|
|
189
|
+
rounding?: RoundingMode;
|
|
190
|
+
}): DecimalText;
|
|
191
|
+
/** `-1`, `0` or `1` as `a` is below, equal to or above `b`. */
|
|
192
|
+
export declare function decimalCompare(a: DecimalText | string, b: DecimalText | string): -1 | 0 | 1;
|
|
193
|
+
/**
|
|
194
|
+
* Whether `a` and `b` are the same decimal, exactly (`'43.0'` equals `'43.00'` and `43`). Takes what
|
|
195
|
+
* `decimalCompare` takes, but never throws: anything that is not plain decimal text, a finite number
|
|
196
|
+
* or a bigint is never equal, not even to itself.
|
|
197
|
+
*/
|
|
198
|
+
export declare function decimalEquals(a: unknown, b: unknown): boolean;
|
|
199
|
+
/** `a` limited to `[min, max]` (either bound optional). `min` above `max` throws `amount_invalid`. */
|
|
200
|
+
export declare function decimalClamp(a: DecimalText | string, bounds: {
|
|
201
|
+
min?: DecimalText | string;
|
|
202
|
+
max?: DecimalText | string;
|
|
203
|
+
}): DecimalText;
|
|
204
|
+
/** `|a - b| <= tolerance` (`<` with `inclusive: false`). A negative tolerance throws `amount_invalid`. */
|
|
205
|
+
export declare function decimalWithin(a: DecimalText | string, b: DecimalText | string, tolerance: DecimalText | string, options?: {
|
|
206
|
+
inclusive?: boolean;
|
|
207
|
+
}): boolean;
|
|
208
|
+
//#endregion
|
|
209
|
+
//#region src/errors.d.ts
|
|
210
|
+
/**
|
|
211
|
+
* Why a money operation refused its input:
|
|
212
|
+
* - `currency_invalid`: not a currency this package knows (or a retired one where only active codes are allowed)
|
|
213
|
+
* - `currency_mismatch`: two amounts in different currencies were combined
|
|
214
|
+
* - `amount_invalid`: not a finite number, or a stored amount that is not a safe integer
|
|
215
|
+
* - `precision`: more fraction digits than the currency stores (19.999 USD, 5.5 JPY)
|
|
216
|
+
* - `overflow`: too large to store exactly
|
|
217
|
+
* - `parse`: text that is not a decimal number
|
|
218
|
+
* - `currency_missing`: no candidate currency at all (every candidate was empty)
|
|
219
|
+
* - `rate_invalid`: an exchange rate that is non-positive, not an integer ratio, or out of bounds
|
|
220
|
+
* - `wire_unsupported`: the payment provider or destination does not take this currency
|
|
221
|
+
* - `wire_refused`: the amount cannot be sent to the provider (non-positive, a fraction of a whole unit, rounds to nothing)
|
|
222
|
+
*/
|
|
223
|
+
type MoneyErrorCode = 'currency_invalid' | 'currency_mismatch' | 'amount_invalid' | 'precision' | 'overflow' | 'parse' | 'currency_missing' | 'rate_invalid' | 'wire_unsupported' | 'wire_refused';
|
|
224
|
+
interface MoneyErrorOptions {
|
|
225
|
+
/** The input field the value came from; prefixed to the message. */
|
|
226
|
+
field?: string;
|
|
227
|
+
}
|
|
228
|
+
/** The one error this package throws for bad money input. Check `code`, not the message. */
|
|
229
|
+
export declare class MoneyError extends Error {
|
|
230
|
+
readonly code: MoneyErrorCode;
|
|
231
|
+
readonly field?: string;
|
|
232
|
+
constructor(code: MoneyErrorCode, message: string, options?: MoneyErrorOptions);
|
|
233
|
+
}
|
|
234
|
+
//#endregion
|
|
235
|
+
//#region src/format.d.ts
|
|
236
|
+
interface FormatMoneyOptions {
|
|
237
|
+
/** Default `'en-US'`; an unsupported or malformed tag falls back to it. */
|
|
238
|
+
locale?: string;
|
|
239
|
+
/** Default `'symbol'`. `'symbol+code'` appends the code unless the output already shows it. */
|
|
240
|
+
currencyDisplay?: 'symbol' | 'narrowSymbol' | 'code' | 'symbol+code';
|
|
241
|
+
/**
|
|
242
|
+
* `'display'` (default): the display digits, half-up. `'widen'`: at least the display
|
|
243
|
+
* digits, more when the stored value needs them (JPY 550 shows `¥5.5`). `'trim-zero'`:
|
|
244
|
+
* no fraction for a whole number of major units, else the display digits.
|
|
245
|
+
*/
|
|
246
|
+
digits?: 'display' | 'widen' | 'trim-zero';
|
|
247
|
+
/** Default `'auto'`. */
|
|
248
|
+
signDisplay?: 'auto' | 'always' | 'exceptZero';
|
|
249
|
+
/** Default `'-'`; `'−'` (U+2212) replaces the leading ASCII minus. */
|
|
250
|
+
minusSign?: '-' | '−';
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
* Money for people: `'$1,234.50'`, `'¥1,200'`, `'KWD 1.005'`, `'-€5.00'`. Locale, currency
|
|
254
|
+
* display, digits and sign are explicit options; the active app locale is the caller's to
|
|
255
|
+
* pass. If Intl throws, `'<CODE> <decimal>'`.
|
|
256
|
+
*/
|
|
257
|
+
export declare function formatMoney(m: Money, options?: FormatMoneyOptions): string;
|
|
258
|
+
/**
|
|
259
|
+
* A stored amount that may be fractional (a Medusa total, 107.8175; a percentage promotion) for
|
|
260
|
+
* people: rounded half-up to a whole storage unit (`roundToUnit`), then `formatMoney` with `options`.
|
|
261
|
+
* A number must be finite; text is plain decimal; an unknown currency throws `currency_invalid`.
|
|
262
|
+
*/
|
|
263
|
+
export declare function formatStored(amount: number | DecimalText, currency: string, options?: FormatMoneyOptions): string;
|
|
264
|
+
/**
|
|
265
|
+
* Like `formatMoney` below `threshold` major units (default 10000, by magnitude); at or above
|
|
266
|
+
* it, compact short notation with `maximumSignificantDigits` (default 3): `'$12.3K'`.
|
|
267
|
+
*/
|
|
268
|
+
export declare function formatMoneyCompact(m: Money, options?: FormatMoneyOptions & {
|
|
269
|
+
threshold?: number;
|
|
270
|
+
maximumSignificantDigits?: number;
|
|
271
|
+
}): string;
|
|
272
|
+
/** `'$10.00 – $20.00'` (en dash between spaces), `'$10.00+'` when `high` is null; zero amounts show `zeroLabel` when given. */
|
|
273
|
+
export declare function formatMoneyRange(low: Money, high: Money | null, options?: FormatMoneyOptions & {
|
|
274
|
+
zeroLabel?: string;
|
|
275
|
+
}): string;
|
|
276
|
+
/**
|
|
277
|
+
* A chart tick formatter for values that already are major units: always compact, with the
|
|
278
|
+
* same rounding as `formatMoneyCompact` (`1234567` gives `'$1.23M'`).
|
|
279
|
+
*/
|
|
280
|
+
export declare function moneyAxisFormatter(currency: string, options?: Pick<FormatMoneyOptions, 'locale' | 'currencyDisplay'>): (major: number) => string;
|
|
281
|
+
//#endregion
|
|
282
|
+
//#region src/iso-minor.d.ts
|
|
283
|
+
/**
|
|
284
|
+
* ISO minor units at the display digits: `round(major x 10^displayDigits)` with `rounding`
|
|
285
|
+
* (default half-up). JPY 150000 gives 1500, KWD 1005 gives 1005, IQD 10000 gives 10, HUF
|
|
286
|
+
* 435050 gives 4351. Not for payment providers (use `/wire`).
|
|
287
|
+
*/
|
|
288
|
+
export declare function toIsoMinor(m: Money, options?: {
|
|
289
|
+
rounding?: RoundingMode;
|
|
290
|
+
}): number;
|
|
291
|
+
/**
|
|
292
|
+
* ISO minor units at the display digits (or `digits`) as Money. The value must be a safe
|
|
293
|
+
* integer (number or integer text). With `digits` above the storage exponent the value must
|
|
294
|
+
* land on a whole storage unit, else `precision`.
|
|
295
|
+
*/
|
|
296
|
+
export declare function fromIsoMinor(value: number | string, currency: string, options?: {
|
|
297
|
+
digits?: FractionDigits;
|
|
298
|
+
}): Money;
|
|
299
|
+
//#endregion
|
|
300
|
+
export { type AllocateOptions, type AllocationStrategy, type AllocationWeight, type AmountUnit, CURRENCIES, type CurrencyCode, type CurrencyCodeOptions, type CurrencyInfo, type CurrencyStatus, type DecimalInput, type DecimalText, type FormatMoneyOptions, type FractionDigits, type FractionalMoney, type FromMajorOptions, type MajorDecimal, type MedusaRaw, type Money, type MoneyByCurrency, type MoneyErrorCode, type MoneyErrorOptions, type PriceLike, type Ratio, type ReadStoredDecimalOptions, type ReadStoredOptions, type RoundToUnitOptions, type RoundingMode, type StorageScale, type StoredAmount, type StoredAmountOptions, type ToMajorDecimalOptions, type UnitPricePart, type WaterfallResult, currencyCode, currencyInfo, currencyLabel, currencyName, currencyOptions, currencySymbol, displayDigits, firstCurrency, fromMajor, isCurrencyCode, isPositiveAmount, isStoredAmount, localeForCurrency, medusaCurrencyCode, medusaEpsilon, money, moneyByCurrency, moneyFor, parseCurrencyCode, readCalculatedPrice, readStored, readStoredDecimal, readStoredField, readStoredLiteral, sameCurrency, storageDigits, storageScale, toMajorDecimal, toMajorNumber, toMedusaAmountInput, toMedusaRaw };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
import { A as storageScale, C as medusaCurrencyCode, D as sameCurrency, F as decimalClamp, G as isDecimalLiteral, H as decimalSubtract, I as decimalCompare, K as parseDecimalText, L as decimalDivide, O as storageDigits, P as assertDecimalLiteral, Q as MoneyError, R as decimalEquals, S as localeForCurrency, T as parseCurrencyCode, U as decimalSum, V as decimalRound, W as decimalWithin, _ as currencySymbol, a as money, b as firstCurrency, c as toMajorDecimal, d as currencyCode, f as currencyInfo, g as currencyOptions, h as currencyName, l as toMajorNumber, n as fromMajor, o as readStored, p as currencyLabel, q as trimDecimalZeros, r as isStoredAmount, u as CURRENCIES, v as displayDigits, w as medusaEpsilon, x as isCurrencyCode, z as decimalMultiply } from "./money-WhnGiI00.js";
|
|
2
|
+
import { i as roundToUnit, n as roundMajor } from "./rounding-g9rvye0k.js";
|
|
3
|
+
import { i as splitUnitPrice, r as allocateWaterfall, t as allocate } from "./allocate-X7_jAtOj.js";
|
|
4
|
+
import { _ as within, a as equals, c as isPositive, d as min, f as multiply, g as sum, h as subtract, i as divide, l as isZero, m as remaining, n as clamp, o as excessOver, p as percentOf, r as compare, s as isNegative, t as add, u as max } from "./arithmetic-BhNwisjb.js";
|
|
5
|
+
import { t as hasOwnKey } from "./own-D7544D3k.js";
|
|
6
|
+
import { a as moneyAxisFormatter, i as formatStored, n as formatMoneyCompact, r as formatMoneyRange, t as formatMoney } from "./format-BdPnijvn.js";
|
|
7
|
+
import { r as toIsoMinor, t as fromIsoMinor } from "./iso-minor-CqkWDxHS.js";
|
|
8
|
+
import { a as readStoredDecimal, i as readCalculatedPrice, l as toMedusaAmountInput, n as moneyByCurrency, o as readStoredField, r as moneyFor, s as readStoredLiteral, t as isPositiveAmount, u as toMedusaRaw } from "./medusa-DqJY1qpj.js";
|
|
9
|
+
//#region src/data/countries.ts
|
|
10
|
+
const COUNTRY_CURRENCY = {
|
|
11
|
+
AC: "SHP",
|
|
12
|
+
AD: "EUR",
|
|
13
|
+
AE: "AED",
|
|
14
|
+
AF: "AFN",
|
|
15
|
+
AG: "XCD",
|
|
16
|
+
AI: "XCD",
|
|
17
|
+
AL: "ALL",
|
|
18
|
+
AM: "AMD",
|
|
19
|
+
AO: "AOA",
|
|
20
|
+
AR: "ARS",
|
|
21
|
+
AS: "USD",
|
|
22
|
+
AT: "EUR",
|
|
23
|
+
AU: "AUD",
|
|
24
|
+
AW: "AWG",
|
|
25
|
+
AX: "EUR",
|
|
26
|
+
AZ: "AZN",
|
|
27
|
+
BA: "BAM",
|
|
28
|
+
BB: "BBD",
|
|
29
|
+
BD: "BDT",
|
|
30
|
+
BE: "EUR",
|
|
31
|
+
BF: "XOF",
|
|
32
|
+
BG: "EUR",
|
|
33
|
+
BH: "BHD",
|
|
34
|
+
BI: "BIF",
|
|
35
|
+
BJ: "XOF",
|
|
36
|
+
BL: "EUR",
|
|
37
|
+
BM: "BMD",
|
|
38
|
+
BN: "BND",
|
|
39
|
+
BO: "BOB",
|
|
40
|
+
BQ: "USD",
|
|
41
|
+
BR: "BRL",
|
|
42
|
+
BS: "BSD",
|
|
43
|
+
BT: "BTN",
|
|
44
|
+
BV: "NOK",
|
|
45
|
+
BW: "BWP",
|
|
46
|
+
BY: "BYN",
|
|
47
|
+
BZ: "BZD",
|
|
48
|
+
CA: "CAD",
|
|
49
|
+
CC: "AUD",
|
|
50
|
+
CD: "CDF",
|
|
51
|
+
CF: "XAF",
|
|
52
|
+
CG: "XAF",
|
|
53
|
+
CH: "CHF",
|
|
54
|
+
CI: "XOF",
|
|
55
|
+
CK: "NZD",
|
|
56
|
+
CL: "CLP",
|
|
57
|
+
CM: "XAF",
|
|
58
|
+
CN: "CNY",
|
|
59
|
+
CO: "COP",
|
|
60
|
+
CR: "CRC",
|
|
61
|
+
CU: "CUP",
|
|
62
|
+
CV: "CVE",
|
|
63
|
+
CW: "XCG",
|
|
64
|
+
CX: "AUD",
|
|
65
|
+
CY: "EUR",
|
|
66
|
+
CZ: "CZK",
|
|
67
|
+
DE: "EUR",
|
|
68
|
+
DG: "USD",
|
|
69
|
+
DJ: "DJF",
|
|
70
|
+
DK: "DKK",
|
|
71
|
+
DM: "XCD",
|
|
72
|
+
DO: "DOP",
|
|
73
|
+
DZ: "DZD",
|
|
74
|
+
EA: "EUR",
|
|
75
|
+
EC: "USD",
|
|
76
|
+
EE: "EUR",
|
|
77
|
+
EG: "EGP",
|
|
78
|
+
EH: "MAD",
|
|
79
|
+
ER: "ERN",
|
|
80
|
+
ES: "EUR",
|
|
81
|
+
ET: "ETB",
|
|
82
|
+
EU: "EUR",
|
|
83
|
+
FI: "EUR",
|
|
84
|
+
FJ: "FJD",
|
|
85
|
+
FK: "FKP",
|
|
86
|
+
FM: "USD",
|
|
87
|
+
FO: "DKK",
|
|
88
|
+
FR: "EUR",
|
|
89
|
+
GA: "XAF",
|
|
90
|
+
GB: "GBP",
|
|
91
|
+
GD: "XCD",
|
|
92
|
+
GE: "GEL",
|
|
93
|
+
GF: "EUR",
|
|
94
|
+
GG: "GBP",
|
|
95
|
+
GH: "GHS",
|
|
96
|
+
GI: "GIP",
|
|
97
|
+
GL: "DKK",
|
|
98
|
+
GM: "GMD",
|
|
99
|
+
GN: "GNF",
|
|
100
|
+
GP: "EUR",
|
|
101
|
+
GQ: "XAF",
|
|
102
|
+
GR: "EUR",
|
|
103
|
+
GS: "GBP",
|
|
104
|
+
GT: "GTQ",
|
|
105
|
+
GU: "USD",
|
|
106
|
+
GW: "XOF",
|
|
107
|
+
GY: "GYD",
|
|
108
|
+
HK: "HKD",
|
|
109
|
+
HM: "AUD",
|
|
110
|
+
HN: "HNL",
|
|
111
|
+
HR: "EUR",
|
|
112
|
+
HT: "HTG",
|
|
113
|
+
HU: "HUF",
|
|
114
|
+
IC: "EUR",
|
|
115
|
+
ID: "IDR",
|
|
116
|
+
IE: "EUR",
|
|
117
|
+
IL: "ILS",
|
|
118
|
+
IM: "GBP",
|
|
119
|
+
IN: "INR",
|
|
120
|
+
IO: "USD",
|
|
121
|
+
IQ: "IQD",
|
|
122
|
+
IR: "IRR",
|
|
123
|
+
IS: "ISK",
|
|
124
|
+
IT: "EUR",
|
|
125
|
+
JE: "GBP",
|
|
126
|
+
JM: "JMD",
|
|
127
|
+
JO: "JOD",
|
|
128
|
+
JP: "JPY",
|
|
129
|
+
KE: "KES",
|
|
130
|
+
KG: "KGS",
|
|
131
|
+
KH: "KHR",
|
|
132
|
+
KI: "AUD",
|
|
133
|
+
KM: "KMF",
|
|
134
|
+
KN: "XCD",
|
|
135
|
+
KP: "KPW",
|
|
136
|
+
KR: "KRW",
|
|
137
|
+
KW: "KWD",
|
|
138
|
+
KY: "KYD",
|
|
139
|
+
KZ: "KZT",
|
|
140
|
+
LA: "LAK",
|
|
141
|
+
LB: "LBP",
|
|
142
|
+
LC: "XCD",
|
|
143
|
+
LI: "CHF",
|
|
144
|
+
LK: "LKR",
|
|
145
|
+
LR: "LRD",
|
|
146
|
+
LS: "LSL",
|
|
147
|
+
LT: "EUR",
|
|
148
|
+
LU: "EUR",
|
|
149
|
+
LV: "EUR",
|
|
150
|
+
LY: "LYD",
|
|
151
|
+
MA: "MAD",
|
|
152
|
+
MC: "EUR",
|
|
153
|
+
MD: "MDL",
|
|
154
|
+
ME: "EUR",
|
|
155
|
+
MF: "EUR",
|
|
156
|
+
MG: "MGA",
|
|
157
|
+
MH: "USD",
|
|
158
|
+
MK: "MKD",
|
|
159
|
+
ML: "XOF",
|
|
160
|
+
MM: "MMK",
|
|
161
|
+
MN: "MNT",
|
|
162
|
+
MO: "MOP",
|
|
163
|
+
MP: "USD",
|
|
164
|
+
MQ: "EUR",
|
|
165
|
+
MR: "MRU",
|
|
166
|
+
MS: "XCD",
|
|
167
|
+
MT: "EUR",
|
|
168
|
+
MU: "MUR",
|
|
169
|
+
MV: "MVR",
|
|
170
|
+
MW: "MWK",
|
|
171
|
+
MX: "MXN",
|
|
172
|
+
MY: "MYR",
|
|
173
|
+
MZ: "MZN",
|
|
174
|
+
NA: "NAD",
|
|
175
|
+
NC: "XPF",
|
|
176
|
+
NE: "XOF",
|
|
177
|
+
NF: "AUD",
|
|
178
|
+
NG: "NGN",
|
|
179
|
+
NI: "NIO",
|
|
180
|
+
NL: "EUR",
|
|
181
|
+
NO: "NOK",
|
|
182
|
+
NP: "NPR",
|
|
183
|
+
NR: "AUD",
|
|
184
|
+
NU: "NZD",
|
|
185
|
+
NZ: "NZD",
|
|
186
|
+
OM: "OMR",
|
|
187
|
+
PA: "PAB",
|
|
188
|
+
PE: "PEN",
|
|
189
|
+
PF: "XPF",
|
|
190
|
+
PG: "PGK",
|
|
191
|
+
PH: "PHP",
|
|
192
|
+
PK: "PKR",
|
|
193
|
+
PL: "PLN",
|
|
194
|
+
PM: "EUR",
|
|
195
|
+
PN: "NZD",
|
|
196
|
+
PR: "USD",
|
|
197
|
+
PS: "ILS",
|
|
198
|
+
PT: "EUR",
|
|
199
|
+
PW: "USD",
|
|
200
|
+
PY: "PYG",
|
|
201
|
+
QA: "QAR",
|
|
202
|
+
RE: "EUR",
|
|
203
|
+
RO: "RON",
|
|
204
|
+
RS: "RSD",
|
|
205
|
+
RU: "RUB",
|
|
206
|
+
RW: "RWF",
|
|
207
|
+
SA: "SAR",
|
|
208
|
+
SB: "SBD",
|
|
209
|
+
SC: "SCR",
|
|
210
|
+
SD: "SDG",
|
|
211
|
+
SE: "SEK",
|
|
212
|
+
SG: "SGD",
|
|
213
|
+
SH: "SHP",
|
|
214
|
+
SI: "EUR",
|
|
215
|
+
SJ: "NOK",
|
|
216
|
+
SK: "EUR",
|
|
217
|
+
SL: "SLE",
|
|
218
|
+
SM: "EUR",
|
|
219
|
+
SN: "XOF",
|
|
220
|
+
SO: "SOS",
|
|
221
|
+
SR: "SRD",
|
|
222
|
+
SS: "SSP",
|
|
223
|
+
ST: "STN",
|
|
224
|
+
SV: "USD",
|
|
225
|
+
SX: "XCG",
|
|
226
|
+
SY: "SYP",
|
|
227
|
+
SZ: "SZL",
|
|
228
|
+
TA: "GBP",
|
|
229
|
+
TC: "USD",
|
|
230
|
+
TD: "XAF",
|
|
231
|
+
TF: "EUR",
|
|
232
|
+
TG: "XOF",
|
|
233
|
+
TH: "THB",
|
|
234
|
+
TJ: "TJS",
|
|
235
|
+
TK: "NZD",
|
|
236
|
+
TL: "USD",
|
|
237
|
+
TM: "TMT",
|
|
238
|
+
TN: "TND",
|
|
239
|
+
TO: "TOP",
|
|
240
|
+
TR: "TRY",
|
|
241
|
+
TT: "TTD",
|
|
242
|
+
TV: "AUD",
|
|
243
|
+
TW: "TWD",
|
|
244
|
+
TZ: "TZS",
|
|
245
|
+
UA: "UAH",
|
|
246
|
+
UG: "UGX",
|
|
247
|
+
UM: "USD",
|
|
248
|
+
US: "USD",
|
|
249
|
+
UY: "UYU",
|
|
250
|
+
UZ: "UZS",
|
|
251
|
+
VA: "EUR",
|
|
252
|
+
VC: "XCD",
|
|
253
|
+
VE: "VES",
|
|
254
|
+
VG: "USD",
|
|
255
|
+
VI: "USD",
|
|
256
|
+
VN: "VND",
|
|
257
|
+
VU: "VUV",
|
|
258
|
+
WF: "XPF",
|
|
259
|
+
WS: "WST",
|
|
260
|
+
XK: "EUR",
|
|
261
|
+
YE: "YER",
|
|
262
|
+
YT: "EUR",
|
|
263
|
+
ZA: "ZAR",
|
|
264
|
+
ZM: "ZMW",
|
|
265
|
+
ZW: "ZWG"
|
|
266
|
+
};
|
|
267
|
+
//#endregion
|
|
268
|
+
//#region src/country.ts
|
|
269
|
+
/** The current legal tender of an ISO 3166 country (`'US'` or `'us'`), or null when unknown. Never guesses USD. */
|
|
270
|
+
function defaultCurrencyForCountry(country) {
|
|
271
|
+
if (typeof country !== "string") return null;
|
|
272
|
+
const key = country.toUpperCase();
|
|
273
|
+
return hasOwnKey(COUNTRY_CURRENCY, key) ? COUNTRY_CURRENCY[key] : null;
|
|
274
|
+
}
|
|
275
|
+
//#endregion
|
|
276
|
+
export { CURRENCIES, MoneyError, add, allocate, allocateWaterfall, assertDecimalLiteral, clamp, compare, currencyCode, currencyInfo, currencyLabel, currencyName, currencyOptions, currencySymbol, decimalClamp, decimalCompare, decimalDivide, decimalEquals, decimalMultiply, decimalRound, decimalSubtract, decimalSum, decimalWithin, defaultCurrencyForCountry, displayDigits, divide, equals, excessOver, firstCurrency, formatMoney, formatMoneyCompact, formatMoneyRange, formatStored, fromIsoMinor, fromMajor, isCurrencyCode, isDecimalLiteral, isNegative, isPositive, isPositiveAmount, isStoredAmount, isZero, localeForCurrency, max, medusaCurrencyCode, medusaEpsilon, min, money, moneyAxisFormatter, moneyByCurrency, moneyFor, multiply, parseCurrencyCode, parseDecimalText, percentOf, readCalculatedPrice, readStored, readStoredDecimal, readStoredField, readStoredLiteral, remaining, roundMajor, roundToUnit, sameCurrency, splitUnitPrice, storageDigits, storageScale, subtract, sum, toIsoMinor, toMajorDecimal, toMajorNumber, toMedusaAmountInput, toMedusaRaw, trimDecimalZeros, within };
|