Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ flagpack-*.tar
# Temporary files, for example, from tests.
/tmp/

.DS_Store
.DS_Store

.idea/
1 change: 1 addition & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
elixir 1.14.1
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ The package can be installed by adding `flagpack` to your list of dependencies i
```elixir
def deps do
[
{:flagpack, "~> 0.1.0"}
{:flagpack, "~> 0.3.0"}
]
end
```
Expand Down
2 changes: 1 addition & 1 deletion assets/flagpack.exs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ defmodule Flagpack do

<%= for %{func: func, alpha: alpha, svg: svg} <- @flags do %>
@doc """
Renders the <%= Helpers.country_name_by_alpha(alpha) %> flag.
Renders the <%= Flagpack.Helpers.country_name_by_alpha(alpha) %> flag.

## Examples
<Flagpack.<%= func %> />
Expand Down
11 changes: 10 additions & 1 deletion lib/helpers.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule Helpers do
defmodule Flagpack.Helpers do
@country_codes [
%{country_name: "Andorra", alpha2: "AD", alpha3: "AND", numeric: "020"},
%{
Expand Down Expand Up @@ -517,6 +517,15 @@ defmodule Helpers do
|> Map.get(:country_name)
end

def country_alpha3_by_alpha2(alpha2) do
alpha2 = String.upcase(alpha2)

case Enum.find(@country_codes, &(get_alpha2(&1) == alpha2)) do
nil -> nil
info = %{} -> Map.get(info, :alpha3)
end
end

Comment on lines +520 to +528
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this function?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Flo0807 I had saved alpha2 for languages and thought it's useful to find country alpha3 version from the library to use later for rendering flags. However may be it's not a best place for it and is project related. Idk, it's a helper function.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Flo0807 I had language code in 2 char formats and wanted quickly find what alpha3 representation is supported by Flagpack and call rendering if it's not nil. Idk, it could be app related code though I've added it to the helper.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to switch to alpha-2 codes for rendering flags in a future version of this package (see #35). How about adding a Flagpack.Helpers.get_country_codes function that returns the list of structs containing alpha-2, alpha-3, country_name and numeric information?

def get_country_codes, do: @country_codes

Then you can implement the above filtering in your application, and we do not have a function mainly cut to your application.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Flo0807 it sounds reasonable. what do you mean by numeric information?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See the Flag Index or the list of country_codes in the Helper module.

In addition to the name, alpha2 and alpha3, every flag also has a unique numeric code.

defp get_alpha2(country), do: Map.get(country, :alpha2, "")
defp get_alpha3(country), do: Map.get(country, :alpha3, "")
end