mirror of
https://github.com/sbrl/Minetest-WorldEditAdditions.git
synced 2024-09-12 08:08:30 +02:00
refactored table_tosting
This commit is contained in:
parent
b72d36816e
commit
191e99f0b5
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user