Jump to content

Module:Includes: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
catch missing array
Use unpack instead of custom iterator
 
Line 9: Line 9:
end
end
if fromIndex < 1 then fromIndex = 1 end
if fromIndex < 1 then fromIndex = 1 end
local iter, a = ipairs(array)
for _, v in ipairs({unpack(array, fromIndex)}) do
for _, v in iter, a, (fromIndex - 1) do
if v == searchElement then
if v == searchElement then
return true
return true

Latest revision as of 20:58, 13 August 2024

-- Equivalent to JavaScript array.includes(searchElement) or
-- array.includes(searchElement, fromIndex), except fromIndex is 1 indexed
return function (array, searchElement, fromIndex)
	if type(array) ~= 'table' then return false end
	fromIndex = tonumber(fromIndex)
	if fromIndex then
		if (fromIndex < 0) then
			fromIndex = #array + fromIndex + 1
		end
		if fromIndex < 1 then fromIndex = 1 end
		for _, v in ipairs({unpack(array, fromIndex)}) do
			if v == searchElement then
				return true
			end
		end
	else
		for _, v in pairs(array) do
			if v == searchElement then
				return true
			end
		end
	end
	return false
end