Country codes to flag emoji in Elixir

Country codes to flag emoji in Elixir

September 09, 2023|
1 min read
  • elixir

I just read this blog post. It showed, in JavaScript, how to convert country code string to its respective flag emoji, and I wanted to see what it would look like in Elixir. So I pulled up iex and came up with the following:

@doc """
Convert country code to Flag Emoji

## Examples

    iex> to_flag_emoji("US")

    iex> to_flag_emoji("NL")

    iex> to_flag_emoji("CH")

    iex> to_flag_emoji("IT") == to_flag_emoji("it")
def to_flag_emoji(code) do
  |> String.upcase()
  |> String.to_charlist()
  |> + 127397))
  |> to_string()

Here's the explanation:

The flag emoji is a combination of the two unicode region characters, located at unicode position 127462 for the letter A. For CH (Switzerland), we want the indexes to be 127464 and 127469.

And with ExUnit's doctests, we get tests for free from the examples. Gotta love the Elixir stdlib!

© 2023, Dorian Karter