Skip to content

Commit

Permalink
Merge pull request #3 from wingyplus/iss-2
Browse files Browse the repository at this point in the history
Insert final new line when format json file
  • Loading branch information
wingyplus authored Jan 5, 2022
2 parents f032d37 + 3589422 commit 1bb52dc
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lib/json_formatter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,18 @@ defmodule JsonFormatter do
end

@impl true
def format(contents, _opts) do
def format(contents, opts) do
contents
|> Jason.decode!(objects: :ordered_objects)
|> Jason.encode!(pretty: [indent: " ", line_separator: "\n", after_colon: " "])
|> maybe_insert_final_newline(opts)
end

defp maybe_insert_final_newline(content, opts) do
if opts[:extension] do
content <> "\n"
else
content
end
end
end

0 comments on commit 1bb52dc

Please sign in to comment.