Module:Sandbox/User:Waddie96
Jump to navigation
Jump to search
Run benchmarks: Module:Sandbox/User:Waddie96/Benchmarks
- Sandbox/User:Waddie96/Table
- Sandbox/User:Waddie96/benchmark
- Sandbox/User:Waddie96/benchmark/doc
- Sandbox/User:Waddie96/doc
- Sandbox/User:Waddie96/protection
- Sandbox/User:Waddie96/styles.css
- Sandbox/User:Waddie96/table
- Sandbox/User:Waddie96/table.css
- Sandbox/User:Waddie96/table/doc
- Sandbox/User:Waddie96/table/styles.css
- Sandbox/User:Waddie96/testcases
Usage
[edit source]{{#invoke:Sandbox/User:Waddie96|function_name}}
local p = {}
-- Mapping of string inputs to boolean values
local STR_BOOL = {
["yes"] = true, ["y"] = true, ["true"] = true, ["t"] = true, ["on"] = true, ["1"] = true,
["no"] = false, ["n"] = false, ["false"] = false, ["f"] = false, ["off"] = false, ["0"] = false
}
-- Converts numeric strings or numbers to boolean
local function num(s)
local n = tonumber(s)
if n == nil then return nil end
return n ~= 0
end
-- Main function
---@param input any Input value to evaluate
---@param default any Default to return if input unrecognized
---@return boolean|any
function p.main(frame)
local input = frame.args[1]
local default = frame.args[2]
-- Convert input to string for consistent lookup
local s = tostring(input)
local lower = string.lower(s)
-- Lookup string in STR_BOOL table
if STR_BOOL[lower] ~= nil then
return STR_BOOL[lower]
end
-- Try numeric conversion
local nval = num(s)
if nval ~= nil then
return nval
end
-- Fallback
return default
end
return p