unit
Interfaces
UnitFormatOptions
Defined in: unit.ts:34
Properties
base?
optional base?: SIPrefix;
Defined in: unit.ts:43
The prefix the stored value is already in.
A control that keeps milliseconds in value is { base: 'm' } with a
unit of 's': 1500 then displays as 1.5s, and parse gives 1500 back.
Default
''
digits?
optional digits?: number;
Defined in: unit.ts:57
Digits after the decimal point. The number is left as-is when omitted.
prefixes?
optional prefixes?: boolean;
Defined in: unit.ts:53
Whether to scale the number and pick a prefix at all.
Turn it off for anything that is not an SI quantity. dB, %, cents and
semitones do not take prefixes, and -6dB read as "-6 deci-B" is wrong
rather than merely unusual.
Default
true
separator?
optional separator?: string;
Defined in: unit.ts:62
Text placed between the number and the unit.
Default
''
UnitFormatter
Defined in: unit.ts:66
The format / parse pair a NumberInput takes.
Properties
format
format: (value) => string;
Defined in: unit.ts:67
Parameters
| Parameter | Type |
|---|---|
value | number |
Returns
string
parse
parse: (text) => number;
Defined in: unit.ts:68
Parameters
| Parameter | Type |
|---|---|
text | string |
Returns
number
Type Aliases
SIPrefix
type SIPrefix = "p" | "n" | "µ" | "m" | "" | "k" | "M" | "G";
Defined in: unit.ts:10
The SI prefixes unitFormat chooses between.
Deliberately narrower than the full SI set: yocto through yotta are of no
use to an audio control, and every extra prefix is one more symbol parse
has to tell apart from a unit.
Functions
unitFormat()
function unitFormat(unit, options?): UnitFormatter;
Defined in: unit.ts:101
Build the format and parse of a unit, as one pair.
They are returned together because they have to agree: a format that
writes 1.23kHz is only useful next to a parse that reads it back as
1230. Spread the result into the input.
Parameters
| Parameter | Type |
|---|---|
unit | string |
options | UnitFormatOptions |
Returns
Examples
unitFormat('Hz') // 1234 -> '1.23kHz'
unitFormat('s', { base: 'm' }) // value in ms. 1500 -> '1.5s'
unitFormat('s', { base: 'm', digits: 2 }) // 1500 -> '1.50s'
unitFormat('dB', { prefixes: false, digits: 1 }) // -6.25 -> '-6.3dB'
<NumberInput.Root {...unitFormat('Hz', { digits: 2 })} value={v} onChange={setV}>