Module:RaritySort

Revision as of 17:55, 15 May 2025 by Ryan (talk | contribs)

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

local p = {}

-- Utility to clean up value for processing
local function clean(rarity)
    if not rarity or type(rarity) ~= "string" then return nil end
    rarity = mw.text.trim(rarity)
    if rarity:sub(1, 1) == "~" then
        rarity = rarity:sub(2)
    end
    return rarity
end

-- Get numeric sort order
function p.getSortValue(frame)
    local rarity = clean(frame.args[1])
    if not rarity then return 99 end

    local lowered = string.lower(rarity)

    if lowered == "1" or lowered == "always" then
        return 1
    elseif lowered:match("^1/%d+$") then
        return 2
    elseif lowered == "rare" then
        return 3
    elseif lowered == "never" then
        return 4
    else
        return 99
    end
end

-- Display value — keep ~ except when it's "1"
function p.normalizeDisplay(frame)
    local original = frame.args[1]
    if not original or type(original) ~= "string" then return "Unknown" end

    local trimmed = mw.text.trim(original)
    local raw = trimmed:gsub("^~", "")  -- remove ~ only for checking
    if raw == "1" then
        return "Always"
    end

    return trimmed  -- includes ~ if present
end

return p