elixir-todo-list/lib/todo/csv_importer.ex

30 lines
548 B
Elixir
Raw Normal View History

2024-08-04 13:21:15 +02:00
defmodule Todo.CSVImporter do
alias Todo.List
def import(path) do
path
|> File.stream!()
|> Stream.map(&parse_line/1)
|> List.new()
end
defp parse_line(line) do
line
|> String.trim()
|> String.split(",")
|> create_entry()
end
defp create_entry([date, title]) do
final_date = parse_date(date)
%{date: final_date, title: title}
end
defp parse_date(string) do
[year, month, day] = string |> String.split("-") |> Enum.map(&String.to_integer/1)
Date.new!(year, month, day)
end
end