Packages

Join URL path segments and normalize the result.

Current section

Files

Jump to
url_join src url_join@parts.erl
Raw

src/url_join@parts.erl

-module(url_join@parts).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/url_join/parts.gleam").
-export([process/1, join/1]).
-file("src/url_join/parts.gleam", 5).
-spec process(list(binary())) -> list(binary()).
process(Parts) ->
_pipe = gleam@list:index_map(
Parts,
fun(Part, I) ->
Len = erlang:length(Parts),
No_leading = case I > 0 of
true ->
url_join@slashes:trim_leading(Part);
false ->
Part
end,
case I < (Len - 1) of
true ->
url_join@slashes:trim_trailing(No_leading);
false ->
url_join@slashes:collapse_trailing(No_leading)
end
end
),
gleam@list:filter(_pipe, fun(P) -> not gleam@string:is_empty(P) end).
-file("src/url_join/parts.gleam", 31).
-spec join_two(binary(), binary()) -> binary().
join_two(Prev, Part) ->
case {gleam_stdlib:string_ends_with(Prev, <<"?"/utf8>>),
gleam_stdlib:string_ends_with(Prev, <<"#"/utf8>>)} of
{true, _} ->
<<Prev/binary, Part/binary>>;
{_, true} ->
<<Prev/binary, Part/binary>>;
{_, _} ->
<<<<Prev/binary, "/"/utf8>>/binary, Part/binary>>
end.
-file("src/url_join/parts.gleam", 20).
-spec join(list(binary())) -> binary().
join(Parts) ->
case Parts of
[] ->
<<""/utf8>>;
[One] ->
One;
[First, Second | Rest] ->
Acc = join_two(First, Second),
gleam@list:fold(
Rest,
Acc,
fun(Acc@1, Part) -> join_two(Acc@1, Part) end
)
end.