44 lines
971 B
Plaintext

function table.chunk(tbl, size)
size = size or 1
size = size > 0 and size or 1
local result = {}
local tblKeys = table.getKeys(tbl)
for k, v in pairs(tblKeys) do
local chunkId = math.ceil(k / size)
if not result[chunkId] then
result[chunkId] = {}
end
result[chunkId][v] = tbl[v]
end
return result
end
function table.deepcopy(orig)
local orig_type = type(orig)
local copy
if orig_type == 'table' then
copy = {}
for orig_key, orig_value in next, orig, nil do
copy[table.deepcopy(orig_key)] = table.deepcopy(orig_value)
end
setmetatable(copy, table.deepcopy(getmetatable(orig)))
else -- number, string, boolean, etc
copy = orig
end
return copy
end
function table.merge(table1, table2)
local newTable = table.deepcopy(table1)
for k, v in pairs(table2) do
newTable[k] = v
end
return newTable
end