diff --git a/CHANGELOG.md b/CHANGELOG.md index c95edfe..f7c1587 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,13 @@ # Changelog +## Cldr_Currencies v2.15.0 + +This is the changelog for Cldr_Currencies v2.15.0 released on January 28th, 2023. For older changelogs please consult the release tag on [GitHub](https://github.com/elixir-cldr/cldr_currencies/tags) + +### Enhancements + +* Add `Cldr.Currency.current_territory_currencies/1` and `MyApp.Cldr.Currency.current_territory_currencies/0` that returns a mapping from territory code from currency type for all territories that have a current currency. Thanks for @lawik for the inspiration. + ## Cldr_Currencies v2.14.3 This is the changelog for Cldr_Currencies v2.14.3 released on January 27th, 2023. For older changelogs please consult the release tag on [GitHub](https://github.com/elixir-cldr/cldr_currencies/tags) diff --git a/lib/cldr/backend.ex b/lib/cldr/backend.ex index 97a38f6..7a4bca0 100644 --- a/lib/cldr/backend.ex +++ b/lib/cldr/backend.ex @@ -591,6 +591,30 @@ defmodule Cldr.Currency.Backend do {:error, Cldr.Locale.locale_error(locale)} end + @doc """ + Returns a mapping from a territory code to its + current currency code. + + If a territory has no current currency (like + Antartica, territory code `:AQ`) then no + mapping is returned for that territory. + + ## Returns + + * A map of `{territory_code => Cldr.Currency.t}` + + ## Example + + iex> #{inspect __MODULE__}.current_territory_currencies() + + """ + @doc since: "2.15.0" + + @spec current_territory_currencies() :: %{Cldr.Locale.territory_code() => Cldr.Currency.t()} + def current_territory_currencies do + Cldr.Currency.current_territory_currencies(unquote(backend)) + end + @doc """ Returns a map of the metadata for all currencies for a given locale and raises on error. diff --git a/lib/cldr/currency.ex b/lib/cldr/currency.ex index 47ffa3e..78a49d4 100644 --- a/lib/cldr/currency.ex +++ b/lib/cldr/currency.ex @@ -757,6 +757,47 @@ defmodule Cldr.Currency do end end + @doc """ + Returns a mapping from a territory code to its + current currency code. + + If a territory has no current currency (like + Antartica, territory code `:AQ`) then no + mapping is returned for that territory. + + ## Arguments + + * `backend` is any module that includes `use Cldr` and therefore + is a `Cldr` backend module. The default is `Cldr.default_backend!/0` + + ## Returns + + * A map of `{territory_code => Cldr.Currency.t}` + + """ + @doc since: "2.15.0" + + @spec current_territory_currencies() :: %{Cldr.Locale.territory_code() => t()} + def current_territory_currencies(backend \\ Cldr.default_backend!()) do + territory_currencies() + |> Enum.map(fn + {:ZZ, _currencies} -> + nil + + {territory, _currencies} -> + case current_currency_for_territory(territory) do + nil -> {territory, nil} + currency -> {territory, currency_for_code!(currency, backend)} + end + end) + |> Enum.reject(fn + {_territory, nil} -> true + nil -> true + _other -> false + end) + |> Map.new() + end + @doc """ Returns a list of historic and the current currency for a given locale. @@ -882,9 +923,10 @@ defmodule Cldr.Currency do of an ISO 4217 currency code, or a `t:Cldr.Currency` struct. * `backend` is any module that includes `use Cldr` and therefore - is a `Cldr` backend module + is a `Cldr` backend module. The default is `Cldr.default_backend!/0`. - * `options` is a `Keyword` list of options. + * `options` is a `Keyword` list of options. The default is + `[]`. ## Options @@ -936,13 +978,13 @@ defmodule Cldr.Currency do @spec currency_for_code(code() | t(), Cldr.backend(), Keyword.t()) :: {:ok, t()} | {:error, {module(), String.t()}} - def currency_for_code(currency_or_currency_code, backend, options \\ []) + def currency_for_code(currency_or_currency_code, backend \\ Cldr.default_backend!(), options \\ []) def currency_for_code(%__MODULE__{} = currency, _backend, _options) do {:ok, currency} end - def currency_for_code(currency_code, backend, options) do + def currency_for_code(currency_code, backend, options) when is_atom(backend) and is_list(options) do {locale, backend} = Cldr.locale_and_backend_from(options[:locale], backend) with {:ok, code} <- Cldr.validate_currency(currency_code), @@ -952,6 +994,10 @@ defmodule Cldr.Currency do end end + def currency_for_code(currency_code, nil, [] = options) when is_list(options) do + currency_for_code(currency_code, nil, options) + end + @doc """ Returns the currency metadata for the requested currency code. @@ -1014,7 +1060,7 @@ defmodule Cldr.Currency do @spec currency_for_code!(code() | t(), Cldr.backend(), Keyword.t()) :: t() | no_return() - def currency_for_code!(currency_or_currency_code, backend, options \\ []) do + def currency_for_code!(currency_or_currency_code, backend \\ nil, options \\ []) do case currency_for_code(currency_or_currency_code, backend, options) do {:ok, currency} -> currency {:error, {exception, reason}} -> raise exception, reason diff --git a/mix.exs b/mix.exs index 8daf6f6..625a998 100644 --- a/mix.exs +++ b/mix.exs @@ -1,7 +1,7 @@ defmodule Cldr.Currencies.MixProject do use Mix.Project - @version "2.14.3" + @version "2.15.0" def project do [