Module:Yesno and Module:Yesno/sandbox: Difference between pages

(Difference between pages)
Jump to navigation Jump to search
Page 1
Page 2
imported>MusikAnimal
Undid revision 948472533 by [[Special:Contributions/w>Vogone|w>Vogone]] ([[User talk:w>Vogone|talk]])
 
imported>Waddie96
f
 
Line 1: Line 1:
-- Function allowing for consistent treatment of boolean-like wikitext input.
--- Allows for consistent treatment of boolean-like wikitext input.
-- It works similarly to the template {{yesno}}.
--- Uses lookup table, instead of if-elseif-else statement, for efficiency.
--
-- If your wiki uses non-ASCII characters for any of "yes", "no", etc., you
-- should replace "string.lower" with "mw.ustring.lower" in the
-- following line. NOTE: It is _much_ slower.
local LOWER = string.lower
local TO_NUMBER = tonumber
local TYPE = type
local BOOLEAN_MAP = {
    yes = true, y = true, ['true'] = true, t = true, on = true, ['1'] = true,
    no = false, n = false, ['false'] = false, f = false, off = false, ['0'] = false
}
return function (value, defaultResponse)
    if value == nil then
        return nil
    end


return function (val, default)
    local valueType = TYPE(value)
-- If your wiki uses non-ascii characters for any of "yes", "no", etc., you
 
-- should replace "val:lower()" with "mw.ustring.lower(val)" in the
    if valueType == 'boolean' then
-- following line.
        return value
val = type(val) == 'string' and val:lower() or val
    elseif valueType == 'string' then
if val == nil then
        local lookupResult = BOOLEAN_MAP[LOWER(value)]
return nil
        if lookupResult ~= nil then
elseif val == true
            return lookupResult
or val == 'yes'
        end
or val == 'y'
    end
or val == 'true'
 
or val == 't'
    -- Numeric check works for both numbers and numeric strings.
or val == 'on'
    local number = TO_NUMBER(value)
or tonumber(val) == 1
    if number == 1 then
then
        return true
return true
    elseif number == 0 then
elseif val == false
        return false
or val == 'no'
    end
or val == 'n'
 
or val == 'false'
    return defaultResponse
or val == 'f'
or val == 'off'
or tonumber(val) == 0
then
return false
else
return default
end
end
end