CSV
The CSV module provides functions to encode and decode CSV data.
Example
The following code demonstrates how to encode and decode CSV data.
local MyModule = select(2, ...).MyModule
local CSV = MyModule:From("LibTSMUtil"):Include("Format.CSV")
local FIELDS = {"name", "class", "level"}
local encodeContext = CSV.EncodeStart(FIELDS)
CSV.EncodeAddRowDataRaw(encodeContext, "Player1", "Monk", 15)
CSV.EncodeAddRowDataRaw(encodeContext, "Player2", "Mage", 28)
local csvData = CSV.EncodeEnd(encodeContext)
print(csvData) -- name,class,level\nPlayer1,Monk,15\nPlayer2,Mage,28
local decodeContext = CSV.DecodeStart(csvData, FIELDS)
for name, class, level in CSV.DecodeIterator(decodeContext) do
print(name, class, level)
end
-- Player1 Monk 15
-- Player2 Mage 28
CSV.DecodeEnd(decodeContext)
API
-
class Format.CSV:
LibTSMModule -
staticmethod EncodeStart(keys:
string[]):table Creates a CSV encoding context for the specified keys.
- Parameters:
keys (
string[]) – The keys which are being encoded
-
staticmethod EncodeAddRowData(context:
table, data:table) Adds a row to the CSV encoding context.
- Parameters:
context (
table) – The CSV encoding contextdata (
table) – The data for the row
-
staticmethod EncodeAddRowDataRaw(context:
table, ...:string) Adds a raw row to the CSV encoding context.
- Parameters:
context (
table) – The CSV encoding context... (
string) – The raw data for the row
-
staticmethod EncodeEnd(context:
table):string Ends a CSV encoding context and returns the resulting CSV string.
- Parameters:
context (
table) – The CSV encoding context
-
staticmethod Encode(keys:
string[], data:table):string Encodes the specified data to a CSV string.
- Parameters:
keys (
string[]) – The list of keys to encodedata (
table) – The data to encode
-
staticmethod DecodeStart(str:
string, fields:string[]): (context?:table) Creates a CSV decoding context for the specified fields.
- Parameters:
str (
string) – The CSV encoded datafields (
string[]) – The fields which are being decoded
-
staticmethod DecodeIterator(context:
table): fun(): ...unknown,table Iterates over the CSV encoded data.
- Parameters:
context (
table) – The CSV decoding context- Returns:
_1 (fun(): ...
unknown) – Iterator with fields matching the decoded values
-
staticmethod DecodeEnd(context:
table):boolean Ends a CSV decoding context and returns whether or not the data was fully decoded successfully.
- Parameters:
context (
table) – The CSV decoding context
-
staticmethod EncodeStart(keys: