refactored table_tosting

This commit is contained in:
VorTechnix 2021-10-10 14:39:50 -07:00
parent b72d36816e
commit 191e99f0b5

View file

@ -2,16 +2,27 @@
-- @param tbl table input table
-- @param sep string key value seperator
-- @param new_line string key value pair delimiter
-- @param max_depth number max recursion depth (optional)
-- @return string concatenated table pairs
local function table_tostring(tbl, sep, new_line)
local function table_tostring(tbl, sep, new_line, max_depth)
if type(sep) ~= "string" then sep = ": " end
if type(new_line) ~= "string" then new_line = ", " end
if type(max_depth) == "number" then max_depth = {depth=0,max=max_depth}
elseif type(max_depth) ~= "table" then max_depth = {depth=0,max=5} end
local ret = {}
if type(tbl) ~= "table" then return "Error: input not table!" end
for key,value in pairs(tbl) do
table.insert(ret,tostring(key) .. sep .. tostring(value) .. new_line)
if type(value) == "table" and max_depth.depth < max_depth.max then
table.insert(ret,tostring(key) .. sep ..
"{" .. table_tostring(value,sep,new_line,{max_depth.depth+1,max_depth.max}) .. "}")
else
table.insert(ret,tostring(key) .. sep .. tostring(value))
end
end
return table.concat(ret,"")
return table.concat(ret,new_line)
end
-- Test:
-- /lua v1 = { x= 0.335, facing= { axis= "z", sign= -1 } }; print(worldeditadditions.table.tostring(v1))
return table_tostring