Jump to content

Module:Yesno

From Cameroon Heritage Encyclopedia

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

local p = {}

-- Core yes/no logic
local function _yesno(val, default)
	val = type(val) == 'string' and val:lower() or val
	if val == nil then
		return nil
	elseif val == true 
		or val == 'yes'
		or val == 'y'
		or val == 'true'
		or val == 't'
		or val == 'on'
		or tonumber(val) == 1
	then
		return true
	elseif val == false
		or val == 'no'
		or val == 'n'
		or val == 'false'
		or val == 'f'
		or val == 'off'
		or tonumber(val) == 0
	then
		return false
	else
		return default
	end
end

-- Wikipedia-style API
function p.yesno(val, default)
	return _yesno(val, default)
end

-- Allow direct calls: require(...)(val)
setmetatable(p, {
	__call = function(_, val, default)
		return _yesno(val, default)
	end
})

return p