From c3836a7820f8b4fd6d903be9bfdc9c8e8aa96e28 Mon Sep 17 00:00:00 2001 From: Jeffrey 'jf' Lim Date: Tue, 12 Sep 2023 18:54:43 +0800 Subject: [PATCH] youtube.cljs: allow for single-digit minutes and seconds in youtube-timestamp (addresses #9920) (#9930) * youtube.cljs: allow for single-digit minutes and seconds in youtube-timestamp (addresses #9920) * youtube.cljs: parse ":" as MM:SS to be consistent with the UI display *NOTE that with this commit, we essentially pass util/safe-parse-int a value we do not say it can take (although it works): nil. This will be fixed in a subsequent commit. * youtube.cljs: remove unreachable branches from cond in parse-timestamp * frontend/util.cljc: update safe-parse-int to accept nil as well. TODO: need help fixing malli schema! * fix: parse timestamp --------- Co-authored-by: charlie --- src/main/frontend/extensions/video/youtube.cljs | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/src/main/frontend/extensions/video/youtube.cljs b/src/main/frontend/extensions/video/youtube.cljs index 1779b6580e..e1e96fdaba 100644 --- a/src/main/frontend/extensions/video/youtube.cljs +++ b/src/main/frontend/extensions/video/youtube.cljs @@ -119,13 +119,13 @@ Remember: You can paste a raw YouTube url as embedded video on mobile." (defn parse-timestamp [timestamp] - (let [reg #"^(?:(\d+):)?([0-5]\d):([0-5]\d)$" + (let [reg #"^(?:(\d+):)?([0-5]?\d):([0-5]?\d)$" reg-number #"^\d+$" timestamp (str timestamp) total-seconds (some-> (re-matches reg-number timestamp) util/safe-parse-int) [_ hours minutes seconds] (re-matches reg timestamp) - [hours minutes seconds] (map util/safe-parse-int (remove nil? [hours minutes seconds]))] + [hours minutes seconds] (map #(if (nil? %) 0 (util/safe-parse-int %)) [hours minutes seconds])] (cond total-seconds total-seconds @@ -133,19 +133,13 @@ Remember: You can paste a raw YouTube url as embedded video on mobile." (and minutes seconds) (+ (* 3600 hours) (* 60 minutes) seconds) - minutes - (+ (* 3600 hours) (* 60 minutes)) - - hours - (* 3600 hours) - :else nil))) (comment ;; hh:mm:ss - (re-matches #"^(?:(\d+):)?([0-5]\d):([0-5]\d)$" "123:22:23") ;; => ["123:22:23" "123" "22" "23"] - (re-matches #"^(?:(\d+):)?([0-5]\d):([0-5]\d)$" "30:23") ;; => ["30:23" nil "30" "23"] + (re-matches #"^(?:(\d+):)?([0-5]?\d):([0-5]?\d)$" "123:22:23") ;; => ["123:22:23" "123" "22" "23"] + (re-matches #"^(?:(\d+):)?([0-5]?\d):([0-5]?\d)$" "30:23") ;; => ["30:23" nil "30" "23"] (parse-timestamp "01:23") ;; => 83