Skip to content

Commit

Permalink
rename sanitize/1 to prepare_for_json/1
Browse files Browse the repository at this point in the history
  • Loading branch information
woylie authored and rrrene committed Jan 15, 2022
1 parent 8daf3a5 commit d953d64
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
20 changes: 10 additions & 10 deletions lib/credo/cli/output/formatter/json.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,34 @@ defmodule Credo.CLI.Output.Formatter.JSON do

def print_map(map) do
map
|> sanitize()
|> prepare_for_json()
|> Jason.encode!(pretty: true)
|> UI.puts()
end

def sanitize(term)
def prepare_for_json(term)
when is_atom(term) or is_number(term) or is_binary(term) do
term
end

def sanitize([]), do: []
def sanitize([h | t]), do: [sanitize(h) | sanitize(t)]
def prepare_for_json([]), do: []
def prepare_for_json([h | t]), do: [prepare_for_json(h) | prepare_for_json(t)]

def sanitize(%Regex{} = regex), do: "~r/#{Regex.source(regex)}/"
def prepare_for_json(%Regex{} = regex), do: "~r/#{Regex.source(regex)}/"

def sanitize(%{} = term) do
def prepare_for_json(%{} = term) do
Enum.into(term, %{}, fn {key, value} ->
{sanitize(key), sanitize(value)}
{prepare_for_json(key), prepare_for_json(value)}
end)
end

def sanitize(term) when is_tuple(term) do
def prepare_for_json(term) when is_tuple(term) do
term
|> Tuple.to_list()
|> sanitize()
|> prepare_for_json()
end

def sanitize(term) do
def prepare_for_json(term) do
inspect(term)
end

Expand Down
4 changes: 2 additions & 2 deletions test/credo/cli/output/formatter/json_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ defmodule Credo.CLI.Output.Formatter.JsonTest do
assert JSON.print_map(%{"option" => ~r/foo/}) == nil
end

test "sanitize/1 sanitizes values" do
assert JSON.sanitize(%{
test "prepare_for_json/1 converts values invalid in json" do
assert JSON.prepare_for_json(%{
"bool" => true,
"list" => ["a", %{"a" => "b", "b" => ~r/foo/}],
"map" => %{"c" => "d", "e" => ["f", 8, ~r/foo/]},
Expand Down

0 comments on commit d953d64

Please sign in to comment.