aboutsummaryrefslogtreecommitdiff
path: root/src/utils/duration-string-to-seconds.ts
diff options
context:
space:
mode:
authorMax Isom <[email protected]>2022-03-13 18:30:36 -0400
committerGitHub <[email protected]>2022-03-13 17:30:36 -0500
commit6c00727a4a22e28c45711b301c7bf4dbaff90117 (patch)
treec23696f95ce0ef9a4f31586af96021329dedf292 /src/utils/duration-string-to-seconds.ts
parent03d5cfffd1a9cd8a9977de0d24cce1be0cc73210 (diff)
downloadmuse-6c00727a4a22e28c45711b301c7bf4dbaff90117.tar.xz
muse-6c00727a4a22e28c45711b301c7bf4dbaff90117.zip
Parse duration strings for /fseek and /seek (#565)
Diffstat (limited to 'src/utils/duration-string-to-seconds.ts')
-rw-r--r--src/utils/duration-string-to-seconds.ts21
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;