Module:RequirementDebug

From Fallen London Wiki

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

local p = {}

local str = require("Module:String")

local function _matchwhy(value,requirement)
	if value == '0' then
		return 'no value'
	end
	if requirement == '' or requirement == nil then
		return 'no requirement'
	end
	local exactvalue = string.match(requirement, '^exactly (%d+)$')
	if exactvalue then
		return value == exactvalue
	end
	local rangemin, rangemax = string.match(requirement, '^(%d+)%s*%-%s*(%d+)$')
	if rangemin and rangemax then
		return rangemin <= value and value <= rangemax
	end
	local minvalue = string.match(requirement, '^%d+$')
	if minvalue then
		return value >= minvalue
	end
	-- escape any regex special characters in the value, since it will be interpreted as regex pattern
	local escapedvalue = str._escapePattern(value)
	local stringmatch = string.match(requirement, escapedvalue)
	if stringmatch then
		return value .. ' matches ' .. requirement
	else
		return value .. ' does not match ' .. requirement
	end
end

local function _match(value,requirement)
	if value == '0' then
		return false
	end
	if requirement == '' or requirement == nil then
		return true
	end
	local exactvalue = string.match(requirement, '^exactly (%d+)$')
	if exactvalue then
		return value == exactvalue
	end
	local rangemin, rangemax = string.match(requirement, '^(%d+)%s*%-%s*(%d+)$')
	if rangemin and rangemax then
		return rangemin <= value and value <= rangemax
	end
	local minvalue = string.match(requirement, '^%d+$')
	if minvalue then
		return value >= minvalue
	end
	-- escape any regex special characters in the value, since it will be interpreted as regex pattern
	local escapedvalue = str._escapePattern(value)
	local stringmatch = string.match(requirement, escapedvalue)
	if stringmatch then
		return true
	else
		return false
	end
end

function p.match(frame)
	local value = frame.args[1] or frame.args['value']
	local requirement = frame.args[2] or frame.args['requirement']
	
	return _match(value,requirement) or ''
end

function p.matchwhy(frame)
	local value = frame.args[1] or frame.args['value']
	local requirement = frame.args[2] or frame.args['requirement']
	
	return _matchwhy(value,requirement)
end

return p