Modul:table/shallowCopy
Penampilan
- Laman modul ini kekurangan sublaman pendokumenan. Sila cipta laman pendokumenan tersebut.
- Pautan berguna: akar laman • sublaman akar laman • pautan • transklusi • kes ujian • kotak pasir
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