defmodule WandererApp.Utils.CSVUtil do @moduledoc false alias NimbleCSV.RFC4180, as: CSV def get_column_names(file) do file |> File.stream!() |> CSV.parse_stream(skip_headers: false) |> Enum.fetch!(0) |> Enum.with_index() |> Map.new(fn {val, num} -> {num, val} end) end def csv_row_to_table_record(file, mapper) do column_names = get_column_names(file) file |> File.stream!() |> CSV.parse_stream(skip_headers: true) |> Enum.map(fn row -> row |> Enum.with_index() |> Map.new(fn {val, num} -> {column_names[num], val} end) |> mapper.() end) end end