| 39 | * @see [Built-in transformations](guide/components/inputs#built-in-transformations) |
| 40 | */ |
| 41 | export function numberAttribute(value: unknown, fallbackValue = NaN): number { |
| 42 | // parseFloat(value) handles most of the cases we're interested in (it treats null, empty string, |
| 43 | // and other non-number values as NaN, where Number just uses 0) but it considers the string |
| 44 | // '123hello' to be a valid number. Therefore we also check if Number(value) is NaN. |
| 45 | const isNumberValue = !isNaN(parseFloat(value as any)) && !isNaN(Number(value)); |
| 46 | return isNumberValue ? Number(value) : fallbackValue; |
| 47 | } |