feat: add table parsing
This commit is contained in:
parent
33d854ffd4
commit
792738c5cc
1 changed files with 23 additions and 0 deletions
|
@ -77,4 +77,27 @@ function Table.average(table)
|
||||||
return Table.sum(table) / #table
|
return Table.sum(table) / #table
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--Parse a basic list into a structured table. Return an error if the number of arguments is not the same
|
||||||
|
---@param table table the list to parse into a table
|
||||||
|
---@param structure table the structure to create the table : each name correspond to an attribute of the parsed table
|
||||||
|
---@param nullableNbr integer the number of nullable argument (at the end of the functions) (can be null)
|
||||||
|
---@return table parsedTable the parsed table
|
||||||
|
function Table.parse(table, structure, nullableNbr)
|
||||||
|
local parsedTable = {}
|
||||||
|
assert(table ~= nil, "The table to parse can't be null")
|
||||||
|
assert(structure ~= nil, "The table structure can't be null")
|
||||||
|
nullableNbr = nullableNbr or 0
|
||||||
|
|
||||||
|
if ((#table) > (#structure)) or ((#table) < (#structure - nullableNbr)) then
|
||||||
|
error("The table to parse doesn't have the right number of arguments: " .. #table .. " instead of " .. #structure .. " and " .. nullableNbr .. " nullables")
|
||||||
|
else
|
||||||
|
for i, key in ipairs(structure) do
|
||||||
|
--print(i, key, table[i])
|
||||||
|
parsedTable[key] = table[i]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return parsedTable
|
||||||
|
end
|
||||||
|
|
||||||
return Table
|
return Table
|
||||||
|
|
Loading…
Reference in a new issue