Util

numAttr

Input-transform factory: coerces any bound value to a number with a fallback. The standard transform for numeric inputs across ngwr.

Usage

import { numAttr } from 'ngwr/utils';

readonly speed = input(4, { transform: numAttr(4) });

// <wr-thing speed="12" />   → 12 (string attribute coerced)
// <wr-thing [speed]="x" />  → x ?? 4

Why ngwr provides this

Angular's own numberAttribute falls back to NaN, which then needs guarding at every read. Wrapping coerceNumberProperty with a declared fallback keeps templates free to pass strings, numbers, or nothing — every numeric input in the kit uses it, so it's exported for app inputs too.

// Angular built-in: NaN fallback leaks into your math.
readonly a = input(0, { transform: numberAttribute }); // 'abc' → NaN

// ngwr: declared fallback.
readonly b = input(4, { transform: numAttr(4) });      // 'abc' → 4

API

NameDescriptionTypeDefault
numAttrReturns an input transform that coerces to number with a fallback.(fallback: number) => (value: unknown) => number