Module:CheckTypeMulti

From The Satanic Wiki
Revision as of 04:36, 2 May 2021 by WikiGOD (talk | contribs) (Created page with "-- <nowiki> -------------------------------------------------------------------------------- -- Like libraryUtil.checkType, but accepts multiple types. -- -- @source <http://g...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Documentation for this module may be created at Module:CheckTypeMulti/doc

-- <nowiki>
--------------------------------------------------------------------------------
-- Like libraryUtil.checkType, but accepts multiple types.
--
-- @source <http://git.io/vtPKB>
--------------------------------------------------------------------------------
return function ( name, argIdx, arg, expectTypes )
	local argType = type( arg )
	for _, expectType in ipairs( expectTypes ) do
		if argType == expectType then
			return
		end
	end
	local n = #expectTypes
	local typeList
	if n > 1 then
		typeList = table.concat( expectTypes, ', ', 1, n - 1 ) .. ' or ' .. expectTypes[n]
	else
		typeList = expectTypes[1]
	end
	local msg = string.format( "bad argument #%d to '%s' (%s expected, got %s)",
		argIdx,
		name,
		typeList,
		type( arg )
	)
	error( msg, 3 )
end

-- </nowiki>
-- (Add categories here.)