Skip to content

Commit

Permalink
Add pre & post conditions for all functions
Browse files Browse the repository at this point in the history
  • Loading branch information
chbrown committed Jun 5, 2018
1 parent b34ef2c commit 9e24b3f
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/jurl/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@
"URL-encode `string`; other implementations sometimes convert '+' to '%20',
but this method does not. Uses UTF-8 as the encoding if applicable."
[string]
{:pre [(or (nil? string) (string? string))]
:post [(string? %)]}
(-> (str string)
#?(:clj (java.net.URLEncoder/encode "UTF-8")
:cljs (js/encodeURIComponent))))

(defn ^:export decode
"URL-decode `string`. Uses UTF-8 as the encoding if applicable."
[string]
{:pre [(or (nil? string) (string? string))]
:post [(string? %)]}
(-> (str string)
#?(:clj (java.net.URLDecoder/decode "UTF-8")
:cljs (js/decodeURIComponent))))
Expand All @@ -34,6 +38,9 @@
; TODO: define how to handle non-tuple parameters
; TODO: consider using URLSearchParams when more browsers support it.
[search]
{:pre [(string? search)]
:post [(every? string? (map first %))
(every? string? (map second %))]}
(when-not (empty? search)
(->> (str/split (subs search 1) #"&")
; (str/split "" #"&") returns [""], but we would prefer []
Expand All @@ -43,6 +50,8 @@
(defn- update-conj
"Add v to the (maybe empty) vector at (get m k)"
[m [k v]]
{:pre [(map? m)]
:post [(contains? % k)]}
; we don't use update since that doesn't allow a default for missing values
; if we didn't mind the ordering we could just use (update m k conj v)
; but this way the order of the vector matches the order of the values
Expand All @@ -53,6 +62,8 @@
([search]
(search->map search nil))
([search {re :split}]
{:pre [(string? search)]
:post [(map? %)]}
(reduce update-conj {} (cond->> (search->seq search)
; when {:split re} is supplied, produce a [k v] pair for each split substring of v
re (mapcat (fn [[k v]] (map vector (repeat k) (str/split v re))))))))
Expand All @@ -62,6 +73,10 @@
(defn ^:export seq->search
"Serialize a seq of key-value tuples back into a string"
[kvs]
{:pre [(or (nil? kvs) (coll? kvs))
(every? string? (map first kvs))
(every? string? (map second kvs))]
:post [(or (nil? %) (string? %))]}
(some->> kvs
(map #(str/join "=" %))
(str/join "&")
Expand All @@ -70,6 +85,8 @@
(defn- map-values
"Construct a new map with all the values of the given map passed through f"
[f kvs]
{:pre [(ifn? f) (map? kvs)]
:post [(map? %)]}
(into (empty kvs) (for [[k v] kvs] [k (f v)])))

(defn- ungroup
Expand All @@ -79,6 +96,12 @@
([m]
(ungroup nil m))
([separator m]
{:pre [(or (nil? separator) (string? separator))
(map? m)
(every? string? (keys m))
(every? string? (apply concat (vals m)))]
:post [(every? string? (map first %))
(every? string? (map second %))]}
(if separator
(for [[k vs] m]
[k (str/join separator vs)])
Expand All @@ -92,4 +115,9 @@
([m]
(map->search m nil))
([m {:keys [separator]}]
{:pre [(or (nil? m) (map? m))
(every? string? (keys m))
(every? string? (apply concat (vals m)))
(or (nil? separator) (string? separator))]
:post [(or (nil? %) (string? %))]}
(some->> m (map-values #(map encode %)) (ungroup separator) seq->search)))

0 comments on commit 9e24b3f

Please sign in to comment.