From d032c4fdb68b812b0f08aebc11bdedf515ab4925 Mon Sep 17 00:00:00 2001 From: Beata Date: Mon, 23 Jun 2025 13:46:16 +0200 Subject: [PATCH 1/5] latitude must be a number or a binary to be formatted as a coordinate Co-authored-by: Stefan Fochler --- lib/value_formatters.ex | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/value_formatters.ex b/lib/value_formatters.ex index 3452c70..57a0f19 100644 --- a/lib/value_formatters.ex +++ b/lib/value_formatters.ex @@ -88,8 +88,8 @@ defmodule ValueFormatters do %DateTime{} -> "date" %NaiveDateTime{} -> "date" %Time{} -> "date" - [_lat, _lng, _radius] -> "coordinates" - [_lat, _lng] -> "coordinates" + [lat, _lng, _radius] when is_number(lat) or is_binary(lat) -> "coordinates" + [lat, _lng] when is_number(lat) or is_binary(lat) -> "coordinates" %{"lat" => _lat, "lng" => _lng, "radius" => _radius} -> "coordinates" %{"lat" => _lat, "lng" => _lng} -> "coordinates" _ -> "The type of value #{inspect(value)} is not supported." From a3f5a7a8fdadeaf571852ee8b55743adc3e0e132 Mon Sep 17 00:00:00 2001 From: Beata Date: Mon, 23 Jun 2025 13:51:19 +0200 Subject: [PATCH 2/5] tests for arrays --- test/value_formatters_test.exs | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/test/value_formatters_test.exs b/test/value_formatters_test.exs index 938df65..5b5b1eb 100644 --- a/test/value_formatters_test.exs +++ b/test/value_formatters_test.exs @@ -554,6 +554,11 @@ 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 "inference object with radius" do assert ValueFormatters.to_string( %{"lat" => 43.1298, "lng" => 54.1234, "radius" => 1}, @@ -578,6 +583,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 +616,32 @@ 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 + end + describe "render" do test "render unit html" do assert ValueFormatters.to_string( From 762874eaeaf512364e5ef9f9c3499dc4eb290756 Mon Sep 17 00:00:00 2001 From: Beata Date: Mon, 23 Jun 2025 13:53:20 +0200 Subject: [PATCH 3/5] fix typo --- test/value_formatters_test.exs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/test/value_formatters_test.exs b/test/value_formatters_test.exs index 5b5b1eb..72b7e6b 100644 --- a/test/value_formatters_test.exs +++ b/test/value_formatters_test.exs @@ -555,7 +555,11 @@ defmodule ValueFormattersTest do end test "full coordinates as string" do - assert ValueFormatters.to_string("123.1345, 34.123, 2", %{"format" => "coordinates"}, @opts) == + assert ValueFormatters.to_string( + ["123.1345", "34.123", "2"], + %{"format" => "coordinates"}, + @opts + ) == {:ok, "123.1345°, 34.123°, 2 m"} end From cf504048bdca1836768b46cd75260812c1a639ff Mon Sep 17 00:00:00 2001 From: Beata Date: Mon, 23 Jun 2025 14:49:53 +0200 Subject: [PATCH 4/5] check that all values in a list of coordinates are either a binary or a number --- lib/value_formatters.ex | 38 ++++++++++++++++++++++++++-------- test/value_formatters_test.exs | 15 ++++++++++++++ 2 files changed, 44 insertions(+), 9 deletions(-) diff --git a/lib/value_formatters.ex b/lib/value_formatters.ex index 57a0f19..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] when is_number(lat) or is_binary(lat) -> "coordinates" - [lat, _lng] when is_number(lat) or is_binary(lat) -> "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/test/value_formatters_test.exs b/test/value_formatters_test.exs index 72b7e6b..e91c886 100644 --- a/test/value_formatters_test.exs +++ b/test/value_formatters_test.exs @@ -563,6 +563,15 @@ defmodule ValueFormattersTest do {: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}, @@ -644,6 +653,12 @@ defmodule ValueFormattersTest do "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 From 18a1fa84c4ccb6272a80ff9f1b57ba17e54467a2 Mon Sep 17 00:00:00 2001 From: Beata Date: Mon, 23 Jun 2025 16:35:06 +0200 Subject: [PATCH 5/5] bump version to 0.1.3 --- mix.exs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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,