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: Difference between revisions

From HighSpell Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
local p = {}
local p = {}


-- Internal: cleans rarity string for logic
-- Clean: trim and strip leading ~
local function clean(rarity)
local function clean(rarity)
     if not rarity or type(rarity) ~= "string" then return nil end
     if not rarity or type(rarity) ~= "string" then return nil end
Line 11: Line 11:
end
end


-- Get numeric sort value
function p.getSortValue(frame)
function p.getSortValue(frame)
     local rarity = clean(frame.args[1])
     local rarity = clean(frame.args[1])
Line 31: Line 30:
end
end


-- Display with ~ if originally present, except for "1" → "Always"
-- For class logic: cleaned string, lowercase
function p.getCleanValue(frame)
    local rarity = clean(frame.args[1])
    return rarity or "Unknown"
end
 
-- For display
function p.normalizeDisplay(frame)
function p.normalizeDisplay(frame)
     local original = frame.args[1]
     local original = frame.args[1]

Revision as of 17:57, 15 May 2025

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

local p = {}

-- Clean: trim and strip leading ~
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

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

-- For class logic: cleaned string, lowercase
function p.getCleanValue(frame)
    local rarity = clean(frame.args[1])
    return rarity or "Unknown"
end

-- For display
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 hasTilde = trimmed:sub(1, 1) == "~"
    local cleaned = hasTilde and trimmed:sub(2) or trimmed

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

    return hasTilde and ("~" .. cleaned) or cleaned
end

return p