A pure Clojure/ClojureScript URI library.
Key features
.cljc
There are a number of Clojure libraries for working with URI/URLs (see
Similar projects below). They all rely to some degree on
java.net.URI
or java.net.URL
. This lib provides a pure-Clojure/ClojureScript
alternative.
See the announcement blog post
To install, add the following dependency to your project or build file:
[lambdaisland/uri "1.2.0"]
(require '[lambdaisland.uri :refer [uri join]])
;; uri :: String -> lambdaisland.uri.URI
(uri "//example.com/foo/bar")
;;=> #lambdaisland/uri "//example.com/foo/bar"
;; A URI is a record, use assoc to update specific parts
;; Use `str` if you want the URI back as a string
(str
(assoc (uri "//example.com/foo/bar")
:scheme "https"
:user "arne"
:password "supersecret"
:host "lambdaisland.com"
:port "3333"
:path "/hello/world"
:query "q=5"
:fragment "section1"))
;;=> "https://arne:supersecret@lambdaisland.com:3333/hello/world?q=5#section1"
;; RFC compliant joining of relative URIs
(join "//example.com/foo/bar" "./~arne/site/" "../foo.png")
;;=> #lambdaisland/uri "//example.com/foo/~arne/foo.png"
;; Arguments to `join` are coerced, you can pass strings, java.net.URI, or any x
;; for which `(str x)` returns a URI string.
(join (java.net.URI. "http://example.com/foo/bar") (uri "./~arne/site/") "../foo.png")
;;=> #lambdaisland/uri "http://example.com/foo/~arne/foo.png"
;; URI implements IFn for keyword based lookup, so it's fully
;; interface-compatible with Clojure maps.
(:path (uri "http://example.com/foo/bar"))
;; Instances of URI are printed with a #lambdaisland/uri reader tag. To read
;; them back from EDN, use the provided readers.
(require '[clojure.edn :as edn])
(edn/read-string
{:readers lambdaisland.uri/edn-readers}
"#lambdaisland/uri \"http://example.com/foo/~arne/foo.png\"")
java.net.URI
.java.net.URI
.java.net.URI
.RFC3986 Uniform Resource Identifier (URI): Generic Syntax
This library implements the algorithm specified in Section 5.2 of that RFC.
It has been tested against this list of test cases compiled by the W3C.
Copyright © 2017 Arne Brasseur
Distributed under the Mozilla Public License 2.0.
Can you improve this documentation? These fine people already did:
Arne Brasseur & Martin KlepschEdit on GitHub
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close