aboutsummaryrefslogtreecommitdiff
path: root/src/utils
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils')
-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;