Module:Parameters/remove holes: Difference between revisions

From Thadar En
Jump to navigation Jump to search
en>Surjection
m (Protected "Module:parameters/remove holes" ([Edit=Allow only template editors and administrators] (indefinite) [Move=Allow only template editors and administrators] (indefinite)))
 
m (1 revision imported)
 
(No difference)

Latest revision as of 22:25, 5 September 2022

Documentation for this module may be created at Module:Parameters/remove holes/doc

-- A helper function that removes empty numeric indexes in a table,
-- so that the values are tightly packed like in a normal Lua table.
-- equivalent to require("Module:table").compressSparseArray
return function (t)
	local ret = {}
	local index = 1
	local highest = 0
	for num, _ in pairs(t) do
		if type(num) == "number" and num > 0 and num < math.huge and math.floor(num) == num then
			highest = math.max(highest, num)
		end
	end
	for i = 1, highest do
		if t[i] then
			ret[index] = t[i]
			index = index + 1
		end
	end
	return ret
end