diff --git a/lib/cldr/backend.ex b/lib/cldr/backend.ex index 09d5467..c0aaaa0 100644 --- a/lib/cldr/backend.ex +++ b/lib/cldr/backend.ex @@ -250,8 +250,8 @@ defmodule Cldr.Currency.Backend do ## Arguments - * `currency_code` is a `binary` or `atom` representation of an - ISO 4217 currency code. + * `currency_or_currency_code` is a `binary` or `atom` representation + of an ISO 4217 currency code, or a `%Cldr.Currency{}` struct. ## Examples @@ -288,14 +288,14 @@ defmodule Cldr.Currency.Backend do }} """ - @spec currency_for_code(Cldr.Currency.code, Keyword.t()) :: - {:ok, Cldr.Currency.t} | {:error, {module(), String.t()}} + @spec currency_for_code(Cldr.Currency.code() | Cldr.Currency.t(), Keyword.t()) :: + {:ok, Cldr.Currency.t()} | {:error, {module(), String.t()}} def currency_for_code( - currency_code, + currency_or_currency_code, options \\ [locale: unquote(backend).default_locale()] ) do - Cldr.Currency.currency_for_code(currency_code, unquote(backend), options) + Cldr.Currency.currency_for_code(currency_or_currency_code, unquote(backend), options) end defp get_currency_metadata(code, nil) do diff --git a/lib/cldr/currency.ex b/lib/cldr/currency.ex index 55980d0..303ac1c 100644 --- a/lib/cldr/currency.ex +++ b/lib/cldr/currency.ex @@ -771,8 +771,8 @@ defmodule Cldr.Currency do ## Arguments - * `currency_code` is a `binary` or `atom` representation of an - ISO 4217 currency code. + * `currency_or_currency_code` is a `binary` or `atom` representation + of an ISO 4217 currency code, or a `%Cldr.Currency{}` struct. * `backend` is any module that includes `use Cldr` and therefore is a `Cldr` backend module @@ -820,16 +820,18 @@ defmodule Cldr.Currency do """ - @spec currency_for_code(code, Cldr.backend(), Keyword.t()) :: - {:ok, t} | {:error, {module(), String.t()}} + @spec currency_for_code(code() | t(), Cldr.backend(), Keyword.t()) :: + {:ok, t()} | {:error, {module(), String.t()}} - def currency_for_code(currency_code, backend, options \\ []) do + def currency_for_code(currency_or_currency_code, backend, options \\ []) + def currency_for_code(%__MODULE__{} = currency, _backend, _options), do: {:ok, currency} + + def currency_for_code(currency_code, backend, options) do {locale, backend} = Cldr.locale_and_backend_from(options[:locale], backend) with {:ok, code} <- known_currency_code(currency_code), {:ok, locale} <- Cldr.validate_locale(locale, backend), {:ok, currencies} <- currencies_for_locale(locale, backend) do - {:ok, Map.get_lazy(currencies, code, fn -> Map.get(private_currencies(), code) end)} end end