Module:CheckTypeMulti: Difference between revisions

From The Satanic Wiki
Jump to navigation Jump to search
Created page with "-- <nowiki> -------------------------------------------------------------------------------- -- Like libraryUtil.checkType, but accepts multiple types. -- -- @source <http://g..."
 
(No difference)

Latest revision as of 04:36, 2 May 2021

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.)