From 8eaff8de4689f1fb5d136d365cd35a96282a07f2 Mon Sep 17 00:00:00 2001 From: Alex Snezhko Date: Sat, 24 Feb 2024 19:26:29 +0000 Subject: [PATCH] Changed wording from `percentEncode` to `encode` --- compiler/test/stdlib/uri.test.gr | 49 ++++++++++++-------------------- stdlib/uri.gr | 42 +++++++++++++-------------- stdlib/uri.md | 14 ++++----- 3 files changed, 46 insertions(+), 59 deletions(-) diff --git a/compiler/test/stdlib/uri.test.gr b/compiler/test/stdlib/uri.test.gr index 408454a2d..64c8abc8d 100644 --- a/compiler/test/stdlib/uri.test.gr +++ b/compiler/test/stdlib/uri.test.gr @@ -71,21 +71,16 @@ testValid( expectedFragment: Some("frag/?"), } ) -testValid( - "a12+3-4.5://1a-._~%1f%Fa!$&'()*+,;=:@0.99.100.255://?1%1f@:/?#/?a", - { - ...default, - expectedScheme: Some("a12+3-4.5"), - expectedUserinfo: Some( - "1a-._~%1f%Fa!$&'()*+,;=:" - ), // Do not turn %1f into %1F in userinfo - expectedHost: Some("0.99.100.255"), - expectedPath: "//", - expectedQuery: Some("1%1F@:/?"), - expectedFragment: Some("/?a"), - expectedString: "a12+3-4.5://1a-._~%1f%Fa!$&'()*+,;=:@0.99.100.255//?1%1F@:/?#/?a", - } -) +testValid("a12+3-4.5://1a-._~%1f%Fa!$&'()*+,;=:@0.99.100.255://?1%1f@:/?#/?a", { + ...default, + expectedScheme: Some("a12+3-4.5"), + expectedUserinfo: Some("1a-._~%1f%Fa!$&'()*+,;=:"), // Do not turn %1f into %1F in userinfo + expectedHost: Some("0.99.100.255"), + expectedPath: "//", + expectedQuery: Some("1%1F@:/?"), + expectedFragment: Some("/?a"), + expectedString: "a12+3-4.5://1a-._~%1f%Fa!$&'()*+,;=:@0.99.100.255//?1%1F@:/?#/?a", +}) testValid( "mailto:me@email.com", { ...default, expectedScheme: Some("mailto"), expectedPath: "me@email.com" } @@ -333,7 +328,7 @@ assert Result.map( ) ) == Ok( - "ht+1-tp://me%40pw@g+r%2Fa*in%3A80:80/%2520d:o'c%23s!?/a?b%23c=d:ef#Ur%23i-m/ake" + "ht+1-tp://me%40pw@g+r%2Fa*in%3A80:80/%2520d:o'c%23s!?/a?b%23c=d:ef#Ur%23i-m/ake", ) assert Result.map( Uri.toString, @@ -348,11 +343,7 @@ assert Result.map( // update let orig = Result.unwrap(Uri.make()) -assert Uri.update( - orig, - scheme=Some(Some("+")), - percentEncodeComponents=false -) == +assert Uri.update(orig, scheme=Some(Some("+")), percentEncodeComponents=false) == Err(Uri.InvalidSchemeError) assert Uri.update( orig, @@ -374,11 +365,7 @@ assert Uri.update(orig, path=Some("%2"), percentEncodeComponents=false) == Err(Uri.InvalidPathError) assert Uri.update(orig, query=Some(Some("#")), percentEncodeComponents=false) == Err(Uri.InvalidQueryError) -assert Uri.update( - orig, - fragment=Some(Some("%")), - percentEncodeComponents=false -) == +assert Uri.update(orig, fragment=Some(Some("%")), percentEncodeComponents=false) == Err(Uri.InvalidFragmentError) assert Uri.update(orig, port=Some(Some(80))) == Err(Uri.PortWithNoHost) @@ -415,7 +402,7 @@ assert Result.map( ) ) == Ok( - "ht+1-tp://me%40pw@g+r%2Fa*in%3A80:80/%2520d:o'c%23s!?/a?b%23c=d:ef#Ur%23i-m/ake" + "ht+1-tp://me%40pw@g+r%2Fa*in%3A80:80/%2520d:o'c%23s!?/a?b%23c=d:ef#Ur%23i-m/ake", ) assert Result.map( Uri.toString, @@ -426,14 +413,14 @@ assert Result.map( let orig = Result.unwrap(Uri.parse("ftp:path")) assert Uri.update(orig, host=Some(Some("domain"))) == Err(Uri.InvalidPathError) -// percentEncode/percentDecode +// encode/decode let encoded = "%F0%9F%8C%BE" let decoded = "🌾" -assert Uri.percentDecode(encoded) == Ok(decoded) -assert Uri.percentEncode(decoded) == encoded +assert Uri.decode(encoded) == Ok(decoded) +assert Uri.encode(decoded) == encoded -assert Uri.percentDecode("%2") == Err(Uri.InvalidPercentEncoding) +assert Uri.decode("%2") == Err(Uri.InvalidPercentEncoding) // encodeQuery/decodeQuery diff --git a/stdlib/uri.gr b/stdlib/uri.gr index f924674ff..5c8713bfb 100644 --- a/stdlib/uri.gr +++ b/stdlib/uri.gr @@ -265,13 +265,13 @@ let removeDotSegments = path => { * @param encodeSet: An indication for which characters to percent-encode. `EncodeNonUnreserved` by default * @returns A percent-encoding of the given string * - * @example Uri.percentEncode("h3ll0_.w ?o+rld", encodeSet=Uri.EncodeNonUnreserved) // "h3ll0_.w%20%3Fo%2Brld" - * @example Uri.percentEncode("d+om@i:n.com", encodeSet=Uri.EncodeRegisteredHost) // "d+om%40i%3An.com" - * @example Uri.percentEncode("word", encodeSet=Uri.EncodeCustom(c => c == 'o')) // "w%6Frd" + * @example Uri.encode("h3ll0_.w ?o+rld", encodeSet=Uri.EncodeNonUnreserved) // "h3ll0_.w%20%3Fo%2Brld" + * @example Uri.encode("d+om@i:n.com", encodeSet=Uri.EncodeRegisteredHost) // "d+om%40i%3An.com" + * @example Uri.encode("word", encodeSet=Uri.EncodeCustom(c => c == 'o')) // "w%6Frd" * * @since v0.6.0 */ -provide let percentEncode = (str, encodeSet=EncodeNonUnreserved) => { +provide let encode = (str, encodeSet=EncodeNonUnreserved) => { let shouldEncode = makePercentEncoder(encodeSet) let chars = String.explode(str) let rec getChars = (i, acc) => { @@ -307,7 +307,7 @@ provide let percentEncode = (str, encodeSet=EncodeNonUnreserved) => { * * @since v0.6.0 */ -provide let percentDecode = str => { +provide let decode = str => { if (!isValidPercentEncoding(str)) { Err(InvalidPercentEncoding) } else { @@ -325,9 +325,9 @@ provide let percentDecode = str => { */ provide let encodeQuery = (urlVals, encodeSet=EncodeNonUnreserved) => { let parts = List.map(((key, val)) => { - percentEncode(key, encodeSet=encodeSet) ++ + encode(key, encodeSet=encodeSet) ++ "=" ++ - percentEncode(val, encodeSet=encodeSet) + encode(val, encodeSet=encodeSet) }, urlVals) List.join("&", parts) @@ -834,17 +834,17 @@ provide let make = ( } let (userinfo, host, path, query, fragment) = if (percentEncodeComponents) { - let encode = (val, encodeSet) => - Option.map(val => percentEncode(val, encodeSet=encodeSet), val) + let encodeOption = (val, encodeSet) => + Option.map(val => encode(val, encodeSet=encodeSet), val) let isIpAddressHost = Result.isOk(parseFallible(parseIpAddress, host)) ( - encode(userinfo, EncodeUserinfo), - if (!isIpAddressHost) encode(host, EncodeRegisteredHost) else host, - percentEncode(path, encodeSet=EncodePath), - encode(query, EncodeQueryOrFragment), - encode(fragment, EncodeQueryOrFragment), + encodeOption(userinfo, EncodeUserinfo), + if (!isIpAddressHost) encodeOption(host, EncodeRegisteredHost) else host, + encode(path, encodeSet=EncodePath), + encodeOption(query, EncodeQueryOrFragment), + encodeOption(fragment, EncodeQueryOrFragment), ) } else { (userinfo, host, path, query, fragment) @@ -973,8 +973,8 @@ provide let update = ( } let (userinfo, host, path, query, fragment) = if (percentEncodeComponents) { - let encode = (val, encodeSet) => match (val) { - Some(Some(val)) => Some(Some(percentEncode(val, encodeSet=encodeSet))), + let encodeOption = (val, encodeSet) => match (val) { + Some(Some(val)) => Some(Some(encode(val, encodeSet=encodeSet))), val => val, } @@ -984,11 +984,11 @@ provide let update = ( } ( - encode(userinfo, EncodeUserinfo), - if (!isIpAddressHost) encode(host, EncodeRegisteredHost) else host, - Option.map(path => percentEncode(path, encodeSet=EncodePath), path), - encode(query, EncodeQueryOrFragment), - encode(fragment, EncodeQueryOrFragment), + encodeOption(userinfo, EncodeUserinfo), + if (!isIpAddressHost) encodeOption(host, EncodeRegisteredHost) else host, + Option.map(path => encode(path, encodeSet=EncodePath), path), + encodeOption(query, EncodeQueryOrFragment), + encodeOption(fragment, EncodeQueryOrFragment), ) } else { (userinfo, host, path, query, fragment) diff --git a/stdlib/uri.md b/stdlib/uri.md index 5da45df0a..cc7e815c6 100644 --- a/stdlib/uri.md +++ b/stdlib/uri.md @@ -101,7 +101,7 @@ Used to specify which characters to percent-encode from a string. Functions and constants included in the Uri module. -### Uri.**percentEncode** +### Uri.**encode**
Added in next @@ -109,7 +109,7 @@ No other changes yet.
```grain -percentEncode : (str: String, ?encodeSet: PercentEncodeSet) => String +encode : (str: String, ?encodeSet: PercentEncodeSet) => String ``` Percent-encodes characters in a string based on the specified `PercentEncodeSet`. @@ -130,18 +130,18 @@ Returns: Examples: ```grain -Uri.percentEncode("h3ll0_.w ?o+rld", encodeSet=Uri.EncodeNonUnreserved) // "h3ll0_.w%20%3Fo%2Brld" +Uri.encode("h3ll0_.w ?o+rld", encodeSet=Uri.EncodeNonUnreserved) // "h3ll0_.w%20%3Fo%2Brld" ``` ```grain -Uri.percentEncode("d+om@i:n.com", encodeSet=Uri.EncodeRegisteredHost) // "d+om%40i%3An.com" +Uri.encode("d+om@i:n.com", encodeSet=Uri.EncodeRegisteredHost) // "d+om%40i%3An.com" ``` ```grain -Uri.percentEncode("word", encodeSet=Uri.EncodeCustom(c => c == 'o')) // "w%6Frd" +Uri.encode("word", encodeSet=Uri.EncodeCustom(c => c == 'o')) // "w%6Frd" ``` -### Uri.**percentDecode** +### Uri.**decode**
Added in next @@ -149,7 +149,7 @@ No other changes yet.
```grain -percentDecode : (str: String) => Result +decode : (str: String) => Result ``` Decodes any percent-encoded characters in a string.