Skip to content
Open
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
18 changes: 16 additions & 2 deletions lib/live_select.ex
Original file line number Diff line number Diff line change
Expand Up @@ -543,15 +543,29 @@ defmodule LiveSelect do

iex> decode(["{\"name\":\"New York City\",\"pos\":[-74.00597,40.71427]}","{\"name\":\"Stockholm\",\"pos\":[18.06871,59.32938]}"])
[%{"name" => "New York City","pos" => [-74.00597,40.71427]}, %{"name" => "Stockholm","pos" => [18.06871,59.32938]}]

Non-JSON strings (e.g. raw text from a search input) are returned as-is:

iex> decode("Glon")
"Glon"

iex> decode(["Berlin", "Rome"])
["Berlin", "Rome"]
"""
def decode(selection) do
json = Phoenix.json_library()

case selection do
nil -> []
"" -> nil
selection when is_list(selection) -> Enum.map(selection, &json.decode!/1)
selection -> json.decode!(selection)
selection when is_list(selection) -> Enum.map(selection, &safe_decode(json, &1))
selection -> safe_decode(json, selection)
end
end

defp safe_decode(json, value) do
json.decode!(value)
rescue
_ -> value
end
end
42 changes: 42 additions & 0 deletions test/live_select/decode_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
defmodule LiveSelect.DecodeTest do
@moduledoc false

use ExUnit.Case, async: true

describe "decode/1" do
test "decodes nil to empty list" do
assert LiveSelect.decode(nil) == []
end

test "decodes empty string to nil" do
assert LiveSelect.decode("") == nil
end

test "decodes a JSON-encoded map" do
assert LiveSelect.decode(~s({"name":"Berlin","pos":[13.41,52.52]})) ==
%{"name" => "Berlin", "pos" => [13.41, 52.52]}
end

test "decodes a list of JSON-encoded maps" do
assert LiveSelect.decode([
~s({"name":"Berlin"}),
~s({"name":"Rome"})
]) == [
%{"name" => "Berlin"},
%{"name" => "Rome"}
]
end

test "returns plain string as-is when it's not valid JSON" do
assert LiveSelect.decode("Glon") == "Glon"
end

test "returns string with special characters as-is when not valid JSON" do
assert LiveSelect.decode("São Paulo") == "São Paulo"
end

test "returns list elements as-is when they are not valid JSON" do
assert LiveSelect.decode(["Berlin", "Rome"]) == ["Berlin", "Rome"]
end
end
end
Loading