Module:InfoboxUtils

From nUSA Wiki Archive
Jump to navigation Jump to search

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

--- Functions for use in infobox modules.
-- @require Module:Categorization
-- @require Module:Player link
-- @require Module:Utils
-- <nowiki>
local categorization = require('Module:Categorization')
local player_link = require('Module:Player link')
local utils = require('Module:Utils')

local categorize = categorization.categorize_in_namespaces(0, 'Catalog', 'Community')

local p = {}

--- Create an error message.
-- Formerly, this used [[w:c:dev:Global Lua Modules/User error]].
-- Now it uses the built-in error function and there is no reason to use this.
function p.error_message(...)
    return error(...)
end

p.player_link = player_link.make_player_link

--- Format a number.
-- @param {number} number Number to format.
-- @param {number} max_round Magnitude for rounding.
-- @param {string} category Category to add, optional.
-- @param {number} category_min If present, category is added only when number is less than this value.
-- @param {string} filter_flag Flag to add when less than minimum (for [[Special:AbuseFilter/5]]).
-- @return {string} Formatted number.
function p.format_number(
        number, max_round, category, category_min, filter_flag)
    number = utils.parse_number(number, true) -- Guaranteed to return a number or error
    if max_round then
        -- Round the number.
        local power = 10^(#tostring(number) - 1)
        power = power > max_round and max_round or power
        number = math.ceil(number/power) * power
    end
    local formatted_number = utils.format_number(number)
    if category then
        if category_min then
            if number < category_min then
                if filter_flag then
                    filter_flag = tostring(mw.html.create("span")
                        :wikitext("FLAG::" .. filter_flag)
                        :css("display", "none")
                    )
                end
                return formatted_number .. categorize(category) .. (filter_flag or '')
            else
                return formatted_number
            end
        else
            return formatted_number .. categorize(category)
        end
    else
        return formatted_number
    end
end

return p