@iyulab/components 2.35.1 → 2.36.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.
Files changed (74) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/dist/_virtual/{_@oxc-project_runtime@0.155.0 → _@oxc-project_runtime@0.156.0}/helpers/esm/decorate.js +1 -1
  3. package/dist/_virtual/{_@oxc-project_runtime@0.155.0 → _@oxc-project_runtime@0.156.0}/helpers/esm/decorateMetadata.js +1 -1
  4. package/dist/components/UFloatingElement.js +3 -3
  5. package/dist/components/UFormControlElement.js +2 -2
  6. package/dist/components/UOverlayElement.js +3 -3
  7. package/dist/components/alert/UAlert.js +6 -6
  8. package/dist/components/auto-scroll/UAutoScroll.js +2 -2
  9. package/dist/components/avatar/UAvatar.js +2 -2
  10. package/dist/components/badge/UBadge.js +4 -4
  11. package/dist/components/breadcrumb/UBreadcrumb.js +3 -3
  12. package/dist/components/breadcrumb-item/UBreadcrumbItem.js +3 -3
  13. package/dist/components/button/UButton.js +3 -3
  14. package/dist/components/button/UButton.styles.js +6 -2
  15. package/dist/components/button-group/UButtonGroup.js +2 -2
  16. package/dist/components/calendar/UCalendar.js +4 -4
  17. package/dist/components/calendar/datetime.js +1 -1
  18. package/dist/components/card/UCard.js +2 -2
  19. package/dist/components/carousel/UCarousel.js +5 -5
  20. package/dist/components/carousel/UCarousel.styles.js +4 -0
  21. package/dist/components/checkbox/UCheckbox.js +4 -4
  22. package/dist/components/chip/UChip.js +6 -6
  23. package/dist/components/command-palette/UCommandPalette.js +6 -6
  24. package/dist/components/copy-button/UCopyButton.js +5 -5
  25. package/dist/components/date-picker/UDatePicker.js +8 -8
  26. package/dist/components/date-range-picker/UDateRangePicker.js +9 -9
  27. package/dist/components/dialog/UDialog.js +4 -4
  28. package/dist/components/divider/UDivider.js +2 -2
  29. package/dist/components/drawer/UDrawer.js +4 -4
  30. package/dist/components/expander/UExpander.js +3 -3
  31. package/dist/components/field/UField.js +4 -4
  32. package/dist/components/file-input/UFileInput.js +5 -5
  33. package/dist/components/form/UForm.js +3 -3
  34. package/dist/components/icon/UIcon.js +2 -2
  35. package/dist/components/icon-button/UIconButton.js +4 -4
  36. package/dist/components/input/UInput.js +9 -9
  37. package/dist/components/menu/UMenu.js +3 -3
  38. package/dist/components/menu-item/UMenuItem.js +5 -5
  39. package/dist/components/option/UOption.js +4 -4
  40. package/dist/components/option-group/UOptionGroup.js +2 -2
  41. package/dist/components/pagination/UPagination.js +5 -5
  42. package/dist/components/panel/UPanel.js +2 -2
  43. package/dist/components/popover/UPopover.js +3 -3
  44. package/dist/components/progress-bar/UProgressBar.js +2 -2
  45. package/dist/components/progress-ring/UProgressRing.js +2 -2
  46. package/dist/components/radio/URadio.js +4 -4
  47. package/dist/components/rating/URating.js +5 -5
  48. package/dist/components/select/USelect.js +10 -10
  49. package/dist/components/skeleton/USkeleton.js +2 -2
  50. package/dist/components/slider/USlider.js +5 -5
  51. package/dist/components/spinner/USpinner.js +4 -4
  52. package/dist/components/split-panel/USplitPanel.js +4 -4
  53. package/dist/components/steps/USteps.js +3 -3
  54. package/dist/components/switch/USwitch.js +3 -3
  55. package/dist/components/tab/UTab.js +6 -6
  56. package/dist/components/tab-nav/UTabNav.js +2 -2
  57. package/dist/components/tab-panel/UTabPanel.js +4 -4
  58. package/dist/components/tag/UTag.js +3 -3
  59. package/dist/components/text/UText.js +2 -2
  60. package/dist/components/textarea/UTextarea.js +4 -4
  61. package/dist/components/tooltip/UTooltip.js +3 -3
  62. package/dist/components/tree/UTree.js +3 -3
  63. package/dist/components/tree-item/UTreeItem.js +6 -6
  64. package/dist/index.d.ts +1 -0
  65. package/dist/index.js +26 -25
  66. package/dist/utilities/Dialog.js +2 -2
  67. package/dist/utilities/Theme.js +1 -1
  68. package/dist/utilities/Toast.js +1 -1
  69. package/dist/utilities/numberFormat.d.ts +23 -0
  70. package/dist/utilities/numberFormat.js +260 -0
  71. package/package.json +1 -1
  72. package/skills/iyulab-components/SKILL.md +1 -0
  73. package/skills/iyulab-components/references/utilities/format.md +2 -0
  74. package/skills/iyulab-components/references/utilities/number-format.md +55 -0
@@ -0,0 +1,260 @@
1
+ import { Locale } from "./Locale.js";
2
+ // src/utilities/numberFormat.ts
3
+ /**
4
+ * Number formats shared by the table, sheet and widget packages: spreadsheet format codes (the subset of
5
+ * ECMA-376 §18.8.31 a list needs) and `Intl.NumberFormat` options — see {@link applyNumberFormat}.
6
+ *
7
+ * Format codes:
8
+ *
9
+ * Supported: up to four `;` sections (positive;negative;zero;text) · digit placeholders `0` `#` `?`
10
+ * with `.` and grouping `,` · trailing `,` scaling (÷1000 each) · `%` · quoted literals `"…"` ·
11
+ * escaped literals `\x` · alignment space `_x` · fill `*x` (ignored) · colors `[Red]` `[Green]`
12
+ * `[Blue]` `[Yellow]` `[Black]` · currency `[$₩-412]` · `@` in the text section · any other
13
+ * character as a literal (so `#,##0원` keeps its unit).
14
+ *
15
+ * Not supported (the caller shows the value unformatted): conditions `[>100]`, `[Color N]`,
16
+ * scientific `E+`, fractions `# ?/?`.
17
+ */
18
+ var COLOR_TONES = {
19
+ red: "danger",
20
+ green: "success",
21
+ blue: "info",
22
+ yellow: "warning",
23
+ black: null
24
+ };
25
+ var cache = /* @__PURE__ */ new Map();
26
+ /** Split on `;` outside quotes, brackets and escapes. */
27
+ function splitSections(code) {
28
+ const out = [];
29
+ let cur = "";
30
+ let quoted = false;
31
+ let bracket = false;
32
+ for (let i = 0; i < code.length; i++) {
33
+ const ch = code[i];
34
+ if (quoted) {
35
+ cur += ch;
36
+ if (ch === "\"") quoted = false;
37
+ } else if (bracket) {
38
+ cur += ch;
39
+ if (ch === "]") bracket = false;
40
+ } else if (ch === "\\" || ch === "_" || ch === "*") {
41
+ cur += ch + (code[i + 1] ?? "");
42
+ i++;
43
+ } else if (ch === "\"") {
44
+ quoted = true;
45
+ cur += ch;
46
+ } else if (ch === "[") {
47
+ bracket = true;
48
+ cur += ch;
49
+ } else if (ch === ";") {
50
+ out.push(cur);
51
+ cur = "";
52
+ } else cur += ch;
53
+ }
54
+ out.push(cur);
55
+ return out;
56
+ }
57
+ function parseSection(src) {
58
+ const tokens = [];
59
+ let literal = "";
60
+ let tone;
61
+ let intZeros = 0;
62
+ let minFraction = 0;
63
+ let maxFraction = 0;
64
+ let grouping = false;
65
+ let scale = 0;
66
+ let percent = false;
67
+ let numberSeen = false;
68
+ let inFraction = false;
69
+ let numberClosed = false;
70
+ let pendingCommas = 0;
71
+ const flushLiteral = () => {
72
+ if (literal) tokens.push({
73
+ kind: "literal",
74
+ text: literal
75
+ });
76
+ literal = "";
77
+ };
78
+ const closeNumber = () => {
79
+ if (numberSeen && !numberClosed) {
80
+ scale += pendingCommas;
81
+ pendingCommas = 0;
82
+ numberClosed = true;
83
+ }
84
+ };
85
+ for (let i = 0; i < src.length; i++) {
86
+ const ch = src[i];
87
+ if (ch === "\"") {
88
+ closeNumber();
89
+ const end = src.indexOf("\"", i + 1);
90
+ literal += src.slice(i + 1, end === -1 ? src.length : end);
91
+ i = end === -1 ? src.length : end;
92
+ } else if (ch === "\\") {
93
+ closeNumber();
94
+ literal += src[i + 1] ?? "";
95
+ i++;
96
+ } else if (ch === "_") {
97
+ closeNumber();
98
+ literal += " ";
99
+ i++;
100
+ } else if (ch === "*") i++;
101
+ else if (ch === "[") {
102
+ const end = src.indexOf("]", i);
103
+ if (end === -1) return null;
104
+ const inner = src.slice(i + 1, end);
105
+ i = end;
106
+ if (inner.startsWith("$")) {
107
+ closeNumber();
108
+ const dash = inner.indexOf("-");
109
+ literal += inner.slice(1, dash === -1 ? void 0 : dash);
110
+ continue;
111
+ }
112
+ const key = inner.toLowerCase();
113
+ if (key in COLOR_TONES) {
114
+ tone = COLOR_TONES[key] ?? void 0;
115
+ continue;
116
+ }
117
+ return null;
118
+ } else if (ch === "0" || ch === "#" || ch === "?") {
119
+ if (numberClosed) return null;
120
+ if (!numberSeen) {
121
+ flushLiteral();
122
+ tokens.push({ kind: "number" });
123
+ numberSeen = true;
124
+ }
125
+ if (pendingCommas) {
126
+ grouping = true;
127
+ pendingCommas = 0;
128
+ }
129
+ if (inFraction) {
130
+ maxFraction++;
131
+ if (ch === "0") minFraction++;
132
+ } else if (ch === "0") intZeros++;
133
+ } else if (ch === "." && numberSeen && !numberClosed && !inFraction) inFraction = true;
134
+ else if (ch === "." && !numberSeen && /[0#?]/.test(src[i + 1] ?? "")) {
135
+ flushLiteral();
136
+ tokens.push({ kind: "number" });
137
+ numberSeen = true;
138
+ inFraction = true;
139
+ } else if (ch === "," && numberSeen && !numberClosed) pendingCommas++;
140
+ else if (ch === "%") {
141
+ closeNumber();
142
+ percent = true;
143
+ literal += "%";
144
+ } else if ((ch === "E" || ch === "e") && /[+-]/.test(src[i + 1] ?? "")) return null;
145
+ else if (ch === "/" && numberSeen) return null;
146
+ else if (ch === "@") {
147
+ closeNumber();
148
+ flushLiteral();
149
+ tokens.push({ kind: "text" });
150
+ } else {
151
+ closeNumber();
152
+ literal += ch;
153
+ }
154
+ }
155
+ closeNumber();
156
+ flushLiteral();
157
+ return {
158
+ tokens,
159
+ tone,
160
+ intZeros,
161
+ minFraction,
162
+ maxFraction,
163
+ grouping,
164
+ scale,
165
+ percent,
166
+ hasNumber: numberSeen
167
+ };
168
+ }
169
+ function parseFormat(code) {
170
+ if (cache.has(code)) return cache.get(code);
171
+ const parts = splitSections(code);
172
+ let parsed = null;
173
+ if (parts.length <= 4) {
174
+ const sections = [];
175
+ for (const p of parts) {
176
+ const s = parseSection(p);
177
+ if (!s) {
178
+ sections.length = 0;
179
+ break;
180
+ }
181
+ sections.push(s);
182
+ }
183
+ if (sections.length === parts.length) parsed = { sections };
184
+ }
185
+ cache.set(code, parsed);
186
+ return parsed;
187
+ }
188
+ function renderDigits(abs, s, locale) {
189
+ let n = abs;
190
+ if (s.percent) n *= 100;
191
+ if (s.scale) n /= 1e3 ** s.scale;
192
+ const text = new Intl.NumberFormat(locale, {
193
+ minimumIntegerDigits: Math.max(1, s.intZeros),
194
+ minimumFractionDigits: s.minFraction,
195
+ maximumFractionDigits: s.maxFraction,
196
+ useGrouping: s.grouping
197
+ }).format(n);
198
+ if (s.intZeros === 0 && /^0(?=\D)/.test(text)) return text.slice(1);
199
+ if (s.intZeros === 0 && text === "0") return "";
200
+ return text;
201
+ }
202
+ function renderSection(s, abs, text, locale) {
203
+ let out = "";
204
+ for (const t of s.tokens) if (t.kind === "literal") out += t.text;
205
+ else if (t.kind === "number") out += renderDigits(abs, s, locale);
206
+ else out += text;
207
+ return out;
208
+ }
209
+ function formatCode(value, code, locale) {
210
+ const parsed = parseFormat(code);
211
+ if (!parsed) return null;
212
+ const { sections } = parsed;
213
+ const num = toNumber(value);
214
+ if (Number.isNaN(num)) {
215
+ const textSection = sections[3];
216
+ if (!textSection) return { text: String(value) };
217
+ return {
218
+ text: renderSection(textSection, 0, String(value), locale),
219
+ tone: textSection.tone
220
+ };
221
+ }
222
+ let section = sections[0];
223
+ let sign = "";
224
+ if (num < 0 && sections.length >= 2) section = sections[1];
225
+ else if (num === 0 && sections.length >= 3) section = sections[2];
226
+ else if (num < 0) sign = "-";
227
+ const body = renderSection(section, Math.abs(num), String(value), locale);
228
+ return {
229
+ text: (sign && section.hasNumber && !/[1-9]/.test(renderDigits(Math.abs(num), section, locale)) ? "" : sign) + body,
230
+ tone: section.tone
231
+ };
232
+ }
233
+ function toNumber(value) {
234
+ return typeof value === "number" ? value : typeof value === "string" && value.trim() !== "" ? Number(value) : NaN;
235
+ }
236
+ /**
237
+ * Formats `value` with a {@link NumberFormat} — the one number-format vocabulary the table, sheet and
238
+ * widget packages share. A format code keeps its section color as a theme {@link FormatTone}.
239
+ *
240
+ * - `null` / `undefined` → empty text.
241
+ * - A number or numeric text is formatted; other text goes through a code's text section, or is shown as is.
242
+ * - Neither path writes a signed zero: a negative that rounds to zero reads `0`, as in Excel. For options,
243
+ * that is `signDisplay: 'negative'` unless the options set `signDisplay` themselves.
244
+ * - Returns `null` when a format code uses syntax this engine does not support (conditions `[>100]`,
245
+ * `[Color N]`, scientific `E+`, fractions) — the caller decides how to show the value then.
246
+ *
247
+ * Separators follow `locale`, or the app locale (`Locale.get()`) when omitted.
248
+ */
249
+ function applyNumberFormat(value, format, locale) {
250
+ if (value == null) return { text: "" };
251
+ const loc = locale ?? Locale.get();
252
+ if (typeof format === "string") return formatCode(value, format, loc);
253
+ const num = toNumber(value);
254
+ if (Number.isNaN(num)) return { text: String(value) };
255
+ return { text: new Intl.NumberFormat(loc, {
256
+ signDisplay: "negative",
257
+ ...format
258
+ }).format(num) };
259
+ }
260
+ export { applyNumberFormat };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@iyulab/components",
3
3
  "description": "web-components library based on lit-element made by iyulab",
4
- "version": "2.35.1",
4
+ "version": "2.36.0",
5
5
  "keywords": [
6
6
  "iyulab",
7
7
  "components",
@@ -149,6 +149,7 @@ set on the host yourself always wins and is never overwritten.
149
149
  - [`Locale`](./references/utilities/locale.md) — Validation-message locale registry and lookup utility · search matching (`matchesSearch` — Hangul jamo and initials)
150
150
  - [`elements`](./references/utilities/elements.md) — Shadow-DOM-aware DOM query helpers
151
151
  - [`format`](./references/utilities/format.md) — Locale-aware number, currency and date formatting
152
+ - [`number-format`](./references/utilities/number-format.md) — One number format for tables, sheets and widgets: spreadsheet codes or `Intl` options
152
153
  - [`clipboard`](./references/utilities/clipboard.md) — `copyFromKey` / `pasteFromKey` / `isTextEntry`, Ctrl/Cmd + C · X · V for a grid or list selection in every browser (Safari fires no copy event without a text selection)
153
154
  - [`keyboard`](./references/utilities/keyboard.md) — `isImeComposing` for Enter handlers that must not fire mid-composition (Korean/Japanese/Chinese input) · `CompositionTracker` / `replayInput` for inputs that filter as you type
154
155
  - [`tsv`](./references/utilities/tsv.md) — `encodeTsv` / `decodeTsv`, the spreadsheet clipboard format with RFC 4180 quoting
@@ -12,6 +12,8 @@ They add no formatting rules of their own — everything `Intl` accepts is passe
12
12
  through. Reach for them instead of calling `Intl` directly when you want the active locale
13
13
  applied without threading it through every call site.
14
14
 
15
+ For spreadsheet-style format codes (`#,##0;[Red](#,##0)`), see [`number-format`](./number-format.md).
16
+
15
17
  ## Functions
16
18
 
17
19
  ### `formatNumber(value, options?, locale?)`
@@ -0,0 +1,55 @@
1
+ # number-format
2
+
3
+ ```ts
4
+ import { applyNumberFormat, type NumberFormat } from '@iyulab/components';
5
+ ```
6
+
7
+ Without the barrel's component registrations:
8
+
9
+ ```ts
10
+ import { applyNumberFormat } from '@iyulab/components/dist/utilities/numberFormat.js';
11
+ ```
12
+
13
+ One number-format vocabulary for tables, sheets and widgets. A `NumberFormat` is either a
14
+ **spreadsheet format code** (the subset of Excel's number formats a list needs) or
15
+ **`Intl.NumberFormat` options** — the same two shapes a user brings from a spreadsheet or
16
+ from code.
17
+
18
+ ## `applyNumberFormat(value, format, locale?)`
19
+
20
+ Returns `{ text, tone? }`, or `null` when a format code uses syntax the engine does not
21
+ support.
22
+
23
+ ```ts
24
+ applyNumberFormat(1234.5, '#,##0.00'); // { text: '1,234.50' } in an en locale
25
+ applyNumberFormat(-1234, '#,##0;[Red](#,##0)'); // { text: '(1,234)', tone: 'danger' }
26
+ applyNumberFormat(1234, '#,##0원'); // { text: '1,234원' }
27
+ applyNumberFormat(0.256, { style: 'percent', maximumFractionDigits: 1 }); // { text: '25.6%' }
28
+ applyNumberFormat(0.00007729, { maximumSignificantDigits: 4 }); // { text: '0.00007729' }
29
+ applyNumberFormat(5, '0.00E+00'); // null — scientific is not supported
30
+ ```
31
+
32
+ - `value`: a number or numeric text is formatted. Other text goes through a code's text
33
+ section (the fourth), or is returned as is. `null` / `undefined` give `''`.
34
+ - `locale`: overrides the active locale (`Locale.get()`) for this call — separators follow it.
35
+ - **No signed zero.** A negative that rounds to zero reads `0` on both paths (as Excel does).
36
+ With options that is `signDisplay: 'negative'`, unless the options set `signDisplay`.
37
+ - `tone`: a section color (`[Red]` `[Green]` `[Blue]` `[Yellow]`) becomes a status tone —
38
+ `'danger' | 'success' | 'info' | 'warning'` — so the caller paints it with theme colors,
39
+ not the literal color. `[Black]` is the default text color (no tone).
40
+
41
+ ### Format codes
42
+
43
+ | Supported | Example |
44
+ |---|---|
45
+ | Up to four `;` sections — positive;negative;zero;text | `#,##0;(#,##0);"-"` |
46
+ | Digit placeholders `0` `#` `?`, decimal `.`, grouping `,` | `#,##0.00` · `000` · `#.##` |
47
+ | Trailing `,` divides by 1000 each | `#,##0,,"M"` |
48
+ | `%` multiplies by 100 | `0.0%` |
49
+ | Literals: `"…"`, `\x`, any other character | `#,##0" 원"` · `#,##0원` |
50
+ | `_x` alignment space · `*x` fill (ignored) | `#,##0_)` |
51
+ | Section colors | `[Red]-#,##0` |
52
+ | Currency `[$sym-locale]` (the symbol is used) | `[$₩-412]#,##0` |
53
+ | `@` in the text section | `0;-0;0;"["@"]"` |
54
+
55
+ Not supported (returns `null`): conditions `[>100]`, `[Color N]`, scientific `E+`, fractions `# ?/?`.