-
Notifications
You must be signed in to change notification settings - Fork 4
/
uri.cljc
69 lines (63 loc) · 1.72 KB
/
uri.cljc
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
61
62
63
64
65
66
67
68
69
(ns proton.uri
"URI parser function"
(:require [clojure.string :as string]
#?(:cljs [goog.Uri]))
#?(:cljs (:require-macros [cljs.core :refer [exists?]]))
#?(:clj (:import [java.net URI URLEncoder URLDecoder])))
(defn uri-encode
[s]
(some-> s
str
#?(:clj (URLEncoder/encode "UTF-8")
:cljs js/encodeURIComponent)))
(defn uri-decode
[s]
(some-> s
str
#?(:clj (URLDecoder/decode "UTF-8")
:cljs js/decodeURIComponent)))
(defn- split-param [param]
(as-> param $
(string/split $ #"=")
(concat $ (repeat ""))
(take 2 $)))
(defn- parse-query
[^URI uri]
(let [qstr (.getQuery uri)
qstr (if (empty? qstr)
nil
qstr)]
(when qstr
(some->> (string/split qstr #"&")
seq
(map split-param)
(mapcat (fn [[k v]] [(keyword k) (uri-decode v)]))
(apply hash-map)))))
(defn- parse-fragment
[^URI uri]
(let [fragment (.getFragment uri)]
(if (empty? fragment)
nil
fragment)))
(defn uri
([]
#?(:clj nil
:cljs (if (exists? js/window)
(-> js/window
.-location
.-href
uri))))
([uri-string]
;; TODO validate uri-string with regex
(let [o #?(:clj (URI. uri-string)
:cljs (goog.Uri. uri-string))]
{:scheme (.getScheme o)
:host #?(:clj (.getHost o)
:cljs (.getDomain o))
:port (if (pos? (.getPort o))
(.getPort o))
:path (.getPath o)
:query (parse-query o)
:fragment (parse-fragment o)
:user-info (if-not (empty? (.getUserInfo o))
(.getUserInfo o))})))