-
-
Notifications
You must be signed in to change notification settings - Fork 137
/
base.gleam
59 lines (51 loc) 路 1.48 KB
/
base.gleam
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import gleam/bit_string
import gleam/string
/// Encodes a BitString into a base 64 encoded string.
///
pub fn encode64(input: BitString, padding: Bool) -> String {
let encoded = do_encode64(input)
case padding {
True -> encoded
False -> string.replace(encoded, "=", "")
}
}
if erlang {
external fn do_encode64(BitString) -> String =
"base64" "encode"
}
if javascript {
external fn do_encode64(BitString) -> String =
"../gleam_stdlib.mjs" "encode64"
}
/// 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
n -> string.append(encoded, string.repeat("=", 4 - n))
}
do_decode64(padded)
}
if erlang {
external fn do_decode64(String) -> Result(BitString, Nil) =
"gleam_stdlib" "base_decode64"
}
if javascript {
external fn do_decode64(String) -> Result(BitString, Nil) =
"../gleam_stdlib.mjs" "decode64"
}
/// 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("-", "+")
|> string.replace("_", "/")
|> decode64()
}