Difference between revisions of "Module:Used by/Sandbox"

From Team Fortress Wiki
Jump to: navigation, search
 
Line 26: Line 26:
 
     elseif args[1] == 'all-except' then
 
     elseif args[1] == 'all-except' then
 
         table.remove(args, 1) -- Remove the 'all-except' argument
 
         table.remove(args, 1) -- Remove the 'all-except' argument
 +
        assert(#args >= 1, "You must provide at least 1 class when using 'all-except'.")
 +
        assert(#args <= 2, "You must provide at most 2 classes when using 'all-except'.")
 
         return common_string{'infobox-used-by-all-except-' .. #args, unpack(args)}
 
         return common_string{'infobox-used-by-all-except-' .. #args, unpack(args)}
 
     else
 
     else
 +
        assert(#args >= 1, "You must provide at least 1 class when using this function.")
 +
        assert(#args <= 5, "You must provide at most 5 classes when using this function.")
 
         return common_string{'infobox-used-by-' .. #args, unpack(args)}
 
         return common_string{'infobox-used-by-' .. #args, unpack(args)}
 
     end
 
     end

Latest revision as of 06:35, 18 March 2023

Documentation for this module may be created at Module:Used by/Sandbox/doc

local p = {}

function common_string(args)
  return mw.getCurrentFrame():expandTemplate{title='Common string', args=args}
end

function get_args(frame)
    local args = {}
    for _, arg in ipairs(frame.args) do
        table.insert(args, arg)
    end
    if #args > 0 then return args end

    for _, arg in ipairs(frame:getParent().args) do
        table.insert(args, arg)
    end

    return args
end

function p.classes(frame)
    local args = get_args(frame)

    if args[1] == 'all' then
        return common_string{'infobox-used-by-all'}
    elseif args[1] == 'all-except' then
        table.remove(args, 1) -- Remove the 'all-except' argument
        assert(#args >= 1, "You must provide at least 1 class when using 'all-except'.")
        assert(#args <= 2, "You must provide at most 2 classes when using 'all-except'.")
        return common_string{'infobox-used-by-all-except-' .. #args, unpack(args)}
    else
        assert(#args >= 1, "You must provide at least 1 class when using this function.")
        assert(#args <= 5, "You must provide at most 5 classes when using this function.")
        return common_string{'infobox-used-by-' .. #args, unpack(args)}
    end
end

return p