Module:table/shallowCopy

From Linguifex
< Module:table
Revision as of 21:37, 4 November 2025 by Sware (talk | contribs) (Created page with "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...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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