Browse Source

Rename non-boolean fns to not end in '?'

'?' usually indicate that the fn returns a boolean. This can be
confusing as seen by mistaken util/url? entry in
https://github.com/logseq/logseq/commit/2401ce9e9ab6ce3e7d2ab63ab4a1a7e49ec6f5ee#diff-d5720cb6221452c421a80ce9e8df86cce229e69ac53b10506161ffcfbf3d6889
Gabriel Horner 3 years ago
parent
commit
f25e49c8d0

+ 14 - 11
src/main/frontend/handler/link.cljs

@@ -1,13 +1,14 @@
 (ns frontend.handler.link
   (:require [frontend.util :as util]))
 
-(def plain-link "(?:http://www\\.|https://www\\.|http://|https://){1}[a-z0-9]+(?:[\\-\\.]{1}[a-z0-9]+)*\\.[a-z]{2,5}(?:.*)*")
+(def plain-link-re-string
+  "(?:http://www\\.|https://www\\.|http://|https://){1}[a-z0-9]+(?:[\\-\\.]{1}[a-z0-9]+)*\\.[a-z]{2,5}(?:.*)*")
 ;; Based on https://orgmode.org/manual/Link-Format.html#Link-Format
-(def org-link-re-1 (re-pattern (util/format "\\[\\[(%s)\\]\\[(.+)\\]\\]" plain-link)))
-(def org-link-re-2 (re-pattern (util/format "\\[\\[(%s)\\]\\]" plain-link)))
-(def markdown-link-re (re-pattern (util/format "^\\[(.+)\\]\\((%s)\\)" plain-link)))
+(def org-link-re-1 (re-pattern (util/format "\\[\\[(%s)\\]\\[(.+)\\]\\]" plain-link-re-string)))
+(def org-link-re-2 (re-pattern (util/format "\\[\\[(%s)\\]\\]" plain-link-re-string)))
+(def markdown-link-re (re-pattern (util/format "^\\[(.+)\\]\\((%s)\\)" plain-link-re-string)))
 
-(defn- org-link?
+(defn- org-link
   [link]
   (when-let [matches (or (re-matches org-link-re-1 link)
                          (re-matches org-link-re-2 link))]
@@ -15,21 +16,23 @@
      :url (second matches)
      :label (nth matches 2 nil)}))
 
-(defn- markdown-link?
+(defn- markdown-link
   [link]
   (when-let [matches (re-matches markdown-link-re link)]
     {:type "markdown-link"
      :url (nth matches 2)
      :label (second matches)}))
 
-(defn- plain-link?
+(defn- plain-link
   [link]
   (when (util/url? link)
     {:type "plain-link"
      :url link}))
 
-(defn link?
+(defn link
+  "If the given string is an org, markdown or plain url, return a map indicating
+  the url type, url itself and the optional label."
   [link]
-  (or (plain-link? link)
-      (org-link? link)
-      (markdown-link? link)))
+  (or (plain-link link)
+      (org-link link)
+      (markdown-link link)))

+ 1 - 1
src/main/frontend/text.cljs

@@ -379,7 +379,7 @@
       (contains? @non-parsing-properties (string/lower-case k))
       v
 
-      (link/link? v)
+      (link/link v)
       v
 
       :else

+ 6 - 6
src/test/frontend/handler/link_test.cljs

@@ -2,16 +2,16 @@
   (:require [cljs.test :refer [are deftest testing]]
             [frontend.handler.link :as link]))
 
-(deftest test-link?
+(deftest test-link
   (testing "non-link"
-    (are [x y] (= (link/link? x) y)
+    (are [x y] (= (link/link x) y)
       "google.com" nil
       "[[google.com][google]]" nil
       "[[google.com]]" nil
       "[google](google.com)" nil))
 
   (testing "plain links"
-    (are [x y] (= (link/link? x) y)
+    (are [x y] (= (link/link x) y)
       "http://www.google.com"
       {:type "plain-link" :url "http://www.google.com"}
 
@@ -19,7 +19,7 @@
       {:type "plain-link" :url "http://google.com"}))
 
   (testing "org links with labels"
-    (are [x y] (= (link/link? x) y)
+    (are [x y] (= (link/link x) y)
       "[[http://www.google.com][google]]"
       {:type "org-link" :url "http://www.google.com" :label "google"}
 
@@ -33,7 +33,7 @@
       {:type "org-link" :url "https://google.com" :label "google"}))
 
   (testing "org links without labels"
-    (are [x y] (= (link/link? x) y)
+    (are [x y] (= (link/link x) y)
       "[[http://www.google.com]]"
       {:type "org-link" :url "http://www.google.com" :label nil}
 
@@ -41,7 +41,7 @@
       {:type "org-link" :url "https://www.google.com" :label nil}))
 
   (testing "markdown links"
-    (are [x y] (= (link/link? x) y)
+    (are [x y] (= (link/link x) y)
       "[google](http://www.google.com)"
       {:type "markdown-link" :url "http://www.google.com" :label "google"}