diff --git a/CHANGELOG.md b/CHANGELOG.md index 5c1b6945..008d5043 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ - Fixed a bug that would result in `list.unique` having quadratic runtime. - Fixed the implementation of `list.key_set` to be tail recursive. - The `pop` and `pop_map` functions in the `list` module have been deprecated. +- The `uri` module gains the `empty` value, representing an empty URI which + equivalent to `""`. ## v0.53.0 - 2025-01-23 diff --git a/src/gleam/uri.gleam b/src/gleam/uri.gleam index b64a1c09..5b6565cc 100644 --- a/src/gleam/uri.gleam +++ b/src/gleam/uri.gleam @@ -28,6 +28,33 @@ pub type Uri { ) } +/// Constant representing an empty URI, equivalent to "". +/// +/// ## Examples +/// +/// ```gleam +/// let uri = Uri(..empty, scheme: Some("https"), host: Some("example.com")) +/// // -> Uri( +/// // scheme: Some("https"), +/// // userinfo: None, +/// // host: Some("example.com"), +/// // port: None, +/// // path: "", +/// // query: None, +/// // fragment: None, +/// // ) +/// ``` +/// +pub const empty = Uri( + scheme: None, + userinfo: None, + host: None, + port: None, + path: "", + query: None, + fragment: None, +) + /// Parses a compliant URI string into the `Uri` Type. /// If the string is not a valid URI string then an error is returned. /// @@ -58,18 +85,7 @@ pub fn parse(uri_string: String) -> Result(Uri, Nil) { // TODO: This is not perfect and will be more permissive than its Erlang // counterpart, ideally we want to replicate Erlang's implementation on the js // target as well. - let default_pieces = - Uri( - scheme: None, - userinfo: None, - host: None, - port: None, - path: "", - query: None, - fragment: None, - ) - - parse_scheme_loop(uri_string, uri_string, default_pieces, 0) + parse_scheme_loop(uri_string, uri_string, empty, 0) } fn parse_scheme_loop( @@ -620,9 +636,9 @@ fn remove_dot_segments_loop( /// ## Examples /// /// ```gleam -/// let uri = Uri(Some("http"), None, Some("example.com"), ...) +/// let uri = Uri(..empty, scheme: Some("https"), host: Some("example.com")) /// to_string(uri) -/// // -> "http://example.com" +/// // -> "https://example.com" /// ``` /// pub fn to_string(uri: Uri) -> String { @@ -664,9 +680,9 @@ pub fn to_string(uri: Uri) -> String { /// ## Examples /// /// ```gleam -/// let assert Ok(uri) = parse("http://example.com/path?foo#bar") +/// let assert Ok(uri) = parse("https://example.com/path?foo#bar") /// origin(uri) -/// // -> Ok("http://example.com") +/// // -> Ok("https://example.com") /// ``` /// pub fn origin(uri: Uri) -> Result(String, Nil) {