diff options
| author | Max Isom <[email protected]> | 2022-03-13 18:30:36 -0400 |
|---|---|---|
| committer | GitHub <[email protected]> | 2022-03-13 17:30:36 -0500 |
| commit | 6c00727a4a22e28c45711b301c7bf4dbaff90117 (patch) | |
| tree | c23696f95ce0ef9a4f31586af96021329dedf292 /src/utils | |
| parent | 03d5cfffd1a9cd8a9977de0d24cce1be0cc73210 (diff) | |
| download | muse-6c00727a4a22e28c45711b301c7bf4dbaff90117.tar.xz muse-6c00727a4a22e28c45711b301c7bf4dbaff90117.zip | |
Parse duration strings for /fseek and /seek (#565)
Diffstat (limited to 'src/utils')
| -rw-r--r-- | src/utils/duration-string-to-seconds.ts | 21 |
1 files changed, 21 insertions, 0 deletions
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; |
