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 <xyhp915@qq.com>
pull/10205/head
Jeffrey 'jf' Lim 2023-09-12 18:54:43 +08:00 committed by GitHub
parent a7dc7c42ca
commit c3836a7820
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 4 additions and 10 deletions

View File

@ -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