Jump to content

Module:yesno

ពីWiktionary

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

-- Function allowing for consistent treatment of boolean-like wikitext input.
-- It works similarly to the template {{yesno}}.
local lower = string.lower
local type = type
local yesno

return function (val, default)
	if val == nil then
		return nil
	elseif not yesno then
		yesno = {
			[true] = true,		[false] = false,
			["true"] = true,	["false"] = false,
			["t"] = true,		["f"] = false,
			[1] = true,			[0] = false,
			["1"] = true,		["0"] = false,
			["yes"] = true,		["no"] = false,
			["y"] = true,		["n"] = false,
			["on"] = true,		["off"] = false,
		}
	end
	local ret = yesno[val]
	if ret ~= nil then
		return ret
	elseif type(val) ~= "string" then
		return default
	end
	ret = yesno[lower(val)]
	if ret ~= nil then
		return ret
	end
	return default
end