( value: string | undefined, config?: NumericInputConfig, )
| 538 | } |
| 539 | |
| 540 | export function normalizeNumericValue( |
| 541 | value: string | undefined, |
| 542 | config?: NumericInputConfig, |
| 543 | ): string { |
| 544 | const trimmed = value?.trim() ?? ""; |
| 545 | if (!trimmed) return ""; |
| 546 | |
| 547 | const parsed = Number(trimmed); |
| 548 | if (!Number.isFinite(parsed)) return ""; |
| 549 | |
| 550 | const min = config?.min; |
| 551 | const max = config?.max; |
| 552 | let normalized = parsed; |
| 553 | if (min !== undefined) normalized = Math.max(min, normalized); |
| 554 | if (max !== undefined) normalized = Math.min(max, normalized); |
| 555 | |
| 556 | if (config?.step !== undefined && config.step > 0) { |
| 557 | const base = min ?? 0; |
| 558 | const stepsFromBase = Math.round((normalized - base) / config.step); |
| 559 | normalized = base + stepsFromBase * config.step; |
| 560 | if (min !== undefined) normalized = Math.max(min, normalized); |
| 561 | if (max !== undefined) normalized = Math.min(max, normalized); |
| 562 | const precision = Math.max( |
| 563 | decimalPlaces(base), |
| 564 | decimalPlaces(config.step), |
| 565 | ); |
| 566 | return formatRoundedNumber(normalized, precision); |
| 567 | } |
| 568 | |
| 569 | return String(normalized); |
| 570 | } |
| 571 | |
| 572 | export function normalizeSliderValue( |
| 573 | value: string | undefined, |
no test coverage detected