-
Notifications
You must be signed in to change notification settings - Fork 1
/
url.clj
58 lines (46 loc) · 1.33 KB
/
url.clj
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
(ns tech.io.url
(:require [clojure.string :as s])
(:import [java.net URL]
[java.io File]))
(defn url->parts
[url]
(let [url (str url)
[url args] (let [first-octothorpe (.indexOf url "#")]
(if (>= first-octothorpe 0)
[(.substring url 0 first-octothorpe)
(.substring url (+ first-octothorpe 1))]
[url nil]))
parts (s/split url #"/")
^String protocol-part (first parts)
path (drop 2 parts)]
{:protocol (keyword (.substring protocol-part 0 (- (count protocol-part) 1)))
:path path
:arguments args}))
(defn- join-forward-slash
^String [path]
(s/join "/" path))
(defn parts->url
"Returns a string. Not a java url."
^String [{:keys [protocol path arguments]}]
(if arguments
(str (name protocol) "://"
(join-forward-slash path)
"#"
arguments)
(str (name protocol) "://"
(join-forward-slash path))))
(defn url?
[url]
(if (string? url)
(try
(:protocol (url->parts url))
(catch Throwable e
false))
false))
(defn string-seq->file-path
[str-seq]
(s/join File/separator str-seq))
(defn parts->file-path
[{:keys [protocol path arguments]}]
;;Windows will need something else here.
(string-seq->file-path path))