Skip to content

Commit

Permalink
Touch up query-string<->seq functions
Browse files Browse the repository at this point in the history
  • Loading branch information
plexus committed Jan 23, 2024
1 parent 39df3ea commit 96249d9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 26 deletions.
8 changes: 2 additions & 6 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@

## Added

- Added functions for dealing with query strings as positional collections: `query-string->seq`,
`seq->query-string`.

## Fixed

## Changed
- Added functions for dealing with query strings as positional collections:
`query-string->seq`, `seq->query-string`.

# 1.16.134 (2023-10-10 / c0f16d8)

Expand Down
32 changes: 16 additions & 16 deletions src/lambdaisland/uri.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -181,14 +181,13 @@
into)))))

(defn query-string->seq
"Parse a query string, consisting of key=value pairs, separated by \"&\".
Return vector of positional [key value] tuples. Keys and values are strings.
Returns empty vector if q is empty string or nil."
"Parse a query string, consisting of key=value pairs, separated by `\"&\"`.
Return a seq of `[key value]` pairs, in the exact order they occur in the
query string. Keys and values are strings. Returns `nil` if `q` is blank or
`nil`."
[q]
(if (str/blank? q)
[]
(->> (str/split q #"&")
(mapv decode-param-pair))))
(when-not (str/blank? q)
(map decode-param-pair (str/split q #"&"))))

(defn query-map
"Return the query section of a URI as a map. Will coerce its argument
Expand Down Expand Up @@ -244,15 +243,16 @@
(apply str))))

(defn seq->query-string
"Convert a positional collection of [key value] tuples into a query string, consisting of key=value pairs
separated by `&`. The result is percent-encoded, so it is always safe to use.
Keys can be strings or keywords. Tuple with `nil` value adds entry like `key=` to query string.
Values are stringified."
[coll]
(some->> (seq coll)
(map (fn [[k v]] (encode-param-pair k v)))
(interpose "&")
(apply str)))
"Convert a positional sequence of `[key value]` tuples into a query string,
consisting of key=value pairs separated by `&`. The result is percent-encoded,
so it is always safe to use. Keys can be strings or keywords. Tuples with
`nil` values adds entries like `key=` to query string. Values are
stringified."
[pairs]
(->> pairs
(map (fn [[k v]] (encode-param-pair k v)))
(interpose "&")
(apply str)))

(defn assoc-query*
"Add additional query parameters to a URI. Takes a URI (or coercible to URI) and
Expand Down
8 changes: 4 additions & 4 deletions test/lambdaisland/uri_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -183,16 +183,16 @@
(deftest query-string->seq-test
(are [query-string expected]
(= expected (uri/query-string->seq query-string))
nil []
"" []
nil nil
"" nil
"a=1&b=2&a=3" [["a" "1"] ["b" "2"] ["a" "3"]]
"a=1&b=" [["a" "1"] ["b" ""]]))

(deftest seq->query-string-test
(are [seq-of-tuples expected]
(= expected (uri/seq->query-string seq-of-tuples))
nil nil
[] nil
nil ""
[] ""
[["a" "1"] ["b" "2"] ["a" "3"]] "a=1&b=2&a=3"
[["a" "1"] ["b" ""]] "a=1&b="))

Expand Down

0 comments on commit 96249d9

Please sign in to comment.