Module:table/shallowCopy
Itsura
- This module lacks a documentation subpage. Please create it.
- Useful links: root page • root page’s subpages • links • transclusions • testcases • sandbox
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