Skip to content

Commit fa2a49f

Browse files
authored
Improve docs for URI.encode/2 (#13908)
Co-authored-by: c4710n <[email protected]>
1 parent 9860f96 commit fa2a49f

File tree

1 file changed

+21
-15
lines changed

1 file changed

+21
-15
lines changed

lib/elixir/lib/uri.ex

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -367,21 +367,27 @@ defmodule URI do
367367
@doc """
368368
Percent-encodes all characters that require escaping in `string`.
369369
370-
By default, this function is meant to escape the whole URI, and
371-
therefore it will only escape characters which are foreign in
372-
all parts of a URI. Reserved characters (such as `:` and `/`)
373-
or unreserved (such as letters and numbers) are not escaped.
374-
375-
Because different components of a URI require different escaping
376-
rules, this function also accepts a `predicate` function as an optional
377-
argument. If passed, this function will be called with each byte
378-
in `string` as its argument and should return a truthy value (anything other
379-
than `false` or `nil`) if the given byte should be left as is, or
380-
return a falsy value (`false` or `nil`) if the character should be
381-
escaped. Defaults to `URI.char_unescaped?/1`.
382-
383-
See `encode_www_form/1` if you are interested in escaping reserved
384-
characters too.
370+
The optional `predicate` argument specifies a function used to detect whether
371+
a byte in the `string` should be escaped:
372+
373+
* if the function returns a truthy value, the byte should be kept as-is.
374+
* if the function returns a falsy value, the byte should be escaped.
375+
376+
The `predicate` argument can use some built-in functions:
377+
378+
* `URI.char_unescaped?/1` (default) - reserved characters (such as `:`
379+
and `/`) or unreserved (such as letters and numbers) are kept as-is.
380+
It's typically used to encode the whole URI.
381+
* `URI.char_unreserved?/1` - unreserved characters (such as letters and
382+
numbers) are kept as-is. It's typically used to encode components in
383+
a URI, such as query or fragment.
384+
* `URI.char_reserved?/1` - Reserved characters (such as `:` and `/`) are
385+
kept as-is.
386+
387+
And, you can also use custom functions.
388+
389+
See `encode_www_form/1` if you are interested in encoding `string` as
390+
"x-www-form-urlencoded".
385391
386392
## Examples
387393

0 commit comments

Comments
 (0)