aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Saxton <peterhsaxton@gmail.com>2020-06-23 11:17:30 +0100
committerLouis Pilfold <louis@lpil.uk>2020-06-24 22:09:19 +0100
commit6b68e5e25006bc734345ce77ede14b6e54214d23 (patch)
tree0436b2f28d3e18797e79aeda5714c171ae74905c
parentaba2570a6ead62c7fa62c29f70689e73f901ac9d (diff)
downloadgleam_stdlib-6b68e5e25006bc734345ce77ede14b6e54214d23.tar.gz
gleam_stdlib-6b68e5e25006bc734345ce77ede14b6e54214d23.zip
add changelog and docs
-rw-r--r--CHANGELOG.md2
-rw-r--r--src/gleam/base.gleam4
2 files changed, 6 insertions, 0 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 25b47f5..518a173 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -21,6 +21,8 @@
- Created the `regex` module with the `from_string`, `from_string_with`, and
`match` functions.
- The `list` module gains the the `pop`, `pop_map` and `key_pop` functions.
+- `base` module created with `encode64`, `decode64`, `url_encode64` and
+ `url_decode64`.
## 0.9.0 - 2020-05-26
diff --git a/src/gleam/base.gleam b/src/gleam/base.gleam
index 0b88734..b3a4651 100644
--- a/src/gleam/base.gleam
+++ b/src/gleam/base.gleam
@@ -7,6 +7,7 @@ external fn erl_encode64(BitString) -> String =
external fn erl_decode64(String) -> Result(BitString, Nil) =
"gleam_stdlib" "base_decoded4"
+/// Encodes a BitString into a base 64 encoded string.
pub fn encode64(input: BitString, padding: Bool) -> String {
let encoded = erl_encode64(input)
case padding {
@@ -15,6 +16,7 @@ pub fn encode64(input: BitString, padding: Bool) -> String {
}
}
+/// Decodes a base 64 encoded string into a BitString.
pub fn decode64(encoded: String) -> Result(BitString, Nil) {
let padded = case bit_string.byte_size(bit_string.from_string(encoded)) % 4 {
0 -> encoded
@@ -23,12 +25,14 @@ pub fn decode64(encoded: String) -> Result(BitString, Nil) {
erl_decode64(padded)
}
+/// Encodes a BitString into a base 64 encoded string with URL and filename safe alphabet.
pub fn url_encode64(input: BitString, padding: Bool) -> String {
encode64(input, padding)
|> string.replace("+", "-")
|> string.replace("/", "_")
}
+/// Decodes a base 64 encoded string with URL and filename safe alphabet into a BitString.
pub fn url_decode64(encoded: String) -> Result(BitString, Nil) {
encoded
|> string.replace("-", "+")