Skip to content

Commit

Permalink
Changed wording from percentEncode to encode
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-snezhko committed Feb 24, 2024
1 parent f10b70e commit 4cadfc8
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 49 deletions.
49 changes: 18 additions & 31 deletions compiler/test/stdlib/uri.test.gr
Expand Up @@ -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" }
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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)

Expand Down Expand Up @@ -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,
Expand All @@ -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

Expand Down
22 changes: 11 additions & 11 deletions stdlib/uri.gr
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand Down Expand Up @@ -835,14 +835,14 @@ provide let make = (

let (userinfo, host, path, query, fragment) = if (percentEncodeComponents) {
let encode = (val, encodeSet) =>
Option.map(val => percentEncode(val, encodeSet=encodeSet), val)
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(path, encodeSet=EncodePath),
encode(query, EncodeQueryOrFragment),
encode(fragment, EncodeQueryOrFragment),
)
Expand Down Expand Up @@ -974,7 +974,7 @@ 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))),
Some(Some(val)) => Some(Some(encode(val, encodeSet=encodeSet))),
val => val,
}

Expand All @@ -986,7 +986,7 @@ provide let update = (
(
encode(userinfo, EncodeUserinfo),
if (!isIpAddressHost) encode(host, EncodeRegisteredHost) else host,
Option.map(path => percentEncode(path, encodeSet=EncodePath), path),
Option.map(path => encode(path, encodeSet=EncodePath), path),
encode(query, EncodeQueryOrFragment),
encode(fragment, EncodeQueryOrFragment),
)
Expand Down
14 changes: 7 additions & 7 deletions stdlib/uri.md
Expand Up @@ -101,15 +101,15 @@ Used to specify which characters to percent-encode from a string.

Functions and constants included in the Uri module.

### Uri.**percentEncode**
### Uri.**encode**

<details disabled>
<summary tabindex="-1">Added in <code>next</code></summary>
No other changes yet.
</details>

```grain
percentEncode : (str: String, ?encodeSet: PercentEncodeSet) => String
encode : (str: String, ?encodeSet: PercentEncodeSet) => String
```

Percent-encodes characters in a string based on the specified `PercentEncodeSet`.
Expand All @@ -130,26 +130,26 @@ 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**

<details disabled>
<summary tabindex="-1">Added in <code>next</code></summary>
No other changes yet.
</details>

```grain
percentDecode : (str: String) => Result<String, PercentDecodingError>
decode : (str: String) => Result<String, PercentDecodingError>
```

Decodes any percent-encoded characters in a string.
Expand Down

0 comments on commit 4cadfc8

Please sign in to comment.