diff --git a/lib/value_formatters.ex b/lib/value_formatters.ex index 3452c70..35e4903 100644 --- a/lib/value_formatters.ex +++ b/lib/value_formatters.ex @@ -84,15 +84,35 @@ defmodule ValueFormatters do defp determine_value_type(value) do case value do - %Date{} -> "date" - %DateTime{} -> "date" - %NaiveDateTime{} -> "date" - %Time{} -> "date" - [_lat, _lng, _radius] -> "coordinates" - [_lat, _lng] -> "coordinates" - %{"lat" => _lat, "lng" => _lng, "radius" => _radius} -> "coordinates" - %{"lat" => _lat, "lng" => _lng} -> "coordinates" - _ -> "The type of value #{inspect(value)} is not supported." + %Date{} -> + "date" + + %DateTime{} -> + "date" + + %NaiveDateTime{} -> + "date" + + %Time{} -> + "date" + + [lat, lng, radius] + when (is_number(lat) or is_binary(lat)) and + (is_number(lng) or is_binary(lng)) and (is_number(radius) or is_binary(radius)) -> + "coordinates" + + [lat, lng] + when (is_number(lat) or is_binary(lat)) and (is_number(lng) or is_binary(lng)) -> + "coordinates" + + %{"lat" => _lat, "lng" => _lng, "radius" => _radius} -> + "coordinates" + + %{"lat" => _lat, "lng" => _lng} -> + "coordinates" + + _ -> + "The type of value #{inspect(value)} is not supported." end end diff --git a/mix.exs b/mix.exs index d1b61a5..8828d82 100644 --- a/mix.exs +++ b/mix.exs @@ -4,7 +4,7 @@ defmodule ValueFormatters.MixProject do def project do [ app: :value_formatters, - version: "0.1.2", + version: "0.1.3", elixir: "~> 1.15", elixirc_paths: elixirc_paths(Mix.env()), start_permanent: Mix.env() == :prod, diff --git a/test/value_formatters_test.exs b/test/value_formatters_test.exs index 938df65..e91c886 100644 --- a/test/value_formatters_test.exs +++ b/test/value_formatters_test.exs @@ -554,6 +554,24 @@ defmodule ValueFormattersTest do {:ok, "123.1345°, 34.123°, 2 m"} end + test "full coordinates as string" do + assert ValueFormatters.to_string( + ["123.1345", "34.123", "2"], + %{"format" => "coordinates"}, + @opts + ) == + {:ok, "123.1345°, 34.123°, 2 m"} + end + + test "mixed coordinates with radius" do + assert ValueFormatters.to_string( + [123.1345, "34.123", 2], + %{"format" => "coordinates"}, + @opts + ) == + {:ok, "123.1345°, 34.123°, 2 m"} + end + test "inference object with radius" do assert ValueFormatters.to_string( %{"lat" => 43.1298, "lng" => 54.1234, "radius" => 1}, @@ -578,6 +596,11 @@ defmodule ValueFormattersTest do {:ok, "123.1345°, 34.123°"} end + test "inference list as string" do + assert ValueFormatters.to_string(["123.1345", "34.123"], %{}, @opts) == + {:ok, "123.1345°, 34.123°"} + end + test "with radius show radius" do assert ValueFormatters.to_string( %{"lat" => 123.134567, "lng" => 34.12345, "radius" => 2}, @@ -606,10 +629,38 @@ defmodule ValueFormattersTest do end end - test "call with empty object format desription" do + test "call with empty object format description" do assert ValueFormatters.to_string(3.14244453, %{"precision" => 2}, @opts) == {:ok, "3.14"} end + describe "array" do + test "doesn't format an arrays of maps with 2 elements" do + assert ValueFormatters.to_string([%{"foo" => "bar"}, %{"bar" => "foo"}], %{}, @opts) == + { + :error, + "Unsupported format The type of value [%{\"foo\" => \"bar\"}, %{\"bar\" => \"foo\"}] is not supported." + } + end + + test "doesn't format an arrays of maps with 3 elements" do + assert ValueFormatters.to_string( + [%{"foo" => "bar"}, %{"bar" => "foo"}, %{"baz" => "qux"}], + %{}, + @opts + ) == + { + :error, + "Unsupported format The type of value [%{\"foo\" => \"bar\"}, %{\"bar\" => \"foo\"}, %{\"baz\" => \"qux\"}] is not supported." + } + end + + test "doesn't format an array with only one map" do + assert ValueFormatters.to_string([123, 456, %{"foo" => "bar"}], %{}, @opts) == + {:error, + "Unsupported format The type of value [123, 456, %{\"foo\" => \"bar\"}] is not supported."} + end + end + describe "render" do test "render unit html" do assert ValueFormatters.to_string(