Module:Util/tables/concat

From Zelda Wiki, the Zelda encyclopedia
Jump to navigation Jump to search

Lua error in package.lua at line 80: module 'Module:Util/tbl/clone' not found.


local clone = require("Module:Util/tbl/clone")

local function concat(array, ...)
	if type(array) ~= "table" then
		array = {array}
	end
	local result = clone(array)
	for i, arrayOrValue in ipairs({...}) do
		if hasArray(arrayOrValue) then
			for _, value in ipairs(arrayOrValue) do
				table.insert(result, value)
			end
		else
			table.insert(result, arrayOrValue)
		end
	end
	return result
end

function hasArray(tbl)
	if type(tbl) ~= "table" then
		return false
	end
	-- An empty table {} should be considered an array, sence the size == 0 check
	for i in ipairs(tbl) do
		return true -- has integer keys starting from 1, therefore hasArray = true
	end
	for k in pairs(tbl) do
		return false -- has no integer keys but has non-integer keys, therefore hasArray = false
	end
	return true -- has no keys at all and is therefore an "empty" array, therefore hasArray = true
end

return concat