@amboras-dev/money 0.0.0-stage → 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/README.md +393 -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-CdnhvAda.js +199 -0
- package/dist/allocate-D7tm_t_o.cjs +222 -0
- package/dist/amount-D7dj76nH.js +200 -0
- package/dist/amount-DHtvZSSm.cjs +241 -0
- package/dist/arithmetic-Bq-8mSal.cjs +233 -0
- package/dist/arithmetic-C-Q_aI49.js +132 -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-BdB7LEIp.js +90 -0
- package/dist/format-BnA1aLJk.cjs +113 -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 +349 -0
- package/dist/index.d.cts +288 -0
- package/dist/index.d.ts +288 -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--gQWI1f2.cjs +65 -0
- package/dist/iso-minor-CtaiIifs.js +48 -0
- package/dist/medusa-CmxCni-G.js +101 -0
- package/dist/medusa-DuQ3-KoD.d.cts +78 -0
- package/dist/medusa-OJ2TJOxt.cjs +154 -0
- package/dist/medusa-RwqxaDGq.d.ts +78 -0
- package/dist/money-CIgELekG.js +3571 -0
- package/dist/money-CbhsT0fB.d.cts +232 -0
- package/dist/money-CbhsT0fB.d.ts +232 -0
- package/dist/money-D5-vlv0h.cjs +3960 -0
- package/dist/number-text-BpvN6NAo.cjs +145 -0
- package/dist/number-text-C82oh6JB.js +134 -0
- package/dist/own-D7544D3k.js +4 -0
- package/dist/own-oP9DXFWv.cjs +9 -0
- package/dist/percent-CKVI_c-m.js +20 -0
- package/dist/percent-Dt8vNIAW.cjs +43 -0
- package/dist/rate-CmeTGNex.d.ts +63 -0
- package/dist/rate-DGPfB_jc.cjs +217 -0
- package/dist/rate-lLPViBuM.js +152 -0
- package/dist/rate-toyS3EMf.d.cts +63 -0
- package/dist/reporting-5xby-Zas.d.ts +31 -0
- package/dist/reporting-DEM97DhQ.d.cts +31 -0
- package/dist/retail-BsvpNv1F.js +69 -0
- package/dist/retail-BwcFJcVN.d.ts +31 -0
- package/dist/retail-DTk2XAMM.cjs +86 -0
- package/dist/retail-Mqd-sPMF.d.cts +31 -0
- package/dist/rounding-CVAPEfq_.js +50 -0
- package/dist/rounding-oZ832yTD.cjs +79 -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 +28518 -0
- package/dist/testing.d.cts +97 -0
- package/dist/testing.d.ts +97 -0
- package/dist/testing.js +28510 -0
- package/dist/wire-CI3Sm1iC.js +1675 -0
- package/dist/wire-Cd4Q1Jmm.d.ts +269 -0
- package/dist/wire-DQZ6fA3c.cjs +1872 -0
- package/dist/wire-hWuixv3w.d.cts +269 -0
- package/dist/wire.cjs +35 -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
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
//#region src/decimal.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* How a value between two representable amounts is resolved. `half-up` rounds a tie away from zero.
|
|
4
|
+
* `floor-after-tenth` is Shopify's discount truncation: round to a tenth of the unit (half-up), then
|
|
5
|
+
* floor (14.99995 gives 15.00, 1.899 gives 1.89).
|
|
6
|
+
*/
|
|
7
|
+
type RoundingMode = 'half-up' | 'half-even' | 'down' | 'up' | 'floor' | 'ceil' | 'floor-after-tenth';
|
|
8
|
+
declare const DECIMAL_TEXT_BRAND: unique symbol;
|
|
9
|
+
/** Canonical decimal text: optional `-`, no leading zeros, no exponent, no trailing fraction zeros, `'0'` for zero. */
|
|
10
|
+
type DecimalText = string & {
|
|
11
|
+
readonly [DECIMAL_TEXT_BRAND]: true;
|
|
12
|
+
};
|
|
13
|
+
//#endregion
|
|
14
|
+
//#region src/registry.d.ts
|
|
15
|
+
declare const CURRENCY_CODE: unique symbol;
|
|
16
|
+
/** An upper-case code this package knows. Obtain one from `currencyCode` or `isCurrencyCode`. */
|
|
17
|
+
type CurrencyCode = string & {
|
|
18
|
+
readonly [CURRENCY_CODE]: true;
|
|
19
|
+
};
|
|
20
|
+
type CurrencyStatus = 'active' | 'historic';
|
|
21
|
+
/** Amboras' storage multiplier: x100 for every currency (zero-decimal ones too), x1000 for KWD, BHD, OMR, TND, JOD, IQD, LYD. */
|
|
22
|
+
type StorageScale = 100 | 1000;
|
|
23
|
+
type FractionDigits = 0 | 2 | 3;
|
|
24
|
+
interface CurrencyInfo {
|
|
25
|
+
readonly code: CurrencyCode;
|
|
26
|
+
/** English name (CLDR). */
|
|
27
|
+
readonly name: string;
|
|
28
|
+
/** English symbol (CLDR): `$`, `CA$`, `¥`; the code itself when CLDR has none. */
|
|
29
|
+
readonly symbol: string;
|
|
30
|
+
/** Narrow symbol (CLDR): `$` for CAD too. */
|
|
31
|
+
readonly narrowSymbol: string;
|
|
32
|
+
/** True when another active currency uses the same narrow symbol (`$`, `¥`, `kr`). */
|
|
33
|
+
readonly symbolIsAmbiguous: boolean;
|
|
34
|
+
/** ISO 4217 numeric code from CLDR, three digits; null where CLDR has none. */
|
|
35
|
+
readonly numericCode: string | null;
|
|
36
|
+
readonly status: CurrencyStatus;
|
|
37
|
+
/** The code that replaced a historic currency; null for active ones. */
|
|
38
|
+
readonly replacedBy: CurrencyCode | null;
|
|
39
|
+
readonly storageScale: StorageScale;
|
|
40
|
+
/** Fraction digits a stored amount is rounded to on the way in (0 for JPY, 3 for IQD). */
|
|
41
|
+
readonly storageDigits: FractionDigits;
|
|
42
|
+
/** Fraction digits shown to people (0 for HUF and IQD, 3 for KWD). */
|
|
43
|
+
readonly displayDigits: FractionDigits;
|
|
44
|
+
/**
|
|
45
|
+
* Medusa core's `decimal_digits` (pinned `@medusajs/utils` defaults; 2 where Medusa lists
|
|
46
|
+
* none). Only for agreeing with Medusa's payment-status epsilon: see `medusaEpsilon`.
|
|
47
|
+
*/
|
|
48
|
+
readonly medusaDigits: FractionDigits;
|
|
49
|
+
/** Other spellings stores print for the currency (`['₹', 'Rs.', 'Rs']` for INR); empty where none. */
|
|
50
|
+
readonly symbolAliases: readonly string[];
|
|
51
|
+
/** The locale storefronts format this currency in (`'de-DE'` for EUR), or null. */
|
|
52
|
+
readonly localeHint: string | null;
|
|
53
|
+
/**
|
|
54
|
+
* A three-decimal currency written with a decimal comma: text reading takes a lone comma
|
|
55
|
+
* before three digits as its decimal (`'12,500'` TND is 12.5) and a lone dot as grouping,
|
|
56
|
+
* the reverse of KWD. True for TND only.
|
|
57
|
+
*/
|
|
58
|
+
readonly commaDecimal: boolean;
|
|
59
|
+
}
|
|
60
|
+
interface CurrencyCodeOptions {
|
|
61
|
+
/**
|
|
62
|
+
* Accept retired codes such as BGN or HRK. Default true: an existing store's currency (a
|
|
63
|
+
* region, a price, a tool input) keeps validating after the code is retired. Pass false only
|
|
64
|
+
* where a currency is being picked for something new (a new store, a new region).
|
|
65
|
+
*/
|
|
66
|
+
allowHistoric?: boolean;
|
|
67
|
+
/** Names the input field in the error. */
|
|
68
|
+
field?: string;
|
|
69
|
+
}
|
|
70
|
+
/** Every currency this package knows, active and historic, sorted by code. */
|
|
71
|
+
declare const CURRENCIES: readonly CurrencyInfo[];
|
|
72
|
+
/** `input` as a CurrencyCode (`'usd'` gives `'USD'`, `'bgn'` gives `'BGN'`). Throws `currency_invalid`, and for a historic code with `allowHistoric: false`. */
|
|
73
|
+
declare function currencyCode(input: string, options?: CurrencyCodeOptions): CurrencyCode;
|
|
74
|
+
/** Whether `value` already is a CurrencyCode: an upper-case known code (historic ones too, unless `allowHistoric: false`). */
|
|
75
|
+
declare function isCurrencyCode(value: unknown, options?: Pick<CurrencyCodeOptions, 'allowHistoric'>): value is CurrencyCode;
|
|
76
|
+
/** Everything known about a currency, historic ones included. Throws `currency_invalid` for an unknown code. */
|
|
77
|
+
declare function currencyInfo(code: string): CurrencyInfo;
|
|
78
|
+
/** 100, or 1000 for KWD, BHD, OMR, TND, JOD, IQD, LYD. Stored amount = major amount x storageScale. */
|
|
79
|
+
declare function storageScale(code: string): StorageScale;
|
|
80
|
+
/** Fraction digits a stored amount is rounded to: 0 for JPY, KRW, VND...; 3 for the x1000 seven; else 2. */
|
|
81
|
+
declare function storageDigits(code: string): FractionDigits;
|
|
82
|
+
/** Fraction digits to show: 0 for JPY, HUF, IDR, IQD...; 3 for KWD, BHD, OMR, TND, JOD, LYD; else 2. */
|
|
83
|
+
declare function displayDigits(code: string): FractionDigits;
|
|
84
|
+
/** `input` as a CurrencyCode, or null (see `findCurrency`). For optional input: "null while unknown". */
|
|
85
|
+
declare function parseCurrencyCode(input: unknown, options?: CurrencyCodeOptions): CurrencyCode | null;
|
|
86
|
+
/**
|
|
87
|
+
* The first candidate that is a known currency (historic codes included). Empty candidates
|
|
88
|
+
* (null, undefined, blank) are skipped; a non-empty one that is not a known code throws
|
|
89
|
+
* `currency_invalid` (a corrupt code is never replaced by a fallback); all empty throws
|
|
90
|
+
* `currency_missing`. No implicit default: pass `'USD'` last to make one visible.
|
|
91
|
+
*/
|
|
92
|
+
declare function firstCurrency(...candidates: unknown[]): CurrencyCode;
|
|
93
|
+
/** Whether both parse (historic allowed) to the same code. Empty or unknown never matches. */
|
|
94
|
+
declare function sameCurrency(a: unknown, b: unknown): boolean;
|
|
95
|
+
/** The lower-case code for Medusa columns, Medusa API payloads and SQL on lower-case columns. Historic codes allowed. */
|
|
96
|
+
declare function medusaCurrencyCode(code: string): string;
|
|
97
|
+
/** CLDR English symbol (`'CA$'`, `'A$'`), or the narrow one (`'$'`) with `display: 'narrow'`. From the registry, never ICU. */
|
|
98
|
+
declare function currencySymbol(code: string, options?: {
|
|
99
|
+
display?: 'symbol' | 'narrow';
|
|
100
|
+
}): string;
|
|
101
|
+
/**
|
|
102
|
+
* The currency's name: English (`locale` absent or `'en'`) from the registry; other locales
|
|
103
|
+
* from `Intl.DisplayNames`, falling back to the English name when Intl throws or knows none.
|
|
104
|
+
*/
|
|
105
|
+
declare function currencyName(code: string, locale?: string): string;
|
|
106
|
+
/** `'Canadian Dollar (CAD $)'`, `'Euro (EUR €)'`, `'UAE Dirham (AED)'`: name, code, and the narrow symbol when it is not the code. */
|
|
107
|
+
declare function currencyLabel(code: string, options?: {
|
|
108
|
+
locale?: string;
|
|
109
|
+
}): string;
|
|
110
|
+
/**
|
|
111
|
+
* Currencies for a picker that chooses a currency for something new: active ones only
|
|
112
|
+
* (`includeHistoric: true` adds the retired ones), `preferred` first in the given order
|
|
113
|
+
* (unknown codes, and retired ones unless included, ignored), the rest by code.
|
|
114
|
+
*/
|
|
115
|
+
declare function currencyOptions(options?: {
|
|
116
|
+
preferred?: readonly string[];
|
|
117
|
+
locale?: string;
|
|
118
|
+
includeHistoric?: boolean;
|
|
119
|
+
}): ReadonlyArray<{
|
|
120
|
+
code: CurrencyCode;
|
|
121
|
+
label: string;
|
|
122
|
+
}>;
|
|
123
|
+
/** The locale storefronts format `code` in (`'de-DE'` for EUR), else `'en-US'`. Opt-in: formatMoney never calls it. */
|
|
124
|
+
declare function localeForCurrency(code: string): string;
|
|
125
|
+
/**
|
|
126
|
+
* Medusa core's payment-status epsilon for `code`, as text: `'1'` (JPY, HUF, IQD...),
|
|
127
|
+
* `'0.001'` (KWD, BHD, JOD, LYD, OMR, TND), else `'0.01'`. Compared against storage-scale
|
|
128
|
+
* numbers, as Medusa does, so these sites agree with Medusa's own `payment_status`.
|
|
129
|
+
*/
|
|
130
|
+
declare function medusaEpsilon(code: string): DecimalText;
|
|
131
|
+
//#endregion
|
|
132
|
+
//#region src/money.d.ts
|
|
133
|
+
declare const STORED_AMOUNT: unique symbol;
|
|
134
|
+
declare const MAJOR_DECIMAL: unique symbol;
|
|
135
|
+
/** An amount in Amboras storage units (major x storageScale): a safe integer. */
|
|
136
|
+
type StoredAmount = number & {
|
|
137
|
+
readonly [STORED_AMOUNT]: true;
|
|
138
|
+
};
|
|
139
|
+
/** A major-unit amount as canonical decimal text: `-12.50`, `1200`, `1.234`. */
|
|
140
|
+
type MajorDecimal = string & {
|
|
141
|
+
readonly [MAJOR_DECIMAL]: true;
|
|
142
|
+
};
|
|
143
|
+
/** A stored amount and its currency. Build one with `money`, `fromMajor` or `readStored`. */
|
|
144
|
+
interface Money {
|
|
145
|
+
readonly amount: StoredAmount;
|
|
146
|
+
readonly currency: CurrencyCode;
|
|
147
|
+
}
|
|
148
|
+
interface FromMajorOptions {
|
|
149
|
+
/**
|
|
150
|
+
* More fraction digits than the currency stores: `'reject'` (default) throws
|
|
151
|
+
* `precision`, `'round'` rounds to the currency's storage digits with `rounding`.
|
|
152
|
+
*/
|
|
153
|
+
precision?: 'reject' | 'round';
|
|
154
|
+
/** Used with `precision: 'round'`. Default `'half-up'` (a tie rounds away from zero). */
|
|
155
|
+
rounding?: RoundingMode;
|
|
156
|
+
/**
|
|
157
|
+
* The finest amount kept: `'currency'` (default) the currency's storage digits (whole yen,
|
|
158
|
+
* a cent); `'storage'` one storage unit (1/100 yen), Medusa `toMinorUnits`
|
|
159
|
+
* (`Math.round(major x divisor)`): JPY 1200.5 is 120050 and with `precision: 'round'`
|
|
160
|
+
* JPY 1200.555 is 120056.
|
|
161
|
+
*/
|
|
162
|
+
unit?: 'currency' | 'storage';
|
|
163
|
+
/** Names the input field in errors. */
|
|
164
|
+
field?: string;
|
|
165
|
+
}
|
|
166
|
+
interface ToMajorDecimalOptions {
|
|
167
|
+
/**
|
|
168
|
+
* `'exact'` (default): the stored value exactly, with the currency's storage digits,
|
|
169
|
+
* widened to the storage scale's digits only when the value needs them (JPY 1200.50).
|
|
170
|
+
* `'display'`: rounded to the currency's display digits with `rounding`.
|
|
171
|
+
*/
|
|
172
|
+
digits?: 'exact' | 'display';
|
|
173
|
+
/** Used with `digits: 'display'`. Default `'half-up'`. */
|
|
174
|
+
rounding?: RoundingMode;
|
|
175
|
+
}
|
|
176
|
+
interface ReadStoredOptions {
|
|
177
|
+
/** `'strict'` (default) throws `amount_invalid` on a bad amount; `'lenient'` returns null. Never 0. */
|
|
178
|
+
mode?: 'strict' | 'lenient';
|
|
179
|
+
/** Names the input field in errors. */
|
|
180
|
+
field?: string;
|
|
181
|
+
}
|
|
182
|
+
interface StoredAmountOptions {
|
|
183
|
+
/** Inclusive, in stored units. */
|
|
184
|
+
min?: number;
|
|
185
|
+
/** Inclusive, in stored units. */
|
|
186
|
+
max?: number;
|
|
187
|
+
/** Default false. */
|
|
188
|
+
allowNegative?: boolean;
|
|
189
|
+
/** Default true. */
|
|
190
|
+
allowZero?: boolean;
|
|
191
|
+
}
|
|
192
|
+
/** Whether `value` is a stored amount: a safe integer number within the given sign and range rules. */
|
|
193
|
+
declare function isStoredAmount(value: unknown, options?: StoredAmountOptions): value is number;
|
|
194
|
+
/** A stored amount (already x100 / x1000) as Money. Throws `amount_invalid` unless it is a safe integer. */
|
|
195
|
+
declare function money(stored: number, currency: string): Money;
|
|
196
|
+
/**
|
|
197
|
+
* A major-unit amount (19.99 or '19.99') as Money: x100, or x1000 for the seven
|
|
198
|
+
* three-decimal currencies. JPY 1500 is stored as 150000.
|
|
199
|
+
*
|
|
200
|
+
* `precision: 'reject'` (default) is capability-kit `toStorageAmount` exactly: more
|
|
201
|
+
* fraction digits than the `unit` keeps (default the currency's storage digits) throws
|
|
202
|
+
* `precision` (19.999 USD, 5.5 JPY) unless a number is only float noise away from an
|
|
203
|
+
* allowed amount. Strings are exact, so they get no noise allowance. `precision: 'round'`
|
|
204
|
+
* rounds to the unit instead (JPY 1500.4 gives 150000; with `unit: 'storage'` 150040).
|
|
205
|
+
* Beyond 2^46 units of the storage digits throws `overflow`.
|
|
206
|
+
*/
|
|
207
|
+
declare function fromMajor(major: number | string, currency: string, options?: FromMajorOptions): Money;
|
|
208
|
+
/** Major units as exact decimal text (USD 1999 gives '19.99', JPY 120000 gives '1200', KWD 1005 gives '1.005'). */
|
|
209
|
+
declare function toMajorDecimal(m: Money, options?: ToMajorDecimalOptions): MajorDecimal;
|
|
210
|
+
/**
|
|
211
|
+
* Major units as a number: the double nearest the exact amount (`digits: 'exact'`, default),
|
|
212
|
+
* or rounded to the display digits first (`digits: 'display'`, the pixel value). For
|
|
213
|
+
* arithmetic, stay in Money.
|
|
214
|
+
*/
|
|
215
|
+
declare function toMajorNumber(m: Money, options?: ToMajorDecimalOptions): number;
|
|
216
|
+
/**
|
|
217
|
+
* A stored amount as Medusa hands it over (a number, a numeric string, a raw
|
|
218
|
+
* `{ value: '4300', precision: 20 }`, a live BigNumber) as Money. Precedence
|
|
219
|
+
* `.value ?? .raw.value ?? .raw_.value`, then `.numeric ?? .numeric_`, then the
|
|
220
|
+
* primitive. `'4300.00'` is accepted; a fractional, non-finite, unsafe or unreadable
|
|
221
|
+
* amount throws `amount_invalid`, or gives null with `mode: 'lenient'` (never 0).
|
|
222
|
+
* An unknown currency always throws.
|
|
223
|
+
*/
|
|
224
|
+
declare function readStored(raw: unknown, currency: string, options: ReadStoredOptions & {
|
|
225
|
+
mode: 'lenient';
|
|
226
|
+
}): Money | null;
|
|
227
|
+
declare function readStored(raw: unknown, currency: string, options?: ReadStoredOptions & {
|
|
228
|
+
mode?: 'strict';
|
|
229
|
+
}): Money;
|
|
230
|
+
declare function readStored(raw: unknown, currency: string, options?: ReadStoredOptions): Money | null;
|
|
231
|
+
//#endregion
|
|
232
|
+
export { localeForCurrency as A, currencyLabel as C, displayDigits as D, currencySymbol as E, storageDigits as F, storageScale as I, DecimalText as L, medusaEpsilon as M, parseCurrencyCode as N, firstCurrency as O, sameCurrency as P, RoundingMode as R, currencyInfo as S, currencyOptions as T, CurrencyInfo as _, StoredAmount as a, StorageScale as b, fromMajor as c, readStored as d, toMajorDecimal as f, CurrencyCodeOptions as g, CurrencyCode as h, ReadStoredOptions as i, medusaCurrencyCode as j, isCurrencyCode as k, isStoredAmount as l, CURRENCIES as m, MajorDecimal as n, StoredAmountOptions as o, toMajorNumber as p, Money as r, ToMajorDecimalOptions as s, FromMajorOptions as t, money as u, CurrencyStatus as v, currencyName as w, currencyCode as x, FractionDigits as y };
|
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
//#region src/decimal.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* How a value between two representable amounts is resolved. `half-up` rounds a tie away from zero.
|
|
4
|
+
* `floor-after-tenth` is Shopify's discount truncation: round to a tenth of the unit (half-up), then
|
|
5
|
+
* floor (14.99995 gives 15.00, 1.899 gives 1.89).
|
|
6
|
+
*/
|
|
7
|
+
type RoundingMode = 'half-up' | 'half-even' | 'down' | 'up' | 'floor' | 'ceil' | 'floor-after-tenth';
|
|
8
|
+
declare const DECIMAL_TEXT_BRAND: unique symbol;
|
|
9
|
+
/** Canonical decimal text: optional `-`, no leading zeros, no exponent, no trailing fraction zeros, `'0'` for zero. */
|
|
10
|
+
type DecimalText = string & {
|
|
11
|
+
readonly [DECIMAL_TEXT_BRAND]: true;
|
|
12
|
+
};
|
|
13
|
+
//#endregion
|
|
14
|
+
//#region src/registry.d.ts
|
|
15
|
+
declare const CURRENCY_CODE: unique symbol;
|
|
16
|
+
/** An upper-case code this package knows. Obtain one from `currencyCode` or `isCurrencyCode`. */
|
|
17
|
+
type CurrencyCode = string & {
|
|
18
|
+
readonly [CURRENCY_CODE]: true;
|
|
19
|
+
};
|
|
20
|
+
type CurrencyStatus = 'active' | 'historic';
|
|
21
|
+
/** Amboras' storage multiplier: x100 for every currency (zero-decimal ones too), x1000 for KWD, BHD, OMR, TND, JOD, IQD, LYD. */
|
|
22
|
+
type StorageScale = 100 | 1000;
|
|
23
|
+
type FractionDigits = 0 | 2 | 3;
|
|
24
|
+
interface CurrencyInfo {
|
|
25
|
+
readonly code: CurrencyCode;
|
|
26
|
+
/** English name (CLDR). */
|
|
27
|
+
readonly name: string;
|
|
28
|
+
/** English symbol (CLDR): `$`, `CA$`, `¥`; the code itself when CLDR has none. */
|
|
29
|
+
readonly symbol: string;
|
|
30
|
+
/** Narrow symbol (CLDR): `$` for CAD too. */
|
|
31
|
+
readonly narrowSymbol: string;
|
|
32
|
+
/** True when another active currency uses the same narrow symbol (`$`, `¥`, `kr`). */
|
|
33
|
+
readonly symbolIsAmbiguous: boolean;
|
|
34
|
+
/** ISO 4217 numeric code from CLDR, three digits; null where CLDR has none. */
|
|
35
|
+
readonly numericCode: string | null;
|
|
36
|
+
readonly status: CurrencyStatus;
|
|
37
|
+
/** The code that replaced a historic currency; null for active ones. */
|
|
38
|
+
readonly replacedBy: CurrencyCode | null;
|
|
39
|
+
readonly storageScale: StorageScale;
|
|
40
|
+
/** Fraction digits a stored amount is rounded to on the way in (0 for JPY, 3 for IQD). */
|
|
41
|
+
readonly storageDigits: FractionDigits;
|
|
42
|
+
/** Fraction digits shown to people (0 for HUF and IQD, 3 for KWD). */
|
|
43
|
+
readonly displayDigits: FractionDigits;
|
|
44
|
+
/**
|
|
45
|
+
* Medusa core's `decimal_digits` (pinned `@medusajs/utils` defaults; 2 where Medusa lists
|
|
46
|
+
* none). Only for agreeing with Medusa's payment-status epsilon: see `medusaEpsilon`.
|
|
47
|
+
*/
|
|
48
|
+
readonly medusaDigits: FractionDigits;
|
|
49
|
+
/** Other spellings stores print for the currency (`['₹', 'Rs.', 'Rs']` for INR); empty where none. */
|
|
50
|
+
readonly symbolAliases: readonly string[];
|
|
51
|
+
/** The locale storefronts format this currency in (`'de-DE'` for EUR), or null. */
|
|
52
|
+
readonly localeHint: string | null;
|
|
53
|
+
/**
|
|
54
|
+
* A three-decimal currency written with a decimal comma: text reading takes a lone comma
|
|
55
|
+
* before three digits as its decimal (`'12,500'` TND is 12.5) and a lone dot as grouping,
|
|
56
|
+
* the reverse of KWD. True for TND only.
|
|
57
|
+
*/
|
|
58
|
+
readonly commaDecimal: boolean;
|
|
59
|
+
}
|
|
60
|
+
interface CurrencyCodeOptions {
|
|
61
|
+
/**
|
|
62
|
+
* Accept retired codes such as BGN or HRK. Default true: an existing store's currency (a
|
|
63
|
+
* region, a price, a tool input) keeps validating after the code is retired. Pass false only
|
|
64
|
+
* where a currency is being picked for something new (a new store, a new region).
|
|
65
|
+
*/
|
|
66
|
+
allowHistoric?: boolean;
|
|
67
|
+
/** Names the input field in the error. */
|
|
68
|
+
field?: string;
|
|
69
|
+
}
|
|
70
|
+
/** Every currency this package knows, active and historic, sorted by code. */
|
|
71
|
+
declare const CURRENCIES: readonly CurrencyInfo[];
|
|
72
|
+
/** `input` as a CurrencyCode (`'usd'` gives `'USD'`, `'bgn'` gives `'BGN'`). Throws `currency_invalid`, and for a historic code with `allowHistoric: false`. */
|
|
73
|
+
declare function currencyCode(input: string, options?: CurrencyCodeOptions): CurrencyCode;
|
|
74
|
+
/** Whether `value` already is a CurrencyCode: an upper-case known code (historic ones too, unless `allowHistoric: false`). */
|
|
75
|
+
declare function isCurrencyCode(value: unknown, options?: Pick<CurrencyCodeOptions, 'allowHistoric'>): value is CurrencyCode;
|
|
76
|
+
/** Everything known about a currency, historic ones included. Throws `currency_invalid` for an unknown code. */
|
|
77
|
+
declare function currencyInfo(code: string): CurrencyInfo;
|
|
78
|
+
/** 100, or 1000 for KWD, BHD, OMR, TND, JOD, IQD, LYD. Stored amount = major amount x storageScale. */
|
|
79
|
+
declare function storageScale(code: string): StorageScale;
|
|
80
|
+
/** Fraction digits a stored amount is rounded to: 0 for JPY, KRW, VND...; 3 for the x1000 seven; else 2. */
|
|
81
|
+
declare function storageDigits(code: string): FractionDigits;
|
|
82
|
+
/** Fraction digits to show: 0 for JPY, HUF, IDR, IQD...; 3 for KWD, BHD, OMR, TND, JOD, LYD; else 2. */
|
|
83
|
+
declare function displayDigits(code: string): FractionDigits;
|
|
84
|
+
/** `input` as a CurrencyCode, or null (see `findCurrency`). For optional input: "null while unknown". */
|
|
85
|
+
declare function parseCurrencyCode(input: unknown, options?: CurrencyCodeOptions): CurrencyCode | null;
|
|
86
|
+
/**
|
|
87
|
+
* The first candidate that is a known currency (historic codes included). Empty candidates
|
|
88
|
+
* (null, undefined, blank) are skipped; a non-empty one that is not a known code throws
|
|
89
|
+
* `currency_invalid` (a corrupt code is never replaced by a fallback); all empty throws
|
|
90
|
+
* `currency_missing`. No implicit default: pass `'USD'` last to make one visible.
|
|
91
|
+
*/
|
|
92
|
+
declare function firstCurrency(...candidates: unknown[]): CurrencyCode;
|
|
93
|
+
/** Whether both parse (historic allowed) to the same code. Empty or unknown never matches. */
|
|
94
|
+
declare function sameCurrency(a: unknown, b: unknown): boolean;
|
|
95
|
+
/** The lower-case code for Medusa columns, Medusa API payloads and SQL on lower-case columns. Historic codes allowed. */
|
|
96
|
+
declare function medusaCurrencyCode(code: string): string;
|
|
97
|
+
/** CLDR English symbol (`'CA$'`, `'A$'`), or the narrow one (`'$'`) with `display: 'narrow'`. From the registry, never ICU. */
|
|
98
|
+
declare function currencySymbol(code: string, options?: {
|
|
99
|
+
display?: 'symbol' | 'narrow';
|
|
100
|
+
}): string;
|
|
101
|
+
/**
|
|
102
|
+
* The currency's name: English (`locale` absent or `'en'`) from the registry; other locales
|
|
103
|
+
* from `Intl.DisplayNames`, falling back to the English name when Intl throws or knows none.
|
|
104
|
+
*/
|
|
105
|
+
declare function currencyName(code: string, locale?: string): string;
|
|
106
|
+
/** `'Canadian Dollar (CAD $)'`, `'Euro (EUR €)'`, `'UAE Dirham (AED)'`: name, code, and the narrow symbol when it is not the code. */
|
|
107
|
+
declare function currencyLabel(code: string, options?: {
|
|
108
|
+
locale?: string;
|
|
109
|
+
}): string;
|
|
110
|
+
/**
|
|
111
|
+
* Currencies for a picker that chooses a currency for something new: active ones only
|
|
112
|
+
* (`includeHistoric: true` adds the retired ones), `preferred` first in the given order
|
|
113
|
+
* (unknown codes, and retired ones unless included, ignored), the rest by code.
|
|
114
|
+
*/
|
|
115
|
+
declare function currencyOptions(options?: {
|
|
116
|
+
preferred?: readonly string[];
|
|
117
|
+
locale?: string;
|
|
118
|
+
includeHistoric?: boolean;
|
|
119
|
+
}): ReadonlyArray<{
|
|
120
|
+
code: CurrencyCode;
|
|
121
|
+
label: string;
|
|
122
|
+
}>;
|
|
123
|
+
/** The locale storefronts format `code` in (`'de-DE'` for EUR), else `'en-US'`. Opt-in: formatMoney never calls it. */
|
|
124
|
+
declare function localeForCurrency(code: string): string;
|
|
125
|
+
/**
|
|
126
|
+
* Medusa core's payment-status epsilon for `code`, as text: `'1'` (JPY, HUF, IQD...),
|
|
127
|
+
* `'0.001'` (KWD, BHD, JOD, LYD, OMR, TND), else `'0.01'`. Compared against storage-scale
|
|
128
|
+
* numbers, as Medusa does, so these sites agree with Medusa's own `payment_status`.
|
|
129
|
+
*/
|
|
130
|
+
declare function medusaEpsilon(code: string): DecimalText;
|
|
131
|
+
//#endregion
|
|
132
|
+
//#region src/money.d.ts
|
|
133
|
+
declare const STORED_AMOUNT: unique symbol;
|
|
134
|
+
declare const MAJOR_DECIMAL: unique symbol;
|
|
135
|
+
/** An amount in Amboras storage units (major x storageScale): a safe integer. */
|
|
136
|
+
type StoredAmount = number & {
|
|
137
|
+
readonly [STORED_AMOUNT]: true;
|
|
138
|
+
};
|
|
139
|
+
/** A major-unit amount as canonical decimal text: `-12.50`, `1200`, `1.234`. */
|
|
140
|
+
type MajorDecimal = string & {
|
|
141
|
+
readonly [MAJOR_DECIMAL]: true;
|
|
142
|
+
};
|
|
143
|
+
/** A stored amount and its currency. Build one with `money`, `fromMajor` or `readStored`. */
|
|
144
|
+
interface Money {
|
|
145
|
+
readonly amount: StoredAmount;
|
|
146
|
+
readonly currency: CurrencyCode;
|
|
147
|
+
}
|
|
148
|
+
interface FromMajorOptions {
|
|
149
|
+
/**
|
|
150
|
+
* More fraction digits than the currency stores: `'reject'` (default) throws
|
|
151
|
+
* `precision`, `'round'` rounds to the currency's storage digits with `rounding`.
|
|
152
|
+
*/
|
|
153
|
+
precision?: 'reject' | 'round';
|
|
154
|
+
/** Used with `precision: 'round'`. Default `'half-up'` (a tie rounds away from zero). */
|
|
155
|
+
rounding?: RoundingMode;
|
|
156
|
+
/**
|
|
157
|
+
* The finest amount kept: `'currency'` (default) the currency's storage digits (whole yen,
|
|
158
|
+
* a cent); `'storage'` one storage unit (1/100 yen), Medusa `toMinorUnits`
|
|
159
|
+
* (`Math.round(major x divisor)`): JPY 1200.5 is 120050 and with `precision: 'round'`
|
|
160
|
+
* JPY 1200.555 is 120056.
|
|
161
|
+
*/
|
|
162
|
+
unit?: 'currency' | 'storage';
|
|
163
|
+
/** Names the input field in errors. */
|
|
164
|
+
field?: string;
|
|
165
|
+
}
|
|
166
|
+
interface ToMajorDecimalOptions {
|
|
167
|
+
/**
|
|
168
|
+
* `'exact'` (default): the stored value exactly, with the currency's storage digits,
|
|
169
|
+
* widened to the storage scale's digits only when the value needs them (JPY 1200.50).
|
|
170
|
+
* `'display'`: rounded to the currency's display digits with `rounding`.
|
|
171
|
+
*/
|
|
172
|
+
digits?: 'exact' | 'display';
|
|
173
|
+
/** Used with `digits: 'display'`. Default `'half-up'`. */
|
|
174
|
+
rounding?: RoundingMode;
|
|
175
|
+
}
|
|
176
|
+
interface ReadStoredOptions {
|
|
177
|
+
/** `'strict'` (default) throws `amount_invalid` on a bad amount; `'lenient'` returns null. Never 0. */
|
|
178
|
+
mode?: 'strict' | 'lenient';
|
|
179
|
+
/** Names the input field in errors. */
|
|
180
|
+
field?: string;
|
|
181
|
+
}
|
|
182
|
+
interface StoredAmountOptions {
|
|
183
|
+
/** Inclusive, in stored units. */
|
|
184
|
+
min?: number;
|
|
185
|
+
/** Inclusive, in stored units. */
|
|
186
|
+
max?: number;
|
|
187
|
+
/** Default false. */
|
|
188
|
+
allowNegative?: boolean;
|
|
189
|
+
/** Default true. */
|
|
190
|
+
allowZero?: boolean;
|
|
191
|
+
}
|
|
192
|
+
/** Whether `value` is a stored amount: a safe integer number within the given sign and range rules. */
|
|
193
|
+
declare function isStoredAmount(value: unknown, options?: StoredAmountOptions): value is number;
|
|
194
|
+
/** A stored amount (already x100 / x1000) as Money. Throws `amount_invalid` unless it is a safe integer. */
|
|
195
|
+
declare function money(stored: number, currency: string): Money;
|
|
196
|
+
/**
|
|
197
|
+
* A major-unit amount (19.99 or '19.99') as Money: x100, or x1000 for the seven
|
|
198
|
+
* three-decimal currencies. JPY 1500 is stored as 150000.
|
|
199
|
+
*
|
|
200
|
+
* `precision: 'reject'` (default) is capability-kit `toStorageAmount` exactly: more
|
|
201
|
+
* fraction digits than the `unit` keeps (default the currency's storage digits) throws
|
|
202
|
+
* `precision` (19.999 USD, 5.5 JPY) unless a number is only float noise away from an
|
|
203
|
+
* allowed amount. Strings are exact, so they get no noise allowance. `precision: 'round'`
|
|
204
|
+
* rounds to the unit instead (JPY 1500.4 gives 150000; with `unit: 'storage'` 150040).
|
|
205
|
+
* Beyond 2^46 units of the storage digits throws `overflow`.
|
|
206
|
+
*/
|
|
207
|
+
declare function fromMajor(major: number | string, currency: string, options?: FromMajorOptions): Money;
|
|
208
|
+
/** Major units as exact decimal text (USD 1999 gives '19.99', JPY 120000 gives '1200', KWD 1005 gives '1.005'). */
|
|
209
|
+
declare function toMajorDecimal(m: Money, options?: ToMajorDecimalOptions): MajorDecimal;
|
|
210
|
+
/**
|
|
211
|
+
* Major units as a number: the double nearest the exact amount (`digits: 'exact'`, default),
|
|
212
|
+
* or rounded to the display digits first (`digits: 'display'`, the pixel value). For
|
|
213
|
+
* arithmetic, stay in Money.
|
|
214
|
+
*/
|
|
215
|
+
declare function toMajorNumber(m: Money, options?: ToMajorDecimalOptions): number;
|
|
216
|
+
/**
|
|
217
|
+
* A stored amount as Medusa hands it over (a number, a numeric string, a raw
|
|
218
|
+
* `{ value: '4300', precision: 20 }`, a live BigNumber) as Money. Precedence
|
|
219
|
+
* `.value ?? .raw.value ?? .raw_.value`, then `.numeric ?? .numeric_`, then the
|
|
220
|
+
* primitive. `'4300.00'` is accepted; a fractional, non-finite, unsafe or unreadable
|
|
221
|
+
* amount throws `amount_invalid`, or gives null with `mode: 'lenient'` (never 0).
|
|
222
|
+
* An unknown currency always throws.
|
|
223
|
+
*/
|
|
224
|
+
declare function readStored(raw: unknown, currency: string, options: ReadStoredOptions & {
|
|
225
|
+
mode: 'lenient';
|
|
226
|
+
}): Money | null;
|
|
227
|
+
declare function readStored(raw: unknown, currency: string, options?: ReadStoredOptions & {
|
|
228
|
+
mode?: 'strict';
|
|
229
|
+
}): Money;
|
|
230
|
+
declare function readStored(raw: unknown, currency: string, options?: ReadStoredOptions): Money | null;
|
|
231
|
+
//#endregion
|
|
232
|
+
export { localeForCurrency as A, currencyLabel as C, displayDigits as D, currencySymbol as E, storageDigits as F, storageScale as I, DecimalText as L, medusaEpsilon as M, parseCurrencyCode as N, firstCurrency as O, sameCurrency as P, RoundingMode as R, currencyInfo as S, currencyOptions as T, CurrencyInfo as _, StoredAmount as a, StorageScale as b, fromMajor as c, readStored as d, toMajorDecimal as f, CurrencyCodeOptions as g, CurrencyCode as h, ReadStoredOptions as i, medusaCurrencyCode as j, isCurrencyCode as k, isStoredAmount as l, CURRENCIES as m, MajorDecimal as n, StoredAmountOptions as o, toMajorNumber as p, Money as r, ToMajorDecimalOptions as s, FromMajorOptions as t, money as u, CurrencyStatus as v, currencyName as w, currencyCode as x, FractionDigits as y };
|