convertToUnit

Commons
stable

Normalise a CSS length value: numbers become "Npx" (or any unit), CSS strings pass through untouched, and null/empty/non-finite inputs return undefined.

API

Signature

Full TypeScript signature sourced from the util file.

typescript

API

Parameters

Input parameters accepted by this util.

str
required
string | number | null | undefined
The raw value to convert. A finite number is suffixed with the unit; a string is returned as-is; null, empty string, or a non-finite number returns undefined.
unit
string
The CSS unit to append when the input is a numeric value.

API

Return value

The value returned by this util.

The normalised CSS length, or undefined when the input is null, empty, or non-finite.

Usage

Examples

Ready-to-paste usage snippets.

Numbers and CSS strings

typescript