This repository was archived by the owner on Oct 21, 2022. It is now read-only.
forked from ibdknox/crate
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutil.cljs
More file actions
60 lines (53 loc) · 1.46 KB
/
util.cljs
File metadata and controls
60 lines (53 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
(ns singultus.util
(:require [clojure.string :as str]))
(def ^:dynamic *base-url* nil)
(defn as-str
([] "")
([x]
; TODO: Maybe use something like (satisfies? INamed x) instead?
(if (or (symbol? x) (keyword? x))
(name x)
(str x)))
([x & xs]
((fn [s more]
(if more
(recur (str s (as-str (first more))) (next more))
s))
(as-str x) xs)))
(defn escape-html
"Change special characters into HTML character entities."
[text]
(-> (as-str text)
(str/replace "&" "&")
(str/replace "<" "<")
(str/replace ">" ">")
(str/replace "\"" """)))
(defn to-uri
"Prepends the base-url to the supplied URI."
[uri]
(if (re-matches #"^\w+:.*" uri)
uri
(str *base-url* uri)))
(defn url-encode-component [s]
"urlencode"
(js/encodeURIComponent (as-str s)))
(defn url-encode
"Turn a map of parameters into a urlencoded string."
[params]
(str/join "&"
(for [[k v] params]
(str (url-encode-component k) "=" (url-encode-component v)))))
(defn url
"Creates a URL string from a variable list of arguments and an optional
parameter map as the last argument. For example:
(url \"/group/\" 4 \"/products\" {:page 9})
=> \"/group/4/products?page=9\""
[& args]
(let [params (last args)
args (butlast args)]
(str
(to-uri
(str (apply str args)
(if (map? params)
(str "?" (url-encode params))
params))))))