aboutsummaryrefslogtreecommitdiff
path: root/src/utils/duration-string-to-seconds.ts
blob: 588ba5bd3bd22b369a85efa2a89c0c63dda74442 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import parse from 'parse-duration';

/**
 * Parse duration strings to seconds.
 * @param str any common duration format, like 1m or 1hr 30s. If the input is a number it's assumed to be in seconds.
 * @returns seconds
 */
const durationStringToSeconds = (str: string) => {
  let seconds;
  const isInputSeconds = Boolean(/\d+$/.exec(str));

  if (isInputSeconds) {
    seconds = Number.parseInt(str, 10);
  } else {
    seconds = parse(str) / 1000;
  }

  return seconds;
};

export default durationStringToSeconds;