diff options
author | Sebastian Porto <s@porto5.com> | 2021-03-13 13:20:04 +1100 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2021-03-13 16:50:19 +0100 |
commit | 3d4b9b6c94b5a41278c72ca0a9f4be6c96f02434 (patch) | |
tree | f8e5382a399ffb5403ce33fffcec0057e2f57c52 /src | |
parent | 549c48866ff72c3c99c9fa8ea2f3b5a2e3ba605a (diff) | |
download | gleam_stdlib-3d4b9b6c94b5a41278c72ca0a9f4be6c96f02434.tar.gz gleam_stdlib-3d4b9b6c94b5a41278c72ca0a9f4be6c96f02434.zip |
Add Examples to uri
Diffstat (limited to 'src')
-rw-r--r-- | src/gleam/uri.gleam | 69 |
1 files changed, 63 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 { |