From 6c00727a4a22e28c45711b301c7bf4dbaff90117 Mon Sep 17 00:00:00 2001 From: Max Isom Date: Sun, 13 Mar 2022 18:30:36 -0400 Subject: Parse duration strings for /fseek and /seek (#565) --- src/utils/duration-string-to-seconds.ts | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 src/utils/duration-string-to-seconds.ts (limited to 'src/utils') diff --git a/src/utils/duration-string-to-seconds.ts b/src/utils/duration-string-to-seconds.ts new file mode 100644 index 0000000..588ba5b --- /dev/null +++ b/src/utils/duration-string-to-seconds.ts @@ -0,0 +1,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; -- cgit v1.2.3