diff options
-rw-r--r-- | src/gleam/uri.gleam | 69 | ||||
-rw-r--r-- | test/gleam/uri_test.gleam | 6 |
2 files changed, 69 insertions, 6 deletions
diff --git a/src/gleam/uri.gleam b/src/gleam/uri.gleam index 10c4a7b..4e15a2b 100644 --- a/src/gleam/uri.gleam +++ b/src/gleam/uri.gleam @@ -49,6 +49,14 @@ type UriKey { /// /// The opposite operation is `uri.to_string` /// +/// ## Examples +/// +/// ``` +/// > parse("https://example.com:1234/a/b?query=true#fragment") +/// +/// Ok(Uri(scheme: Some("https"), ...)) +/// ``` +/// pub fn parse(string: String) -> Result(Uri, Nil) { try uri_map = dynamic.map(erl_parse(string)) @@ -81,6 +89,14 @@ external fn erl_parse_query(String) -> Dynamic = /// /// The opposite operation is `uri.query_to_string`. /// +/// ## Examples +/// +/// ``` +/// > parse_query("a=1&b=2") +/// +/// Ok([tuple("a", "1"), tuple("b", "2")]) +/// ``` +/// pub fn parse_query(query: String) -> Result(List(tuple(String, String)), Nil) { let bool_value = fn(x) { result.map(dynamic.bool(x), fn(_) { "" }) } let query_param = dynamic.typed_tuple2( @@ -113,6 +129,14 @@ external fn erl_query_to_string( /// /// The opposite operation is `uri.parse_query`. /// +/// ## Examples +/// +/// ``` +/// > query_to_string([tuple("a", "1"), tuple("b", "2")]) +/// +/// "a=1&b=2" +/// ``` +/// pub fn query_to_string(query: List(tuple(String, String))) -> String { query |> erl_query_to_string([Encoding(Utf8)]) @@ -123,10 +147,13 @@ pub fn query_to_string(query: List(tuple(String, String))) -> String { /// Encodes a string into a percent encoded representation. /// Note that this encodes space as +. /// -/// ## Example +/// ## Examples /// -/// percent_encode("100% great") -/// > "100%25+great" +/// ``` +/// > percent_encode("100% great") +/// +/// "100%25+great" +/// ``` /// pub fn percent_encode(value: String) -> String { query_to_string([tuple("k", value)]) @@ -135,10 +162,13 @@ pub fn percent_encode(value: String) -> String { /// Decodes a percent encoded string. /// -/// ## Example +/// ## Examples +/// +/// ``` +/// > percent_decode("100%25+great") /// -/// percent_decode("100%25+great") -/// > Ok("100% great") +/// Ok("100% great") +/// ``` /// pub fn percent_decode(value: String) -> Result(String, Nil) { string.concat(["k=", value]) @@ -175,6 +205,14 @@ fn remove_dot_segments(input: List(String)) -> List(String) { /// Removes empty segments and resolves dot-segments as specified in /// [section 5.2](https://www.ietf.org/rfc/rfc3986.html#section-5.2) of the RFC. /// +/// ## Examples +/// +/// ``` +/// > path_segments("/users/1") +/// +/// ["users" ,"1"] +/// ``` +/// pub fn path_segments(path: String) -> List(String) { remove_dot_segments(string.split(path, "/")) } @@ -186,6 +224,15 @@ external fn erl_to_string(Map(UriKey, Dynamic)) -> Dynamic = /// /// The opposite operation is `uri.parse`. /// +/// ## Examples +/// +/// ``` +/// > let uri = Uri(Some("http"), None, Some("example.com"), ...) +/// > to_string(uri) +/// +/// "https://example.com" +/// ``` +/// pub fn to_string(uri: Uri) -> String { let field = fn(key: UriKey, value: Option(anything)) -> Result( tuple(UriKey, Dynamic), @@ -220,6 +267,16 @@ pub fn to_string(uri: Uri) -> String { /// /// The supported uri schemes are `http` and `https` /// Urls without a scheme will return Error +/// +/// ## Examples +/// +/// ``` +/// > assert Ok(uri) = parse("http://example.com/path?foo#bar") +/// > origin(uri) +/// +/// Ok("http://example.com") +/// ``` +/// pub fn origin(uri: Uri) -> Result(String, Nil) { let Uri(scheme: scheme, host: host, port: port, ..) = uri case scheme { diff --git a/test/gleam/uri_test.gleam b/test/gleam/uri_test.gleam index 3d6a187..f0c4eb5 100644 --- a/test/gleam/uri_test.gleam +++ b/test/gleam/uri_test.gleam @@ -67,6 +67,12 @@ pub fn path_only_uri_to_string_test() { pub fn parse_query_string_test() { assert Ok(parsed) = uri.parse_query("foo+bar=1&city=%C3%B6rebro") should.equal(parsed, [tuple("foo bar", "1"), tuple("city", "örebro")]) + + // Duplicates keys not overridden + assert Ok(parsed) = uri.parse_query("a[]=1&a[]=2") + + parsed + |> should.equal([tuple("a[]", "1"), tuple("a[]", "2")]) } pub fn parse_empty_query_string_test() { |