@atscript/ui 0.1.162 → 0.1.164
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/dist/client-factory.cjs +27 -0
- package/dist/client-factory.d.cts +53 -0
- package/dist/client-factory.d.mts +53 -0
- package/dist/client-factory.mjs +25 -0
- package/dist/form/apply.cjs +38 -0
- package/dist/form/apply.d.cts +36 -0
- package/dist/form/apply.d.mts +36 -0
- package/dist/form/apply.mjs +38 -0
- package/dist/form/clone.cjs +30 -0
- package/dist/form/clone.d.cts +23 -0
- package/dist/form/clone.d.mts +23 -0
- package/dist/form/clone.mjs +30 -0
- package/dist/form/create-form-def.cjs +440 -0
- package/dist/form/create-form-def.d.cts +67 -0
- package/dist/form/create-form-def.d.mts +67 -0
- package/dist/form/create-form-def.mjs +438 -0
- package/dist/form/decimal-format.cjs +324 -0
- package/dist/form/decimal-format.d.cts +86 -0
- package/dist/form/decimal-format.d.mts +86 -0
- package/dist/form/decimal-format.mjs +315 -0
- package/dist/form/diff.cjs +458 -0
- package/dist/form/diff.d.cts +80 -0
- package/dist/form/diff.d.mts +80 -0
- package/dist/form/diff.mjs +457 -0
- package/dist/form/dirty.cjs +65 -0
- package/dist/form/dirty.d.cts +47 -0
- package/dist/form/dirty.d.mts +47 -0
- package/dist/form/dirty.mjs +64 -0
- package/dist/form/error-utils.cjs +97 -0
- package/dist/form/error-utils.d.cts +58 -0
- package/dist/form/error-utils.d.mts +58 -0
- package/dist/form/error-utils.mjs +92 -0
- package/dist/form/form-actions.cjs +30 -0
- package/dist/form/form-actions.d.cts +17 -0
- package/dist/form/form-actions.d.mts +17 -0
- package/dist/form/form-actions.mjs +30 -0
- package/dist/form/grid.cjs +69 -0
- package/dist/form/grid.d.cts +49 -0
- package/dist/form/grid.d.mts +49 -0
- package/dist/form/grid.mjs +64 -0
- package/dist/form/labels.cjs +10 -0
- package/dist/form/labels.d.cts +7 -0
- package/dist/form/labels.d.mts +7 -0
- package/dist/form/labels.mjs +10 -0
- package/dist/form/measurement.cjs +20 -0
- package/dist/form/measurement.d.cts +29 -0
- package/dist/form/measurement.d.mts +29 -0
- package/dist/form/measurement.mjs +20 -0
- package/dist/form/path-utils.cjs +182 -0
- package/dist/form/path-utils.d.cts +47 -0
- package/dist/form/path-utils.d.mts +47 -0
- package/dist/form/path-utils.mjs +175 -0
- package/dist/form/rebase.cjs +89 -0
- package/dist/form/rebase.d.cts +61 -0
- package/dist/form/rebase.d.mts +61 -0
- package/dist/form/rebase.mjs +89 -0
- package/dist/form/skipped-nav.cjs +100 -0
- package/dist/form/skipped-nav.d.cts +13 -0
- package/dist/form/skipped-nav.d.mts +13 -0
- package/dist/form/skipped-nav.mjs +98 -0
- package/dist/form/types.cjs +40 -0
- package/dist/form/types.d.cts +133 -0
- package/dist/form/types.d.mts +133 -0
- package/dist/form/types.mjs +36 -0
- package/dist/form/union-detect.cjs +46 -0
- package/dist/form/union-detect.d.cts +22 -0
- package/dist/form/union-detect.d.mts +22 -0
- package/dist/form/union-detect.mjs +46 -0
- package/dist/form/validate.cjs +150 -0
- package/dist/form/validate.d.cts +50 -0
- package/dist/form/validate.d.mts +50 -0
- package/dist/form/validate.mjs +147 -0
- package/dist/index.cjs +217 -3360
- package/dist/index.d.cts +38 -1693
- package/dist/index.d.mts +38 -1693
- package/dist/index.mjs +35 -3179
- package/dist/nav/model-routes.cjs +59 -0
- package/dist/nav/model-routes.d.cts +33 -0
- package/dist/nav/model-routes.d.mts +33 -0
- package/dist/nav/model-routes.mjs +59 -0
- package/dist/plugin/annotations.cjs +577 -0
- package/dist/plugin/annotations.mjs +577 -0
- package/dist/plugin/primitives.cjs +30 -0
- package/dist/plugin/primitives.mjs +30 -0
- package/dist/plugin/value-help-validation.cjs +419 -0
- package/dist/plugin/value-help-validation.mjs +413 -0
- package/dist/plugin.cjs +4 -1001
- package/dist/plugin.mjs +2 -999
- package/dist/shared/annotation-keys.cjs +189 -0
- package/dist/shared/annotation-keys.d.cts +82 -0
- package/dist/shared/annotation-keys.d.mts +82 -0
- package/dist/shared/annotation-keys.mjs +97 -0
- package/dist/shared/field-resolver.cjs +123 -0
- package/dist/shared/field-resolver.d.cts +77 -0
- package/dist/shared/field-resolver.d.mts +77 -0
- package/dist/shared/field-resolver.mjs +110 -0
- package/dist/shared/meta-cache.cjs +244 -0
- package/dist/shared/meta-cache.d.cts +110 -0
- package/dist/shared/meta-cache.d.mts +110 -0
- package/dist/shared/meta-cache.mjs +237 -0
- package/dist/shared/str.cjs +19 -0
- package/dist/shared/str.d.cts +7 -0
- package/dist/shared/str.d.mts +7 -0
- package/dist/shared/str.mjs +17 -0
- package/dist/table/column-resolver.cjs +17 -0
- package/dist/table/column-resolver.d.cts +11 -0
- package/dist/table/column-resolver.d.mts +11 -0
- package/dist/table/column-resolver.mjs +15 -0
- package/dist/table/create-table-def.cjs +233 -0
- package/dist/table/create-table-def.d.cts +15 -0
- package/dist/table/create-table-def.d.mts +15 -0
- package/dist/table/create-table-def.mjs +233 -0
- package/dist/table/navigate-href.cjs +45 -0
- package/dist/table/navigate-href.d.cts +31 -0
- package/dist/table/navigate-href.d.mts +31 -0
- package/dist/table/navigate-href.mjs +45 -0
- package/dist/table/types.d.cts +287 -0
- package/dist/table/types.d.mts +287 -0
- package/dist/table/value-kind.cjs +20 -0
- package/dist/table/value-kind.d.cts +15 -0
- package/dist/table/value-kind.d.mts +15 -0
- package/dist/table/value-kind.mjs +19 -0
- package/dist/value-help/dict-paths.cjs +17 -0
- package/dist/value-help/dict-paths.d.cts +11 -0
- package/dist/value-help/dict-paths.d.mts +11 -0
- package/dist/value-help/dict-paths.mjs +17 -0
- package/dist/value-help/distinct-client.cjs +51 -0
- package/dist/value-help/distinct-client.d.cts +40 -0
- package/dist/value-help/distinct-client.d.mts +40 -0
- package/dist/value-help/distinct-client.mjs +50 -0
- package/dist/value-help/extract-literals.cjs +76 -0
- package/dist/value-help/extract-literals.d.cts +32 -0
- package/dist/value-help/extract-literals.d.mts +32 -0
- package/dist/value-help/extract-literals.mjs +74 -0
- package/dist/value-help/extract-ref.cjs +100 -0
- package/dist/value-help/extract-ref.d.cts +37 -0
- package/dist/value-help/extract-ref.d.mts +37 -0
- package/dist/value-help/extract-ref.mjs +100 -0
- package/dist/value-help/filter-tree.cjs +45 -0
- package/dist/value-help/filter-tree.mjs +45 -0
- package/dist/value-help/index.d.mts +8 -0
- package/dist/value-help/resolve-options.cjs +64 -0
- package/dist/value-help/resolve-options.d.cts +35 -0
- package/dist/value-help/resolve-options.d.mts +35 -0
- package/dist/value-help/resolve-options.mjs +60 -0
- package/dist/value-help/resolve.cjs +62 -0
- package/dist/value-help/resolve.d.cts +31 -0
- package/dist/value-help/resolve.d.mts +31 -0
- package/dist/value-help/resolve.mjs +61 -0
- package/dist/value-help/types.d.cts +36 -0
- package/dist/value-help/types.d.mts +36 -0
- package/dist/value-help/value-help-client.cjs +136 -0
- package/dist/value-help/value-help-client.d.cts +67 -0
- package/dist/value-help/value-help-client.d.mts +67 -0
- package/dist/value-help/value-help-client.mjs +134 -0
- package/package.json +10 -10
|
@@ -0,0 +1,315 @@
|
|
|
1
|
+
//#region src/form/decimal-format.ts
|
|
2
|
+
const WHITESPACE_GROUP_RE = /[ ]/g;
|
|
3
|
+
const numberFormatCache = /* @__PURE__ */ new Map();
|
|
4
|
+
function getNumberFormat(locale, opts) {
|
|
5
|
+
const key = `${locale ?? ""}|${opts.style ?? ""}|${opts.currency ?? ""}|${opts.currencyDisplay ?? ""}|${opts.useGrouping ?? ""}|${opts.minimumFractionDigits ?? ""}|${opts.maximumFractionDigits ?? ""}`;
|
|
6
|
+
let f = numberFormatCache.get(key);
|
|
7
|
+
if (!f) {
|
|
8
|
+
f = new Intl.NumberFormat(locale, opts);
|
|
9
|
+
numberFormatCache.set(key, f);
|
|
10
|
+
}
|
|
11
|
+
return f;
|
|
12
|
+
}
|
|
13
|
+
const decimalSeparatorCache = /* @__PURE__ */ new Map();
|
|
14
|
+
const thousandsSeparatorCache = /* @__PURE__ */ new Map();
|
|
15
|
+
const currencyDisplayCache = /* @__PURE__ */ new Map();
|
|
16
|
+
const currencyDecimalsCache = /* @__PURE__ */ new Map();
|
|
17
|
+
/** "." in en-US, "," in fr-FR. Returns "." when locale is undefined. */
|
|
18
|
+
function getDecimalSeparator(locale) {
|
|
19
|
+
const key = locale ?? "";
|
|
20
|
+
const cached = decimalSeparatorCache.get(key);
|
|
21
|
+
if (cached !== void 0) return cached;
|
|
22
|
+
let out = ".";
|
|
23
|
+
try {
|
|
24
|
+
out = getNumberFormat(locale, { useGrouping: false }).formatToParts(1.1).find((p) => p.type === "decimal")?.value ?? ".";
|
|
25
|
+
} catch {}
|
|
26
|
+
decimalSeparatorCache.set(key, out);
|
|
27
|
+
return out;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* "," in en-US, NNBSP (U+202F) in fr-FR. Returns "" when no grouping is
|
|
31
|
+
* applied (or when Intl rejects the locale).
|
|
32
|
+
*/
|
|
33
|
+
function getThousandsSeparator(locale) {
|
|
34
|
+
const key = locale ?? "";
|
|
35
|
+
const cached = thousandsSeparatorCache.get(key);
|
|
36
|
+
if (cached !== void 0) return cached;
|
|
37
|
+
let out = "";
|
|
38
|
+
try {
|
|
39
|
+
out = getNumberFormat(locale, { useGrouping: true }).formatToParts(1e6).find((p) => p.type === "group")?.value ?? "";
|
|
40
|
+
} catch {}
|
|
41
|
+
thousandsSeparatorCache.set(key, out);
|
|
42
|
+
return out;
|
|
43
|
+
}
|
|
44
|
+
function getCurrencyDisplayParts(code, locale) {
|
|
45
|
+
const key = `${locale ?? ""}|${code}`;
|
|
46
|
+
const cached = currencyDisplayCache.get(key);
|
|
47
|
+
if (cached) return cached;
|
|
48
|
+
let out = {
|
|
49
|
+
symbol: code,
|
|
50
|
+
position: "prefix"
|
|
51
|
+
};
|
|
52
|
+
try {
|
|
53
|
+
const parts = getNumberFormat(locale, {
|
|
54
|
+
style: "currency",
|
|
55
|
+
currency: code,
|
|
56
|
+
currencyDisplay: "narrowSymbol"
|
|
57
|
+
}).formatToParts(0);
|
|
58
|
+
const idxCur = parts.findIndex((p) => p.type === "currency");
|
|
59
|
+
const idxInt = parts.findIndex((p) => p.type === "integer");
|
|
60
|
+
out = {
|
|
61
|
+
symbol: parts[idxCur]?.value ?? code,
|
|
62
|
+
position: idxInt === -1 || idxCur < idxInt ? "prefix" : "suffix"
|
|
63
|
+
};
|
|
64
|
+
} catch {}
|
|
65
|
+
currencyDisplayCache.set(key, out);
|
|
66
|
+
return out;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Currency's natural decimal count via Intl. JPY=0, USD/EUR=2, BHD/KWD=3.
|
|
70
|
+
* Returns `undefined` for codes Intl doesn't know — caller falls back to
|
|
71
|
+
* `dbPrecisionScale`.
|
|
72
|
+
*/
|
|
73
|
+
function getCurrencyDecimals(code, locale) {
|
|
74
|
+
if (!code) return void 0;
|
|
75
|
+
const key = `${locale ?? ""}|${code}`;
|
|
76
|
+
if (currencyDecimalsCache.has(key)) return currencyDecimalsCache.get(key);
|
|
77
|
+
let out;
|
|
78
|
+
try {
|
|
79
|
+
const max = getNumberFormat(locale, {
|
|
80
|
+
style: "currency",
|
|
81
|
+
currency: code
|
|
82
|
+
}).resolvedOptions().maximumFractionDigits;
|
|
83
|
+
out = typeof max === "number" ? max : void 0;
|
|
84
|
+
} catch {
|
|
85
|
+
out = void 0;
|
|
86
|
+
}
|
|
87
|
+
currencyDecimalsCache.set(key, out);
|
|
88
|
+
return out;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Parse a user-typed decimal string. Returns the canonical decimal
|
|
92
|
+
* (no thousands separator, "." as decimal separator) or `null` if invalid.
|
|
93
|
+
* Accepts both "." and "," as the decimal separator (locale-aware).
|
|
94
|
+
* Strips the locale thousands separator. Preserves sign. Does NOT
|
|
95
|
+
* enforce scale.
|
|
96
|
+
*/
|
|
97
|
+
function parseDecimalInput(raw, locale) {
|
|
98
|
+
if (typeof raw !== "string") return null;
|
|
99
|
+
const trimmed = raw.trim();
|
|
100
|
+
if (trimmed === "") return null;
|
|
101
|
+
const decSep = getDecimalSeparator(locale);
|
|
102
|
+
const thouSep = getThousandsSeparator(locale);
|
|
103
|
+
let sign = "";
|
|
104
|
+
let body = trimmed;
|
|
105
|
+
if (body.startsWith("-")) {
|
|
106
|
+
sign = "-";
|
|
107
|
+
body = body.slice(1);
|
|
108
|
+
} else if (body.startsWith("+")) body = body.slice(1);
|
|
109
|
+
if (body.length === 0) return null;
|
|
110
|
+
if (body.includes("-") || body.includes("+")) return null;
|
|
111
|
+
if (thouSep && body.includes(thouSep)) {
|
|
112
|
+
const decIdx = body.indexOf(decSep);
|
|
113
|
+
const headRaw = decIdx === -1 ? body : body.slice(0, decIdx);
|
|
114
|
+
const tail = decIdx === -1 ? "" : body.slice(decIdx);
|
|
115
|
+
if (tail.includes(thouSep)) return null;
|
|
116
|
+
if (headRaw.includes(thouSep)) {
|
|
117
|
+
const groups = headRaw.split(thouSep);
|
|
118
|
+
const first = groups[0];
|
|
119
|
+
if (first.length < 1 || first.length > 3 || !/^\d+$/.test(first)) return null;
|
|
120
|
+
for (let i = 1; i < groups.length; i += 1) if (!/^\d{3}$/.test(groups[i])) return null;
|
|
121
|
+
}
|
|
122
|
+
body = `${headRaw.split(thouSep).join("")}${tail}`;
|
|
123
|
+
}
|
|
124
|
+
body = body.replace(WHITESPACE_GROUP_RE, "");
|
|
125
|
+
if (decSep !== ".") {
|
|
126
|
+
if ((body.match(/,/g)?.length ?? 0) > 1) return null;
|
|
127
|
+
if (body.includes(".") && body.includes(",")) return null;
|
|
128
|
+
if (body.includes(",")) body = body.replace(",", ".");
|
|
129
|
+
} else if (body.includes(",")) {
|
|
130
|
+
if (body.includes(".")) return null;
|
|
131
|
+
if ((body.match(/,/g)?.length ?? 0) > 1) return null;
|
|
132
|
+
body = body.replace(",", ".");
|
|
133
|
+
}
|
|
134
|
+
if (!/^\d*\.?\d*$/.test(body)) return null;
|
|
135
|
+
if (body === "" || body === ".") return null;
|
|
136
|
+
if (body.endsWith(".")) body = body.slice(0, -1);
|
|
137
|
+
if (body.startsWith(".")) body = `0${body}`;
|
|
138
|
+
const dot = body.indexOf(".");
|
|
139
|
+
if (dot === -1) body = body.replace(/^0+(\d)/, "$1");
|
|
140
|
+
else {
|
|
141
|
+
const head = body.slice(0, dot);
|
|
142
|
+
const tail = body.slice(dot);
|
|
143
|
+
body = `${head.replace(/^0+(\d)/, "$1")}${tail}`;
|
|
144
|
+
}
|
|
145
|
+
if (sign === "-" && /^0+(\.0+)?$/.test(body)) sign = "";
|
|
146
|
+
return `${sign}${body}`;
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Enforce a fractional-digit count by truncating or padding. String-only —
|
|
150
|
+
* no float arithmetic. Default behaviour truncates (no rounding) so digits
|
|
151
|
+
* the user typed can't silently shift. Pass `roundHalfUp: true` to round.
|
|
152
|
+
*
|
|
153
|
+
* enforceScale("12.345", 2) → "12.34" (truncate)
|
|
154
|
+
* enforceScale("12.3", 4) → "12.3000" (pad)
|
|
155
|
+
* enforceScale("12", 0) → "12"
|
|
156
|
+
* enforceScale("12.99", 0) → "12" (no rounding)
|
|
157
|
+
*/
|
|
158
|
+
function enforceScale(s, scale, opts) {
|
|
159
|
+
if (typeof s !== "string" || s === "") return s;
|
|
160
|
+
if (scale === void 0 || scale < 0) return s;
|
|
161
|
+
const parts = splitDecimalString(s);
|
|
162
|
+
if (scale === 0) {
|
|
163
|
+
if (opts?.roundHalfUp && parts.decimal.length > 0 && parts.decimal[0] >= "5") {
|
|
164
|
+
const bumped = addOne(parts.integer);
|
|
165
|
+
return joinDecimalString({
|
|
166
|
+
sign: parts.sign,
|
|
167
|
+
integer: bumped,
|
|
168
|
+
decimal: ""
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
return joinDecimalString({
|
|
172
|
+
sign: parts.sign,
|
|
173
|
+
integer: parts.integer,
|
|
174
|
+
decimal: ""
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
if (parts.decimal.length === scale) return joinDecimalString(parts);
|
|
178
|
+
if (parts.decimal.length < scale) return joinDecimalString({
|
|
179
|
+
sign: parts.sign,
|
|
180
|
+
integer: parts.integer,
|
|
181
|
+
decimal: parts.decimal.padEnd(scale, "0")
|
|
182
|
+
});
|
|
183
|
+
let head = parts.decimal.slice(0, scale);
|
|
184
|
+
const dropped = parts.decimal.slice(scale);
|
|
185
|
+
let integer = parts.integer;
|
|
186
|
+
if (opts?.roundHalfUp && dropped.length > 0 && dropped[0] >= "5") {
|
|
187
|
+
const bumpedFrac = addOne(head);
|
|
188
|
+
if (bumpedFrac.length > head.length) {
|
|
189
|
+
integer = addOne(integer);
|
|
190
|
+
head = "0".repeat(scale);
|
|
191
|
+
} else head = bumpedFrac;
|
|
192
|
+
}
|
|
193
|
+
return joinDecimalString({
|
|
194
|
+
sign: parts.sign,
|
|
195
|
+
integer,
|
|
196
|
+
decimal: head
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
function addOne(digits) {
|
|
200
|
+
if (digits === "") return "1";
|
|
201
|
+
const out = digits.split("");
|
|
202
|
+
let i = out.length - 1;
|
|
203
|
+
let carry = 1;
|
|
204
|
+
while (i >= 0 && carry > 0) {
|
|
205
|
+
const d = out[i].charCodeAt(0) - 48 + carry | 0;
|
|
206
|
+
if (d >= 10) {
|
|
207
|
+
out[i] = "0";
|
|
208
|
+
carry = 1;
|
|
209
|
+
} else {
|
|
210
|
+
out[i] = String.fromCharCode(d + 48);
|
|
211
|
+
carry = 0;
|
|
212
|
+
}
|
|
213
|
+
i -= 1;
|
|
214
|
+
}
|
|
215
|
+
if (carry > 0) out.unshift("1");
|
|
216
|
+
return out.join("");
|
|
217
|
+
}
|
|
218
|
+
function splitDecimalString(s) {
|
|
219
|
+
if (typeof s !== "string" || s === "") return {
|
|
220
|
+
sign: "",
|
|
221
|
+
integer: "0",
|
|
222
|
+
decimal: ""
|
|
223
|
+
};
|
|
224
|
+
let sign = "";
|
|
225
|
+
let body = s;
|
|
226
|
+
if (body.startsWith("-")) {
|
|
227
|
+
sign = "-";
|
|
228
|
+
body = body.slice(1);
|
|
229
|
+
} else if (body.startsWith("+")) body = body.slice(1);
|
|
230
|
+
if (body === "") return {
|
|
231
|
+
sign: "",
|
|
232
|
+
integer: "0",
|
|
233
|
+
decimal: ""
|
|
234
|
+
};
|
|
235
|
+
const dot = body.indexOf(".");
|
|
236
|
+
let integer;
|
|
237
|
+
let decimal;
|
|
238
|
+
if (dot === -1) {
|
|
239
|
+
integer = body;
|
|
240
|
+
decimal = "";
|
|
241
|
+
} else {
|
|
242
|
+
integer = body.slice(0, dot);
|
|
243
|
+
decimal = body.slice(dot + 1);
|
|
244
|
+
}
|
|
245
|
+
if (integer === "") integer = "0";
|
|
246
|
+
integer = integer.replace(/^0+(\d)/, "$1");
|
|
247
|
+
if (sign === "-" && integer === "0" && !decimal.replace(/0/g, "")) sign = "";
|
|
248
|
+
return {
|
|
249
|
+
sign,
|
|
250
|
+
integer,
|
|
251
|
+
decimal
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
function joinDecimalString(parts) {
|
|
255
|
+
const integer = parts.integer === "" ? "0" : parts.integer;
|
|
256
|
+
const decimal = parts.decimal;
|
|
257
|
+
const body = decimal === "" ? integer : `${integer}.${decimal}`;
|
|
258
|
+
return `${parts.sign}${body}`;
|
|
259
|
+
}
|
|
260
|
+
function formatDecimalForDisplay(opts) {
|
|
261
|
+
const { value, scale, locale, currency, unit, useGrouping = currency !== void 0 || unit !== void 0 || scale !== void 0 } = opts;
|
|
262
|
+
if (value === null || value === void 0 || value === "") return "";
|
|
263
|
+
let canonical;
|
|
264
|
+
if (typeof value === "number") {
|
|
265
|
+
if (!Number.isFinite(value)) return "";
|
|
266
|
+
canonical = String(value);
|
|
267
|
+
} else canonical = value;
|
|
268
|
+
if (!/^[+-]?\d*(\.\d*)?$/.test(canonical) || canonical === "." || canonical === "-") return canonical;
|
|
269
|
+
if (scale !== void 0 && scale >= 0) canonical = enforceScale(canonical, scale);
|
|
270
|
+
const parts = splitDecimalString(canonical);
|
|
271
|
+
const asNumber = Number(canonical);
|
|
272
|
+
const intlSafe = Number.isFinite(asNumber);
|
|
273
|
+
if (currency) {
|
|
274
|
+
try {
|
|
275
|
+
const fmt = getNumberFormat(locale, {
|
|
276
|
+
style: "currency",
|
|
277
|
+
currency,
|
|
278
|
+
currencyDisplay: "narrowSymbol",
|
|
279
|
+
minimumFractionDigits: scale,
|
|
280
|
+
maximumFractionDigits: scale,
|
|
281
|
+
useGrouping
|
|
282
|
+
});
|
|
283
|
+
if (intlSafe) return fmt.format(asNumber);
|
|
284
|
+
} catch {}
|
|
285
|
+
return `${currency} ${formatPlain(parts, scale, locale, useGrouping)}`;
|
|
286
|
+
}
|
|
287
|
+
const base = formatPlain(parts, scale, locale, useGrouping);
|
|
288
|
+
return unit ? `${base} ${unit}` : base;
|
|
289
|
+
}
|
|
290
|
+
function formatPlain(parts, scale, locale, useGrouping) {
|
|
291
|
+
const intStr = useGrouping ? groupInteger(parts.integer, locale) : parts.integer;
|
|
292
|
+
const decStr = scale !== void 0 && scale >= 0 ? parts.decimal.padEnd(scale, "0").slice(0, scale) : parts.decimal;
|
|
293
|
+
const decSep = getDecimalSeparator(locale);
|
|
294
|
+
const body = decStr.length > 0 ? `${intStr}${decSep}${decStr}` : intStr;
|
|
295
|
+
return `${parts.sign}${body}`;
|
|
296
|
+
}
|
|
297
|
+
/**
|
|
298
|
+
* Insert the locale's thousands separator into a plain integer string.
|
|
299
|
+
* Pure string-based — no float math, handles arbitrary length.
|
|
300
|
+
*/
|
|
301
|
+
function groupInteger(integer, locale) {
|
|
302
|
+
const sep = getThousandsSeparator(locale);
|
|
303
|
+
if (!sep) return integer;
|
|
304
|
+
if (integer.length <= 3) return integer;
|
|
305
|
+
const out = [];
|
|
306
|
+
let i = integer.length;
|
|
307
|
+
while (i > 3) {
|
|
308
|
+
out.unshift(integer.slice(i - 3, i));
|
|
309
|
+
i -= 3;
|
|
310
|
+
}
|
|
311
|
+
out.unshift(integer.slice(0, i));
|
|
312
|
+
return out.join(sep);
|
|
313
|
+
}
|
|
314
|
+
//#endregion
|
|
315
|
+
export { enforceScale, formatDecimalForDisplay, getCurrencyDecimals, getCurrencyDisplayParts, getDecimalSeparator, getThousandsSeparator, groupInteger, joinDecimalString, parseDecimalInput, splitDecimalString };
|