url_join
Join URL path segments and normalize the result, similar to npm's url-join.
Installation
gleam add url_joinUsage
import url_join
pub fn main() -> Nil {
// Join segments and normalize slashes, protocol, query, and hash
let url =
url_join.join([
"http://www.google.com",
"a",
"/b/cd",
"?foo=123",
])
// -> "http://www.google.com/a/b/cd?foo=123"
let api =
url_join.join([
"https://example.com/",
"/api/",
"users",
])
// -> "https://example.com/api/users"
}Behaviour
- Filters out empty segments
-
Merges a leading bare protocol (e.g.
"http:") with the next segment -
Merges a leading
"/"with the next segment -
Normalises protocols to
protocol://(andfile:///for file) - Trims and collapses slashes between segments
-
Does not add a slash before
?,#, or& -
Normalises query string (multiple
?/&become a single?then&)
Further documentation can be found at https://hexdocs.pm/url_join.
Development
gleam run # Run the project
gleam test # Run the tests