Module:If match

From Coral Island Wiki
Jump to navigation Jump to search

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

local p = {}
local lib = require('Module:Feature')

function p.main(frame)
	local args = require('Module:Arguments').getArgs(frame)
	if lib.isEmpty(args[1]) then return '' end
	
	return p._main(args)
end

function p._main(args)
	local str = tostring(args[1])
	local truthy = args['true'] or ''
	local falsey = args['false'] or lib.ternary(lib.isNotEmpty(args['index']), 0, '')
	
	for I,regex in ipairs(args) do
		if I == 1 then
		elseif args[I] and string.match(str, tostring(args[I])) then
			
			if lib.isNotEmpty(args['true' .. tostring(I-1)]) then
				return args['true' .. tostring(I-1)]
			elseif lib.isNotEmpty(args['index']) then
				return I-1
			else
				return truthy
			end
			
		end
	end
	
	return falsey
end

return p