dev
Documentation icon Module documentation
[create]

The documentation for this module is missing. Click here to create it.

--Replaces the inefficient {{#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 = {}

function p.main(frame)
    local tArgLang = frame.args
    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 "")
        elseif (tArgs["#default"]) then
            return tArgs["#default"]
        else
            return ""
        end
    end
end   

return p