Skip to content

Commit

Permalink
adding multi row csv line reading
Browse files Browse the repository at this point in the history
  • Loading branch information
ghatighorias committed Sep 8, 2017
1 parent 09312c9 commit 723c8b0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 17 deletions.
45 changes: 29 additions & 16 deletions lib/course_planner/csv_parser.ex
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
defmodule CoursePlanner.CsvParser do
@moduledoc false

@csv_seperator ","
@csv_row_seperator "\n"
@csv_element_seperator ","

def parse(csv_data, column_count, trim_elements \\ true) do
splited_csv = csv_splitter(csv_data)
trimmed_splitted_csv =
if trim_elements do
trim_splitted_csv(splited_csv)
else
splited_csv
end

if validate_column_count(trimmed_splitted_csv, column_count) do
{:ok, Enum.chunk_every(trimmed_splitted_csv, column_count)}
trimmed_and_splitted_csv = csv_splitter(csv_data, trim_elements)

if validate_column_count(trimmed_and_splitted_csv, column_count) do
{:ok, trimmed_and_splitted_csv}
else
{:error, "Input data is not matching the column number."}
end
Expand All @@ -23,14 +18,32 @@ defmodule CoursePlanner.CsvParser do
Enum.map(splited_csv, &(String.trim(&1)))
end

defp csv_splitter(csv_data) do
String.split(csv_data, @csv_seperator)
defp csv_splitter(csv_data, trim_elements) do
csv_data
|> String.split(@csv_row_seperator, trim: true)
|> Enum.map(fn(row) ->
splited_csv = String.split(row, @csv_element_seperator)

if trim_elements do
trim_splitted_csv(splited_csv)
else
splited_csv
end
end)
end

defp validate_column_count([], _column_count), do: false
defp validate_column_count(trimmed_splitted_csv, column_count) do
trimmed_splitted_csv
|> length()
|> rem(column_count)
|> Kernel.==(0)
|> Enum.reduce(true, fn(row, out) ->
column_count_match =
row
|> length()
|> rem(column_count)
|> Kernel.==(0)

column_count_match && out
end)

end
end
6 changes: 5 additions & 1 deletion test/lib/course_planner/csv_parser_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ defmodule CoursePlanner.CsvParserTest do
end

test "when input has more than one set of data" do
assert CsvParser.parse("a,b,c,d,e,f,g,h", 4) == {:ok, [["a","b","c", "d"], ["e","f","g","h"]]}
data = """
a,b,c,d
e,f,g,h
"""
assert CsvParser.parse(data, 4) == {:ok, [["a","b","c", "d"], ["e","f","g","h"]]}
end

test "parsing csv with element space trimming" do
Expand Down

0 comments on commit 723c8b0

Please sign in to comment.