Module:RaritySort: 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 = {}


-- Utility to clean up value for processing
-- Clean and extract processing info
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, false end
     rarity = mw.text.trim(rarity)
     rarity = mw.text.trim(rarity)
     if rarity:sub(1, 1) == "~" then
     local hasTilde = rarity:sub(1, 1) == "~"
    if hasTilde then
         rarity = rarity:sub(2)
         rarity = rarity:sub(2)
     end
     end
     return rarity
     return rarity, hasTilde
end
end


-- Get numeric sort order
-- RaritySort for sorting
function p.getSortValue(frame)
function p.getSortValue(frame)
     local rarity = clean(frame.args[1])
     local rarityRaw = frame.args[1]
     if not rarity then return 99 end
    local rarity, _ = clean(rarityRaw)
     if not rarity then return 999999999 end


     local lowered = string.lower(rarity)
     local lowered = rarity:lower()


     if lowered == "1" or lowered == "always" then
     if lowered == "1" or lowered == "always" then
         return 1
         return 1
    elseif lowered:match("^1/%d+$") then
        return 2
     elseif lowered == "rare" then
     elseif lowered == "rare" then
         return 3
         return 999999
     elseif lowered == "never" then
     elseif lowered == "never" then
         return 4
         return 0
    else
        return 99
     end
     end
    -- Match 1/X and round down (floor)
    local numStr = lowered:match("^1/(%d+%.?%d*)$")
    if numStr then
        local num = tonumber(numStr)
        if num then
            return math.floor(num)
        end
    end
    return 999999999
end
end


-- Display value — keep ~ except when it's "1"
-- Normalize display text
function p.normalizeDisplay(frame)
function p.normalizeDisplay(frame)
     local original = frame.args[1]
     local original = frame.args[1]
Line 37: Line 46:


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


     return trimmed  -- includes ~ if present
     return hasTilde and ("~" .. raw) or raw
end
end


return p
return p

Revision as of 21:10, 18 May 2025

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

local p = {}

-- Clean and extract processing info
local function clean(rarity)
    if not rarity or type(rarity) ~= "string" then return nil, false end
    rarity = mw.text.trim(rarity)
    local hasTilde = rarity:sub(1, 1) == "~"
    if hasTilde then
        rarity = rarity:sub(2)
    end
    return rarity, hasTilde
end

-- RaritySort for sorting
function p.getSortValue(frame)
    local rarityRaw = frame.args[1]
    local rarity, _ = clean(rarityRaw)
    if not rarity then return 999999999 end

    local lowered = rarity:lower()

    if lowered == "1" or lowered == "always" then
        return 1
    elseif lowered == "rare" then
        return 999999
    elseif lowered == "never" then
        return 0
    end

    -- Match 1/X and round down (floor)
    local numStr = lowered:match("^1/(%d+%.?%d*)$")
    if numStr then
        local num = tonumber(numStr)
        if num then
            return math.floor(num)
        end
    end

    return 999999999
end

-- Normalize display text
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 raw = hasTilde and trimmed:sub(2) or trimmed

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

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

return p