@toreda/time
Version:
Simple, small footprint library for common time operations and converting between units of time.
23 lines (22 loc) • 1.1 kB
TypeScript
import type { TimeUnit } from '../unit';
/**
* Parse a duration value into a number expressed in `units`. Accepts:
* - finite numbers (returned as-is when in safe range)
* - pure numeric strings ('5', '-1.5', '+30') — interpreted as a duration in `units`
* - simple suffix form ('5m', '-1.5h', '+30s') — converted to `units`
* - compound suffix form ('1h30m', '2d4h30m') — components must be in
* descending unit order with no repeats and no whitespace; sign applies
* to the whole expression, not per component
*
* Returns null for empty / whitespace-only strings, NaN/Infinity, date-like
* strings (those containing '-', '/', ':', or 'T' — use `timeValueParseDate`
* for those), unit aliases like '5min', and any result outside the safe
* integer range.
*
* @param value Duration value to parse.
* @param units Target unit; pure numeric strings are read as this unit
* and suffix components are converted into this unit.
*
* @category Time Parsing
*/
export declare function timeValueParse(value: number | string, units: TimeUnit): number | null;