Join URL path segments and normalize the result, similar to npm's url-join.
gleam add url_joinimport 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"
}- 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.
gleam run # Run the project
gleam test # Run the tests