Module:table/shallowCopy
Jump to navigation
Jump to search
Documentation for this module may be created at Module:table/shallowCopy/doc
local next = next
local pairs = pairs
local type = type
--[==[
Returns a clone of an object. If the object is a table, the value returned is a new table, but all subtables and functions are shared. Metamethods are respected unless the `raw` flag is set, but the returned table will have no metatable of its own.]==]
return function(orig, raw)
if type(orig) ~= "table" then
return orig
end
local copy, iter, state, init = {}
if raw then
iter, state = next, orig
else
iter, state, init = pairs(orig)
end
for k, v in iter, state, init do
copy[k] = v
end
return copy
end