Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 29 additions & 9 deletions lib/value_formatters.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
53 changes: 52 additions & 1 deletion test/value_formatters_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -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},
Expand Down Expand Up @@ -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(
Expand Down