Fallout中文維基
Advertisement

此模块的文档可以在模块:Validation/doc创建

local p = {};

function present(field, comp)
    return field ~= nil
end

function absent(field, comp)
    return field == nil
end

function equals(field, comp)
    return field ~= nil and field:lower() == comp:lower()
end

function contains(field, comp)
    return field ~= nil and string.find(field:lower(), comp:lower()) ~= nil
end

function check(field, comp, func, ret)
    if func(field, comp) then
        return ret .. " "
    else
        return ""
    end
end

function handleErrors(funcName, func, comp, checkRet)
    missingComp = ((funcName == "equals") or (funcName == "contains")) and (comp == nil)
    if (missingComp) or (func == nil) or (checkRet == nil) then
        error("Invalid args passed to validateFields. Check [[Module:Validation/doc]] for proper usage.", 0)
    end
end

-- This function will insert text into an infobox based on the properties of a field.
-- Currently, this function can only look at one field for a given text insertion,
-- but it is possible to check multiple fields and insert text separately for each one.
function p.validateFields(frame)
    functions = {["present"]=present, ["absent"]=absent, ["equals"]=equals, ["contains"]=contains}
    infoboxArgs = frame:getParent().args
    args = frame.args
    i = 1
    ret = ""

    while (args[i] ~= nil) do
        inc = 3
        field = infoboxArgs[args[i + 1]]
        comp = nil
        func = functions[args[i]]
        checkRet = args[i + 2]

        if (args[i] == "equals") or (args[i] == "contains") then
            inc = 4
            comp = args[i + 2]
            checkRet = args[i + 3]
        end

        errRet, errMsg = pcall(handleErrors, args[i], func, comp, checkRet)
        if (not errRet) then
            return errMsg
        end

        ret = ret .. check(field, comp, func, checkRet)
        i = i + inc
    end

    return ret
end

return p
Advertisement