Module:RaritySort: Difference between revisions

From HighSpell Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
 
(8 intermediate revisions by the same user not shown)
Line 1: Line 1:
local p = {}
local p = {}


function p.getSortValue(rarity)
-- Clean and extract processing info
     if rarity == nil or type(rarity) ~= "string" then
local function clean(rarity)
         return 99
     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
     end
    -- Strip commas (e.g. "1/10,000" → "1/10000")
    rarity = rarity:gsub(",", "")
    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()


     local raw = rarity
     if lowered == "1" or lowered == "always" then
     rarity = mw.text.trim(rarity)
        return 1
     local r = mw.ustring.lower(rarity)
     elseif lowered == "rare" then
        return 999999
     elseif lowered == "never" then
        return 0
    end


     -- Debug: remove these after testing
     -- Match 1/X and round down (floor)
     if mw.title.getCurrentTitle().text == "Sandbox" then
    local numStr = lowered:match("^1/(%d+%.?%d*)$")
        return "RAW: " .. tostring(raw) .. " | TRIMMED: " .. rarity
     if numStr then
        local num = tonumber(numStr)
        if num then
            return math.floor(num)
        end
     end
     end


     if r == "always" then
    return 999999999
        return 1
end
     elseif r:match("^1/%d+$") then
 
        return 2
-- Normalize display text
    elseif r == "rare" then
function p.normalizeDisplay(frame)
        return 3
    local original = frame.args[1]
     elseif r == "never" then
     if not original or type(original) ~= "string" then return "Unknown" end
         return 4
 
    else
     local trimmed = mw.text.trim(original)
        return 99
    local hasTilde = trimmed:sub(1, 1) == "~"
    local raw = hasTilde and trimmed:sub(2) or trimmed
 
     if raw == "1" then
         return "Always"
     end
     end
    return hasTilde and ("~" .. raw) or raw
end
end


return p
return p

Latest revision as of 21:22, 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
    -- Strip commas (e.g. "1/10,000" → "1/10000")
    rarity = rarity:gsub(",", "")
    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