marocchino / list_to_csv

Convert a list of nested maps to `list(list(String.t))` or CSV. Can be used with GraphQL.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ListToCsv

Convert list of nested map to two dimensional list of strings.

Example

[
  %{
    name: "name1",
    items: [
      %{title: "title1", code: "code1"},
      %{title: "title2", code: "code2"},
      %{title: "title3", code: "code3"}
    ]
  },
  %{
    name: "name2",
    items: [
      %{title: "title4", code: "code4"},
      %{title: "title5", code: "code5"},
      %{title: "title6", code: "code6"},
      %{title: "title7", code: "code7"},
      %{title: "title8", code: "code8"}
    ]
  }
]
|> ListToCsv.parse(
  headers: [
    "名前",
    "アイテム#名",
    "アイテム#コード",
    "item overflow?"
  ],
  keys: [
    :name,
    [:items, :N, :title],
    [:items, :N, :code],
    [:items, &(length(&1) > 4)]
  ],
  length: [items: 4]
)
=> [
  ["名前", "アイテム1名", "アイテム1コード", "アイテム2名",
   "アイテム2コード", "アイテム3名", "アイテム3コード",
   "アイテム4名", "アイテム4コード", "item overflow?"],
  ["name1", "title1", "code1", "title2", "code2", "title3", "code3", "", "",
   "false"],
  ["name2", "title4", "code4", "title5", "code5", "title6", "code6", "title7",
   "code7", "true"]
]

Installation

If available in Hex, the package can be installed by adding list_to_csv to your list of dependencies in mix.exs:

def deps do
  [
    {:list_to_csv, "~> 1.0.0"}
  ]
end

Documentation can be generated with ExDoc and published on HexDocs. Once published, the docs can be found at https://hexdocs.pm/list_to_csv.

About

Convert a list of nested maps to `list(list(String.t))` or CSV. Can be used with GraphQL.

License:MIT License


Languages

Language:Elixir 100.0%