@open-mercato/shared 0.10.1-develop.8289.1.9a25111d49 → 0.10.1-develop.8291.1.6ec3aee41c
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/.turbo/turbo-build.log +1 -1
- package/AGENTS.md +2 -1
- package/dist/lib/currencyPrecision.js +51 -0
- package/dist/lib/currencyPrecision.js.map +7 -0
- package/dist/lib/db/exactDecimalType.js +20 -0
- package/dist/lib/db/exactDecimalType.js.map +7 -0
- package/dist/lib/decimal.js +289 -0
- package/dist/lib/decimal.js.map +7 -0
- package/dist/lib/isoCurrencyMinorUnits.js +313 -0
- package/dist/lib/isoCurrencyMinorUnits.js.map +7 -0
- package/dist/lib/number.js +20 -2
- package/dist/lib/number.js.map +2 -2
- package/dist/lib/version.js +1 -1
- package/dist/lib/version.js.map +1 -1
- package/dist/modules/payment_gateways/types.js.map +2 -2
- package/package.json +4 -2
- package/src/lib/__tests__/currencyPrecision.test.ts +114 -0
- package/src/lib/__tests__/decimal.test.ts +381 -0
- package/src/lib/__tests__/number.test.ts +40 -1
- package/src/lib/currencyPrecision.ts +82 -0
- package/src/lib/db/__tests__/exactDecimalType.test.ts +86 -0
- package/src/lib/db/exactDecimalType.ts +25 -0
- package/src/lib/decimal.ts +427 -0
- package/src/lib/isoCurrencyMinorUnits.ts +315 -0
- package/src/lib/number.ts +32 -2
- package/src/modules/payment_gateways/types.ts +18 -0
|
@@ -0,0 +1,315 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ISO 4217 minor units (decimal places) of every currency code: List One published
|
|
3
|
+
* 2026-09-17 (current codes) and List Three published 2026-01-01 (withdrawn codes).
|
|
4
|
+
* `null` means ISO defines no minor unit: List One marks the code N.A. (XAU, XDR,
|
|
5
|
+
* XXX), or the code is withdrawn and List Three publishes none.
|
|
6
|
+
*/
|
|
7
|
+
export const ISO_4217_MINOR_UNITS: Readonly<Record<string, number | null>> = Object.freeze({
|
|
8
|
+
ADP: null,
|
|
9
|
+
AED: 2,
|
|
10
|
+
AFA: null,
|
|
11
|
+
AFN: 2,
|
|
12
|
+
ALK: null,
|
|
13
|
+
ALL: 2,
|
|
14
|
+
AMD: 2,
|
|
15
|
+
ANG: null,
|
|
16
|
+
AOA: 2,
|
|
17
|
+
AOK: null,
|
|
18
|
+
AON: null,
|
|
19
|
+
AOR: null,
|
|
20
|
+
ARA: null,
|
|
21
|
+
ARP: null,
|
|
22
|
+
ARS: 2,
|
|
23
|
+
ARY: null,
|
|
24
|
+
ATS: null,
|
|
25
|
+
AUD: 2,
|
|
26
|
+
AWG: 2,
|
|
27
|
+
AYM: null,
|
|
28
|
+
AZM: null,
|
|
29
|
+
AZN: 2,
|
|
30
|
+
BAD: null,
|
|
31
|
+
BAM: 2,
|
|
32
|
+
BBD: 2,
|
|
33
|
+
BDT: 2,
|
|
34
|
+
BEC: null,
|
|
35
|
+
BEF: null,
|
|
36
|
+
BEL: null,
|
|
37
|
+
BGJ: null,
|
|
38
|
+
BGK: null,
|
|
39
|
+
BGL: null,
|
|
40
|
+
BGN: null,
|
|
41
|
+
BHD: 3,
|
|
42
|
+
BIF: 0,
|
|
43
|
+
BMD: 2,
|
|
44
|
+
BND: 2,
|
|
45
|
+
BOB: 2,
|
|
46
|
+
BOP: null,
|
|
47
|
+
BOV: 2,
|
|
48
|
+
BRB: null,
|
|
49
|
+
BRC: null,
|
|
50
|
+
BRE: null,
|
|
51
|
+
BRL: 2,
|
|
52
|
+
BRN: null,
|
|
53
|
+
BRR: null,
|
|
54
|
+
BSD: 2,
|
|
55
|
+
BTN: 2,
|
|
56
|
+
BUK: null,
|
|
57
|
+
BWP: 2,
|
|
58
|
+
BYB: null,
|
|
59
|
+
BYN: 2,
|
|
60
|
+
BYR: null,
|
|
61
|
+
BZD: 2,
|
|
62
|
+
CAD: 2,
|
|
63
|
+
CDF: 2,
|
|
64
|
+
CHC: null,
|
|
65
|
+
CHE: 2,
|
|
66
|
+
CHF: 2,
|
|
67
|
+
CHW: 2,
|
|
68
|
+
CLF: 4,
|
|
69
|
+
CLP: 0,
|
|
70
|
+
CNY: 2,
|
|
71
|
+
COP: 2,
|
|
72
|
+
COU: 2,
|
|
73
|
+
CRC: 2,
|
|
74
|
+
CSD: null,
|
|
75
|
+
CSJ: null,
|
|
76
|
+
CSK: null,
|
|
77
|
+
CUC: null,
|
|
78
|
+
CUP: 2,
|
|
79
|
+
CVE: 2,
|
|
80
|
+
CYP: null,
|
|
81
|
+
CZK: 2,
|
|
82
|
+
DDM: null,
|
|
83
|
+
DEM: null,
|
|
84
|
+
DJF: 0,
|
|
85
|
+
DKK: 2,
|
|
86
|
+
DOP: 2,
|
|
87
|
+
DZD: 2,
|
|
88
|
+
ECS: null,
|
|
89
|
+
ECV: null,
|
|
90
|
+
EEK: null,
|
|
91
|
+
EGP: 2,
|
|
92
|
+
ERN: 2,
|
|
93
|
+
ESA: null,
|
|
94
|
+
ESB: null,
|
|
95
|
+
ESP: null,
|
|
96
|
+
ETB: 2,
|
|
97
|
+
EUR: 2,
|
|
98
|
+
FIM: null,
|
|
99
|
+
FJD: 2,
|
|
100
|
+
FKP: 2,
|
|
101
|
+
FRF: null,
|
|
102
|
+
GBP: 2,
|
|
103
|
+
GEK: null,
|
|
104
|
+
GEL: 2,
|
|
105
|
+
GHC: null,
|
|
106
|
+
GHP: null,
|
|
107
|
+
GHS: 2,
|
|
108
|
+
GIP: 2,
|
|
109
|
+
GMD: 2,
|
|
110
|
+
GNE: null,
|
|
111
|
+
GNF: 0,
|
|
112
|
+
GNS: null,
|
|
113
|
+
GQE: null,
|
|
114
|
+
GRD: null,
|
|
115
|
+
GTQ: 2,
|
|
116
|
+
GWE: null,
|
|
117
|
+
GWP: null,
|
|
118
|
+
GYD: 2,
|
|
119
|
+
HKD: 2,
|
|
120
|
+
HNL: 2,
|
|
121
|
+
HRD: null,
|
|
122
|
+
HRK: null,
|
|
123
|
+
HTG: 2,
|
|
124
|
+
HUF: 2,
|
|
125
|
+
IDR: 2,
|
|
126
|
+
IEP: null,
|
|
127
|
+
ILP: null,
|
|
128
|
+
ILR: null,
|
|
129
|
+
ILS: 2,
|
|
130
|
+
INR: 2,
|
|
131
|
+
IQD: 3,
|
|
132
|
+
IRR: 2,
|
|
133
|
+
ISJ: null,
|
|
134
|
+
ISK: 0,
|
|
135
|
+
ITL: null,
|
|
136
|
+
JMD: 2,
|
|
137
|
+
JOD: 3,
|
|
138
|
+
JPY: 0,
|
|
139
|
+
KES: 2,
|
|
140
|
+
KGS: 2,
|
|
141
|
+
KHR: 2,
|
|
142
|
+
KMF: 0,
|
|
143
|
+
KPW: 2,
|
|
144
|
+
KRW: 0,
|
|
145
|
+
KWD: 3,
|
|
146
|
+
KYD: 2,
|
|
147
|
+
KZT: 2,
|
|
148
|
+
LAJ: null,
|
|
149
|
+
LAK: 2,
|
|
150
|
+
LBP: 2,
|
|
151
|
+
LKR: 2,
|
|
152
|
+
LRD: 2,
|
|
153
|
+
LSL: 2,
|
|
154
|
+
LSM: null,
|
|
155
|
+
LTL: null,
|
|
156
|
+
LTT: null,
|
|
157
|
+
LUC: null,
|
|
158
|
+
LUF: null,
|
|
159
|
+
LUL: null,
|
|
160
|
+
LVL: null,
|
|
161
|
+
LVR: null,
|
|
162
|
+
LYD: 3,
|
|
163
|
+
MAD: 2,
|
|
164
|
+
MDL: 2,
|
|
165
|
+
MGA: 2,
|
|
166
|
+
MGF: null,
|
|
167
|
+
MKD: 2,
|
|
168
|
+
MLF: null,
|
|
169
|
+
MMK: 2,
|
|
170
|
+
MNT: 2,
|
|
171
|
+
MOP: 2,
|
|
172
|
+
MRO: null,
|
|
173
|
+
MRU: 2,
|
|
174
|
+
MTL: null,
|
|
175
|
+
MTP: null,
|
|
176
|
+
MUR: 2,
|
|
177
|
+
MVQ: null,
|
|
178
|
+
MVR: 2,
|
|
179
|
+
MWK: 2,
|
|
180
|
+
MXN: 2,
|
|
181
|
+
MXP: null,
|
|
182
|
+
MXV: 2,
|
|
183
|
+
MYR: 2,
|
|
184
|
+
MZE: null,
|
|
185
|
+
MZM: null,
|
|
186
|
+
MZN: 2,
|
|
187
|
+
NAD: 2,
|
|
188
|
+
NGN: 2,
|
|
189
|
+
NIC: null,
|
|
190
|
+
NIO: 2,
|
|
191
|
+
NLG: null,
|
|
192
|
+
NOK: 2,
|
|
193
|
+
NPR: 2,
|
|
194
|
+
NZD: 2,
|
|
195
|
+
OMR: 3,
|
|
196
|
+
PAB: 2,
|
|
197
|
+
PEH: null,
|
|
198
|
+
PEI: null,
|
|
199
|
+
PEN: 2,
|
|
200
|
+
PES: null,
|
|
201
|
+
PGK: 2,
|
|
202
|
+
PHP: 2,
|
|
203
|
+
PKR: 2,
|
|
204
|
+
PLN: 2,
|
|
205
|
+
PLZ: null,
|
|
206
|
+
PTE: null,
|
|
207
|
+
PYG: 0,
|
|
208
|
+
QAR: 2,
|
|
209
|
+
RHD: null,
|
|
210
|
+
ROK: null,
|
|
211
|
+
ROL: null,
|
|
212
|
+
RON: 2,
|
|
213
|
+
RSD: 2,
|
|
214
|
+
RUB: 2,
|
|
215
|
+
RUR: null,
|
|
216
|
+
RWF: 0,
|
|
217
|
+
SAR: 2,
|
|
218
|
+
SBD: 2,
|
|
219
|
+
SCR: 2,
|
|
220
|
+
SDD: null,
|
|
221
|
+
SDG: 2,
|
|
222
|
+
SDP: null,
|
|
223
|
+
SEK: 2,
|
|
224
|
+
SGD: 2,
|
|
225
|
+
SHP: 2,
|
|
226
|
+
SIT: null,
|
|
227
|
+
SKK: null,
|
|
228
|
+
SLE: 2,
|
|
229
|
+
SLL: null,
|
|
230
|
+
SOS: 2,
|
|
231
|
+
SRD: 2,
|
|
232
|
+
SRG: null,
|
|
233
|
+
SSP: 2,
|
|
234
|
+
STD: null,
|
|
235
|
+
STN: 2,
|
|
236
|
+
SUR: null,
|
|
237
|
+
SVC: 2,
|
|
238
|
+
SYP: 2,
|
|
239
|
+
SZL: 2,
|
|
240
|
+
THB: 2,
|
|
241
|
+
TJR: null,
|
|
242
|
+
TJS: 2,
|
|
243
|
+
TMM: null,
|
|
244
|
+
TMT: 2,
|
|
245
|
+
TND: 3,
|
|
246
|
+
TOP: 2,
|
|
247
|
+
TPE: null,
|
|
248
|
+
TRL: null,
|
|
249
|
+
TRY: 2,
|
|
250
|
+
TTD: 2,
|
|
251
|
+
TWD: 2,
|
|
252
|
+
TZS: 2,
|
|
253
|
+
UAH: 2,
|
|
254
|
+
UAK: null,
|
|
255
|
+
UGS: null,
|
|
256
|
+
UGW: null,
|
|
257
|
+
UGX: 0,
|
|
258
|
+
USD: 2,
|
|
259
|
+
USN: 2,
|
|
260
|
+
USS: null,
|
|
261
|
+
UYI: 0,
|
|
262
|
+
UYN: null,
|
|
263
|
+
UYP: null,
|
|
264
|
+
UYU: 2,
|
|
265
|
+
UYW: 4,
|
|
266
|
+
UZS: 2,
|
|
267
|
+
VEB: null,
|
|
268
|
+
VED: 2,
|
|
269
|
+
VEF: null,
|
|
270
|
+
VES: 2,
|
|
271
|
+
VNC: null,
|
|
272
|
+
VND: 0,
|
|
273
|
+
VUV: 0,
|
|
274
|
+
WST: 2,
|
|
275
|
+
XAD: 2,
|
|
276
|
+
XAF: 0,
|
|
277
|
+
XAG: null,
|
|
278
|
+
XAU: null,
|
|
279
|
+
XBA: null,
|
|
280
|
+
XBB: null,
|
|
281
|
+
XBC: null,
|
|
282
|
+
XBD: null,
|
|
283
|
+
XCD: 2,
|
|
284
|
+
XCG: 2,
|
|
285
|
+
XDR: null,
|
|
286
|
+
XEU: null,
|
|
287
|
+
XFO: null,
|
|
288
|
+
XFU: null,
|
|
289
|
+
XOF: 0,
|
|
290
|
+
XPD: null,
|
|
291
|
+
XPF: 0,
|
|
292
|
+
XPT: null,
|
|
293
|
+
XRE: null,
|
|
294
|
+
XSU: null,
|
|
295
|
+
XTS: null,
|
|
296
|
+
XUA: null,
|
|
297
|
+
XXX: null,
|
|
298
|
+
YDD: null,
|
|
299
|
+
YER: 2,
|
|
300
|
+
YUD: null,
|
|
301
|
+
YUM: null,
|
|
302
|
+
YUN: null,
|
|
303
|
+
ZAL: null,
|
|
304
|
+
ZAR: 2,
|
|
305
|
+
ZMK: null,
|
|
306
|
+
ZMW: 2,
|
|
307
|
+
ZRN: null,
|
|
308
|
+
ZRZ: null,
|
|
309
|
+
ZWC: null,
|
|
310
|
+
ZWD: null,
|
|
311
|
+
ZWG: 2,
|
|
312
|
+
ZWL: null,
|
|
313
|
+
ZWN: null,
|
|
314
|
+
ZWR: null,
|
|
315
|
+
})
|
package/src/lib/number.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { decimalToString, isDecimalInput, type DecimalInput } from './decimal'
|
|
2
|
+
|
|
1
3
|
type LocaleNumberSeparators = { group: string; decimal: string }
|
|
2
4
|
|
|
3
5
|
const DEFAULT_SEPARATORS: LocaleNumberSeparators = { group: ',', decimal: '.' }
|
|
@@ -60,6 +62,24 @@ function isValidGrouping(integerPart: string, separator: string): boolean {
|
|
|
60
62
|
* numbers and MUST NOT go through it.
|
|
61
63
|
*/
|
|
62
64
|
export function parseLocaleNumber(input: string | null | undefined, locale?: string): number | null {
|
|
65
|
+
const normalized = normalizeLocaleNumber(input, locale)
|
|
66
|
+
if (normalized === null) return null
|
|
67
|
+
const parsed = Number(normalized)
|
|
68
|
+
return Number.isFinite(parsed) ? parsed : null
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Same parsing rules as `parseLocaleNumber`, but returns the exact decimal string
|
|
73
|
+
* (`1 234,567891234567891` → `1234.567891234567891`) instead of a lossy JS `number`.
|
|
74
|
+
* Use it for money and FX values a user typed.
|
|
75
|
+
*/
|
|
76
|
+
export function parseLocaleDecimal(input: string | null | undefined, locale?: string): string | null {
|
|
77
|
+
const normalized = normalizeLocaleNumber(input, locale)
|
|
78
|
+
if (normalized === null || !isDecimalInput(normalized)) return null
|
|
79
|
+
return decimalToString(normalized)
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function normalizeLocaleNumber(input: string | null | undefined, locale?: string): string | null {
|
|
63
83
|
if (input == null) return null
|
|
64
84
|
const trimmed = input.trim()
|
|
65
85
|
if (!trimmed) return null
|
|
@@ -97,8 +117,7 @@ export function parseLocaleNumber(input: string | null | undefined, locale?: str
|
|
|
97
117
|
|
|
98
118
|
const normalized = `${integerPart.split(groupSeparator).join('')}${fractionParts.length ? `.${fractionParts[0]}` : ''}`
|
|
99
119
|
if (!NORMALIZED_NUMBER.test(normalized)) return null
|
|
100
|
-
|
|
101
|
-
return Number.isFinite(parsed) ? parsed : null
|
|
120
|
+
return normalized
|
|
102
121
|
}
|
|
103
122
|
|
|
104
123
|
/**
|
|
@@ -114,6 +133,17 @@ export function formatLocaleNumberInput(value: number, locale?: string): string
|
|
|
114
133
|
return decimal === '.' ? serialized : serialized.replace('.', decimal)
|
|
115
134
|
}
|
|
116
135
|
|
|
136
|
+
/**
|
|
137
|
+
* Exact counterpart of `formatLocaleNumberInput` for decimal strings: renders every digit
|
|
138
|
+
* (`1234.567891234567891234` → `1234,567891234567891234` under `pl-PL`) in plain notation,
|
|
139
|
+
* without grouping. The output round-trips through `parseLocaleDecimal`.
|
|
140
|
+
*/
|
|
141
|
+
export function formatLocaleDecimalInput(value: DecimalInput, locale?: string): string {
|
|
142
|
+
const serialized = decimalToString(value)
|
|
143
|
+
const { decimal } = resolveLocaleNumberSeparators(locale)
|
|
144
|
+
return decimal === '.' ? serialized : serialized.replace('.', decimal)
|
|
145
|
+
}
|
|
146
|
+
|
|
117
147
|
export function parseNumberWithDefault(
|
|
118
148
|
raw: string | null | undefined,
|
|
119
149
|
fallback: number,
|
|
@@ -142,7 +142,9 @@ export interface PaymentGatewayScope {
|
|
|
142
142
|
export interface PaymentOrderTotal {
|
|
143
143
|
orderId: string
|
|
144
144
|
currencyCode: string
|
|
145
|
+
/** Float copy of `amountDueExact`; prefer the exact field. */
|
|
145
146
|
amountDue: number
|
|
147
|
+
amountDueExact?: string
|
|
146
148
|
}
|
|
147
149
|
|
|
148
150
|
/**
|
|
@@ -167,7 +169,9 @@ export interface CreateSessionInput {
|
|
|
167
169
|
idempotencyKey?: string
|
|
168
170
|
tenantId: string
|
|
169
171
|
organizationId: string
|
|
172
|
+
/** Float copy of `amountExact`; prefer the exact field. */
|
|
170
173
|
amount: number
|
|
174
|
+
amountExact?: string
|
|
171
175
|
currencyCode: string
|
|
172
176
|
captureMethod?: 'automatic' | 'manual'
|
|
173
177
|
paymentTypes?: string[]
|
|
@@ -183,7 +187,9 @@ export interface CreateSessionInput {
|
|
|
183
187
|
export interface SessionLineItem {
|
|
184
188
|
name: string
|
|
185
189
|
quantity: number
|
|
190
|
+
/** Float copy of `unitAmountExact`; prefer the exact field. */
|
|
186
191
|
unitAmount: number
|
|
192
|
+
unitAmountExact?: string
|
|
187
193
|
currencyCode: string
|
|
188
194
|
}
|
|
189
195
|
|
|
@@ -198,7 +204,9 @@ export interface CreateSessionResult {
|
|
|
198
204
|
|
|
199
205
|
export interface CaptureInput {
|
|
200
206
|
sessionId: string
|
|
207
|
+
/** Float copy of `amountExact`; prefer the exact field. */
|
|
201
208
|
amount?: number
|
|
209
|
+
amountExact?: string
|
|
202
210
|
credentials: Record<string, unknown>
|
|
203
211
|
metadata?: Record<string, unknown>
|
|
204
212
|
idempotencyKey?: string
|
|
@@ -206,13 +214,17 @@ export interface CaptureInput {
|
|
|
206
214
|
|
|
207
215
|
export interface CaptureResult {
|
|
208
216
|
status: UnifiedPaymentStatus
|
|
217
|
+
/** Float copy of `capturedAmountExact`; prefer the exact field. */
|
|
209
218
|
capturedAmount: number
|
|
219
|
+
capturedAmountExact?: string
|
|
210
220
|
providerData?: Record<string, unknown>
|
|
211
221
|
}
|
|
212
222
|
|
|
213
223
|
export interface RefundInput {
|
|
214
224
|
sessionId: string
|
|
225
|
+
/** Float copy of `amountExact`; prefer the exact field. */
|
|
215
226
|
amount?: number
|
|
227
|
+
amountExact?: string
|
|
216
228
|
reason?: string
|
|
217
229
|
credentials: Record<string, unknown>
|
|
218
230
|
metadata?: Record<string, unknown>
|
|
@@ -222,7 +234,9 @@ export interface RefundInput {
|
|
|
222
234
|
export interface RefundResult {
|
|
223
235
|
refundId: string
|
|
224
236
|
status: UnifiedPaymentStatus
|
|
237
|
+
/** Float copy of `refundedAmountExact`; prefer the exact field. */
|
|
225
238
|
refundedAmount: number
|
|
239
|
+
refundedAmountExact?: string
|
|
226
240
|
providerData?: Record<string, unknown>
|
|
227
241
|
}
|
|
228
242
|
|
|
@@ -245,8 +259,12 @@ export interface GetStatusInput {
|
|
|
245
259
|
|
|
246
260
|
export interface GatewayPaymentStatus {
|
|
247
261
|
status: UnifiedPaymentStatus
|
|
262
|
+
/** Float copy of `amountExact`; prefer the exact field. */
|
|
248
263
|
amount: number
|
|
264
|
+
amountExact?: string
|
|
265
|
+
/** Float copy of `amountReceivedExact`; prefer the exact field. */
|
|
249
266
|
amountReceived: number
|
|
267
|
+
amountReceivedExact?: string
|
|
250
268
|
currencyCode: string
|
|
251
269
|
providerData?: Record<string, unknown>
|
|
252
270
|
}
|