Module:Yesno: Difference between revisions
Appearance
Undid revision 948472533 by [[Special:Contributions/w>Vogone|w>Vogone]] ([[User talk:w>Vogone|talk]]) |
No edit summary |
||
| (One intermediate revision by the same user not shown) | |||
| Line 1: | Line 1: | ||
local p = {} | |||
-- Core yes/no logic | |||
local function _yesno(val, default) | |||
val = type(val) == 'string' and val:lower() or val | val = type(val) == 'string' and val:lower() or val | ||
if val == nil then | if val == nil then | ||
| Line 31: | Line 28: | ||
end | end | ||
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 | |||
Latest revision as of 08:39, 25 January 2026
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