diff --git a/README.md b/README.md
index 1c2cfab..8c35a61 100644
--- a/README.md
+++ b/README.md
@@ -18,6 +18,8 @@ iex> CurrencyFormatter.format(654300, :eur)
iex> CurrencyFormatter.format(654300, :eur, keep_decimals: true)
"€6.543,00"
+iex> CurrencyFormatter.format(1, "huf")
+"0,01Ft"
iex> CurrencyFormatter.format(123456)
"$1,234.56"
@@ -34,39 +36,19 @@ iex> CurrencyFormatter.format(654321, "AUD", disambiguate: true)
Formatting cents to a currency raw html string
```elixir
- iex> CurrencyFormatter.raw_html_format(123456, "EUR")
- ~s[€1.234,56]
- """
+iex> CurrencyFormatter.raw_html_format(123456, "EUR")
+ ~s[€1.234,56]
```
Formatting cents to a currency safe(phoenix) html string
```elixir
iex> CurrencyFormatter.html_format(123456, "EUR")
- [
- safe: [
- 60,
- "span",
- [[32, "class", 61, 34, "currency-formatter-symbol", 34]],
- 62,
- "€",
- 60,
- 47,
- "span",
- 62
- ],
- safe: [
- 60,
- "span",
- [[32, "class", 61, 34, "currency-formatter-amount", 34]],
- 62,
- "1.234,56",
- 60,
- 47,
- "span",
- 62
- ]
- ]
+ {:safe,
+ [60, "span", 32, "class", 61, 34, "currency-formatter-symbol", 34,
+ 62, "€", 60, 47, "span", 62, 60, "span", 32, "class", 61, 34,
+ "currency-formatter-amount", 34, 62, "1.234,56", 60, 47, "span",
+ 62]}
```
Requesting formatting instructions for a currency
@@ -126,7 +108,7 @@ As this is [available in Hex](https://hex.pm/docs/publish), the package can be i
```elixir
def deps do
- [{:currency_formatter, "~> 0.4"}]
+ [{:currency_formatter, "~> 0.9.0"}]
end
```
diff --git a/lib/currency_formatter.ex b/lib/currency_formatter.ex
index 18d04f6..3a634d3 100644
--- a/lib/currency_formatter.ex
+++ b/lib/currency_formatter.ex
@@ -49,6 +49,7 @@ defmodule CurrencyFormatter do
|> split_units_and_subunits
|> handle_cents(format, opts)
|> set_symbol(format, opts)
+ |> flatten()
end
@doc """
@@ -58,30 +59,11 @@ defmodule CurrencyFormatter do
## example
iex> CurrencyFormatter.html_format(123456, "EUR")
- [
- safe: [
- 60,
- "span",
- [[32, "class", 61, 34, "currency-formatter-symbol", 34]],
- 62,
- "€",
- 60,
- 47,
- "span",
- 62
- ],
- safe: [
- 60,
- "span",
- [[32, "class", 61, 34, "currency-formatter-amount", 34]],
- 62,
- "1.234,56",
- 60,
- 47,
- "span",
- 62
- ]
- ]
+ {:safe,
+ [60, "span", 32, "class", 61, 34, "currency-formatter-symbol", 34,
+ 62, "€", 60, 47, "span", 62, 60, "span", 32, "class", 61, 34,
+ "currency-formatter-amount", 34, 62, "1.234,56", 60, 47, "span",
+ 62]}
"""
def html_format(number, currency, opts \\ Keyword.new()) do
opts = Keyword.put_new(opts, :html, true)
@@ -103,8 +85,7 @@ defmodule CurrencyFormatter do
def raw_html_format(number, currency, opts \\ Keyword.new()) do
number
|> html_format(currency, opts)
- |> Enum.map(&Phoenix.HTML.safe_to_string/1)
- |> Enum.join("")
+ |> Phoenix.HTML.safe_to_string()
end
@doc """
@@ -336,8 +317,7 @@ defmodule CurrencyFormatter do
defp set_symbol(number_string, config, opts) do
if Keyword.get(opts, :html) do
- spans = wrap_in_spans(amount: number_string, symbol: get_symbol(config, opts))
- Phoenix.HTML.safe_to_string(spans)
+ wrap_in_spans(amount: number_string, symbol: get_symbol(config, opts))
else
number_string <> get_symbol(config, opts)
end
@@ -365,4 +345,9 @@ defmodule CurrencyFormatter do
@spec get_disambiguous_symbol(map) :: String.t()
defp get_disambiguous_symbol(%{"disambiguate_symbol" => symbol}), do: symbol
defp get_disambiguous_symbol(config), do: config["symbol"]
+
+ defp flatten(list) when is_list(list),
+ do: {:safe, list |> Enum.map(&(elem(&1, 1))) |> List.flatten()}
+
+ defp flatten(data), do: data
end
diff --git a/mix.exs b/mix.exs
index c90699b..47306ac 100644
--- a/mix.exs
+++ b/mix.exs
@@ -4,7 +4,7 @@ defmodule CurrencyFormatter.Mixfile do
def project do
[
app: :currency_formatter,
- version: "0.8.1",
+ version: "0.9.0",
description: "A library to help with formatting a number to a currency using iso standards and other convenience functions related to formatting currencies",
package: package(),
elixir: "~> 1.7",
@@ -43,7 +43,7 @@ defmodule CurrencyFormatter.Mixfile do
{:earmark, ">= 0.0.0", only: :dev},
{:ex_doc, ">= 0.0.0", only: :dev},
{:excoveralls, ">= 0.0.0", only: :test},
- {:phoenix_html, ">= 0.0.0"},
+ {:phoenix_html, ">= 2.12.0"},
{:poison, "~> 3.1.0"},
]
end
diff --git a/test/currency_formatter_test.exs b/test/currency_formatter_test.exs
index 9265436..d0e59e6 100644
--- a/test/currency_formatter_test.exs
+++ b/test/currency_formatter_test.exs
@@ -226,4 +226,9 @@ defmodule CurrencyFormatterTest do
assert Map.get(currencies, String.downcase(code)) == nil
end)
end
+
+ test "can convert to html string" do
+ assert "€0,01" == CurrencyFormatter.html_format(1, :eur) |> Phoenix.HTML.safe_to_string()
+ assert "1د.إ" == CurrencyFormatter.format("1.00", "AED", html: true) |> Phoenix.HTML.safe_to_string()
+ end
end