Skip to content

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'); // 1000
millisecondsFromString('2s:500ms'); // 2500
millisecondsFromString('2s-500ms', { separator: '-' }); // 2500
millisecondsFromString('1second', {
unitsAlias: { second: 's' },
}); // 1000

Definition

function millisecondsFromString(
string: string,
options?: {
separator?: string;
unitsAlias?: Record<string, TimeUnitsNames>;
},
): number { }

Parameters

  1. string
    type 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.
  2. options
    type 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 AppError for unsupported parts or unit names.