Pārlūkot izejas kodu

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 "<number>:<number>" 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 <[email protected]>
Jeffrey 'jf' Lim 2 gadi atpakaļ
vecāks
revīzija
c3836a7820
1 mainītis faili ar 4 papildinājumiem un 10 dzēšanām
  1. 4 10
      src/main/frontend/extensions/video/youtube.cljs

+ 4 - 10
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