Marvel Database
Register
Advertisement

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

--Replaces the ineficient {{#switch }} parser function
-- Demo module
--Syntax - {{#invoke:switch|main|option|caseX=3|abc=z}} where option is any string to match and caseX any key-value pairs
--Syntax2  {{#invoke:switch|main|2|abc|bcd|222}} -> bcd
-- local frame = {args = { '1',['#default']=8, 5 },getParent = function () local pArgs = {'1',['src']=8, 5 }  return { args = pArgs } end } mw.log('')
local p = {}
local u = require('Dev:Utility')
 
function p.main(frame)
    local tArgLang = u.getArgs(frame)
    return p.switch(tArgLang)
end
 
function p.switch(tArgs)
    if tArgs then
        if tArgs[1] and tArgs[1]~="" then
            local sCase = (tArgs[1].."" or "")
            sCase = mw.text.trim(sCase)
            if tonumber(sCase) then
                sCase = tonumber(sCase)+1
            end
            if not(tArgs[sCase])then
                sCase = "#default"
                if  tonumber(sCase) and sCase<2   then
                    return ""
                end
            end
            return mw.getCurrentFrame():preprocess(tArgs[sCase] or "")
        else 
            if (tArgs["#default"]) then
                return tArgs["#default"] or ""
            end
        end
    end
end   
 
return p
Advertisement