Skip to content

Commit

Permalink
Sort keys in format_bindings/1 (#499)
Browse files Browse the repository at this point in the history
  • Loading branch information
voughtdq authored Feb 15, 2024
1 parent f653c25 commit 5e56e7a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
4 changes: 2 additions & 2 deletions test/elixir_boilerplate_web/errors_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ defmodule ElixirBoilerplateWeb.ErrorsTest do
|> changeset_to_error_messages()

assert html =~ "<li>email has invalid format[validation=:format]</li>"
assert html =~ "<li>email should be %{count} character(s)[count=10,type=:string,kind=:is,validation=:length]</li>"
assert html =~ "<li>email should be %{count} character(s)[count=10,kind=:is,type=:string,validation=:length]</li>"
assert html =~ "<li>multiple_roles.type can&#39;t be blank[validation=:required]</li>"
assert html =~ "<li>nicknames should have at least %{count} item(s)[count=1,type=:list,kind=:min,validation=:length]</li>"
assert html =~ "<li>nicknames should have at least %{count} item(s)[count=1,kind=:min,type=:list,validation=:length]</li>"
assert html =~ "<li>single_role.type is invalid[enum=admin,moderator,member,validation=:inclusion]</li>"
end

Expand Down
8 changes: 7 additions & 1 deletion test/support/gettext_interpolation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ defmodule ElixirBoilerplate.GettextInterpolation do
{:ok, "test[arg=:atom]"}
iex> runtime_interpolate("test", %{arg: [:atom,:atom2]})
{:ok, "test[arg=:atom,:atom2]"}
iex> runtime_interpolate("test", %{b: 1, a: [:a, :b]})
{:ok, "test[a=:a,:b,b=1]"}
"""
@impl true
def runtime_interpolate(message, bindings), do: {:ok, format(message, bindings)}
Expand All @@ -35,7 +37,11 @@ defmodule ElixirBoilerplate.GettextInterpolation do
defp format_bindings(bindings) when is_map(bindings) and map_size(bindings) === 0, do: ""

defp format_bindings(bindings) when is_map(bindings) do
bindings = Enum.map_join(bindings, ",", fn {key, value} -> "#{key}=#{format_value(value)}" end)
bindings =
bindings
|> Enum.sort(fn {k1, _}, {k2, _} -> k1 < k2 end)
|> Enum.map_join(",", fn {key, value} -> "#{key}=#{format_value(value)}" end)

"[#{bindings}]"
end

Expand Down

0 comments on commit 5e56e7a

Please sign in to comment.