-
Notifications
You must be signed in to change notification settings - Fork 0
/
defangle.gleam
50 lines (45 loc) 路 1.29 KB
/
defangle.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
//// Defangle contains functions for defanging IP addresses, URLs, and email addresses.
//// Defanging is the process of replacing certain characters in links to make
//// them safe to share in contexts where they might otherwise be clicked.
import gleam/string
/// Replace the `.` in the URL with `[.]`,
/// and the `http` with `hxxp`,
/// and the `://` with `[://]`
pub fn defang(url: String) -> String {
case string.split(url, "@") {
[user, domain] -> defang_email(user, domain)
_ -> defang_url(url)
}
}
/// Replace the `.` in the URL with `[.]`,
/// and the `http` with `hxxp`,
/// and the `://` with `[://]`
pub fn defang_url(domain: String) -> String {
case string.contains(domain, "[.]") {
True -> domain
False ->
domain
|> string.replace(".", "[.]")
|> string.replace("http", "hxxp")
|> string.replace("://", "[://]")
}
}
/// Replace the `.` in the IP address with `[.]`
pub fn defang_ip(ip: String) -> String {
case string.contains(ip, "[.]") {
True -> ip
False ->
ip
|> string.replace(".", "[.]")
}
}
/// Replace the `@` in the email with `[at]`,
/// and the `.` with `[.]`
pub fn defang_email(user: String, domain: String) -> String {
user
|> string.append("[at]")
|> string.append(
domain
|> defang_url,
)
}