Current section
Files
Jump to
Current section
Files
src/header.erl
-module(header).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([parse_accept_language_header/1, get_first_lang_from_accept_language_header/1]).
-spec parse_accept_language_header(binary()) -> list({binary(), float()}).
parse_accept_language_header(Raw_header) ->
Quality_factor = <<";q="/utf8>>,
_pipe = Raw_header,
_pipe@1 = gleam@string:replace(_pipe, <<" "/utf8>>, <<""/utf8>>),
_pipe@2 = gleam@string:split(_pipe@1, <<","/utf8>>),
_pipe@3 = gleam@list:filter(
_pipe@2,
fun(Lang) -> not gleam@string:is_empty(Lang) end
),
_pipe@4 = gleam@list:filter_map(
_pipe@3,
fun(Lang@1) ->
case gleam_stdlib:contains_string(Lang@1, Quality_factor) of
true ->
gleam@string:split_once(Lang@1, Quality_factor);
false ->
{ok, {Lang@1, <<"1.0"/utf8>>}}
end
end
),
_pipe@5 = gleam@list:filter_map(
_pipe@4,
fun(Lang_with_priority) ->
gleam@result:'try'(
gleam@float:parse(erlang:element(2, Lang_with_priority)),
fun(Priority) ->
{ok, {erlang:element(1, Lang_with_priority), Priority}}
end
)
end
),
_pipe@6 = gleam@list:sort(
_pipe@5,
fun(A, B) ->
gleam@float:compare(erlang:element(2, A), erlang:element(2, B))
end
),
lists:reverse(_pipe@6).
-spec get_first_lang_from_accept_language_header(binary()) -> gleam@option:option(binary()).
get_first_lang_from_accept_language_header(Raw_header) ->
Languages_with_priority = parse_accept_language_header(Raw_header),
_pipe = Languages_with_priority,
_pipe@1 = gleam@list:first(_pipe),
_pipe@2 = gleam@option:from_result(_pipe@1),
gleam@option:map(
_pipe@2,
fun(Lang_with_priority) -> erlang:element(1, Lang_with_priority) end
).