millisecondsFromString
type: function
The millisecondsFromString function parses time-unit strings and returns total milliseconds.
The
millisecondsFromStringErrorScope app error scope array
is exported by this utility and can be used to filter thrown
AppError
by scope for this specific utility.
To learn more about error handling please refer to
error handling section
of the getting started guide, and the AppError
documentation.
Usage
import { millisecondsFromString } from '@mustib/utils';
millisecondsFromString('1s'); // 1000millisecondsFromString('2s:500ms'); // 2500millisecondsFromString('2s-500ms', { separator: '-' }); // 2500millisecondsFromString('1second', { unitsAlias: { second: 's' },}); // 1000Definition
function millisecondsFromString( string: string, options?: { separator?: string; unitsAlias?: Record<string, TimeUnitsNames>; },): number { }Parameters
-
stringtype string = string;- Time string to parse (for example `1s:500ms`).
- Returns `0` for an empty string.
- Supported units are listed in Units and their Values.
-
optionstype options = {separator?: string,unitsAlias?: Record<string, TimeUnitsNames>,};options.separator- Delimiter between parts.
- default: ':'
options.unitsAlias- Custom aliases mapped to canonical units.
- since: v2.1.0
Returns
type T = number;- Total milliseconds represented by the input string.
Error behavior
- Throws scoped
AppErrorfor unsupported parts or unit names.