Edge Rewrite
// HTMLRewriter · presentation

This page was redesigned at the edge.

Cloudflare fetched the original article and streamed it through HTMLRewriter to apply an entirely new visual system without rebuilding the source page.

Jump to content

Module:Sandbox/User:Waddie96

From Wikipedia, the free encyclopedia

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