Still landing on the old Fandom wiki? Install the HighSpell Wiki Redirector browser extension to automatically redirect all highspell.fandom.com links to highspell.wiki. Works on both Firefox and Chrome, and is open-source. → Learn more and install it now!

Module:RarityHandler

From HighSpell Wiki
Revision as of 17:53, 15 May 2025 by Ryan (talk | contribs) (Created page with "local p = {} -- Get numeric sort value function p.getSortValue(frame) local rarity = frame.args[1] if rarity == nil or type(rarity) ~= "string" then return 99 end rarity = mw.text.trim(rarity) if rarity:sub(1, 1) == "~" then rarity = rarity:sub(2) end local lowered = string.lower(rarity) if lowered == "1" then lowered = "always" end if lowered == "always" then return 1 elseif lowered:match(...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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

local p = {}

-- Get numeric sort value
function p.getSortValue(frame)
    local rarity = frame.args[1]

    if rarity == nil or type(rarity) ~= "string" then
        return 99
    end

    rarity = mw.text.trim(rarity)
    if rarity:sub(1, 1) == "~" then
        rarity = rarity:sub(2)
    end

    local lowered = string.lower(rarity)

    if lowered == "1" then
        lowered = "always"
    end

    if 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

-- Return cleaned display value
function p.normalizeDisplay(frame)
    local rarity = frame.args[1]

    if not rarity or type(rarity) ~= "string" then
        return "Unknown"
    end

    rarity = mw.text.trim(rarity)
    if rarity:sub(1, 1) == "~" then
        rarity = rarity:sub(2)
    end

    if rarity == "1" then
        return "Always"
    end

    return rarity
end

return p