Slab.Export (Slab v2.1.0)

Copy Markdown View Source

Serializes records to CSV for the Export tab downloads.

Used internally by Slab.table/1 when export_tab? is set, and public so applications can reuse the same serialization — for example in a custom export controller streaming more rows than the tab's in-socket downloads should carry.

Summary

Functions

Renders records as an RFC 4180 CSV binary.

Functions

csv(records, columns)

Renders records as an RFC 4180 CSV binary.

columns is a list of {header, accessor} tuples — one per CSV column, in order. An accessor is either a field to read from the record or a 1-arity function receiving the record and returning the value — the latter is how computed columns export:

Slab.Export.csv(records, [
  {"Name", :name},
  {"Products", fn record -> Enum.map_join(record.products, ", ", & &1.name) end}
])

Each value is then formatted:

  • nil renders as an empty field
  • dates and times render as ISO 8601
  • lists render their formatted members joined with ", "
  • anything else renders with to_string/1, falling back to inspect/1 for values without a string representation (like maps)

Fields containing commas, quotes, or line breaks are quoted with internal quotes doubled; rows end with \r\n.

Examples

iex> Slab.Export.csv([%{name: "Ada, Countess", active: true}], [{"Name", :name}, {"Active", :active}])
"Name,Active\r\n\"Ada, Countess\",true\r\n"