Packages

Defangle is a Gleam library for defanging URLs, Emails, and IP addresses making them safe to share.

Current section

Files

Jump to
defangle src defangle.erl
Raw

src/defangle.erl

-module(defangle).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([defang_url/1, defang_ip/1, defang_email/2, defang/1]).
-spec defang_url(binary()) -> binary().
defang_url(Domain) ->
case gleam_stdlib:contains_string(Domain, <<"[.]"/utf8>>) of
true ->
Domain;
false ->
_pipe = Domain,
_pipe@1 = gleam@string:replace(_pipe, <<"."/utf8>>, <<"[.]"/utf8>>),
_pipe@2 = gleam@string:replace(
_pipe@1,
<<"http"/utf8>>,
<<"hxxp"/utf8>>
),
gleam@string:replace(_pipe@2, <<"://"/utf8>>, <<"[://]"/utf8>>)
end.
-spec defang_ip(binary()) -> binary().
defang_ip(Ip) ->
case gleam_stdlib:contains_string(Ip, <<"[.]"/utf8>>) of
true ->
Ip;
false ->
_pipe = Ip,
gleam@string:replace(_pipe, <<"."/utf8>>, <<"[.]"/utf8>>)
end.
-spec defang_email(binary(), binary()) -> binary().
defang_email(User, Domain) ->
_pipe = User,
_pipe@1 = gleam@string:append(_pipe, <<"[at]"/utf8>>),
gleam@string:append(
_pipe@1,
begin
_pipe@2 = Domain,
defang_url(_pipe@2)
end
).
-spec defang(binary()) -> binary().
defang(Url) ->
case gleam@string:split(Url, <<"@"/utf8>>) of
[User, Domain] ->
defang_email(User, Domain);
_ ->
defang_url(Url)
end.