Module:RarityHandler: Difference between revisions

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(..."
 
No edit summary
(8 intermediate revisions by the same user not shown)
Line 1: Line 1:
local p = {}
local p = {}


-- Get numeric sort value
-- 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)
    -- Strip leading ~
    if rarity:sub(1, 1) == "~" then
        rarity = rarity:sub(2)
    end
    -- Strip commas
    rarity = rarity:gsub(",", "")
    return rarity
end
 
function p.getSortValue(frame)
function p.getSortValue(frame)
     local rarity = frame.args[1]
     local rarity = frame.args[1]
 
     if not rarity or type(rarity) ~= "string" then
     if rarity == nil or type(rarity) ~= "string" then
         return 999999999
         return 99
     end
     end


    -- Trim whitespace and strip leading ~
     rarity = mw.text.trim(rarity)
     rarity = mw.text.trim(rarity)
     if rarity:sub(1, 1) == "~" then
     if rarity:sub(1, 1) == "~" then
Line 14: Line 26:
     end
     end


     local lowered = string.lower(rarity)
    -- Normalize lowercase
 
     local lowered = rarity:lower()
    if lowered == "1" then
        lowered = "always"
    end


     if lowered == "always" then
    -- Handle special values
     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 999999998
     elseif lowered == "never" then
     elseif lowered == "never" then
         return 4
         return 0
     else
     end
         return 99
 
    -- Match and convert fractional chance like "1/2.5", "1/128"
    local num = lowered:match("^1/(%d+%.?%d*)$")
    if num then
        local n = tonumber(num)
        if n then
            return n
        end
    end
 
    -- Unknown or badly formatted
    return 999999999
end
 
-- For class logic: cleaned string, lowercase
function p.getCleanValue(frame)
    local rarity = clean(frame.args[1])
    if not rarity then return "Unknown" end
 
    if rarity == "1" then
         return "Always"
     end
     end
    return rarity
end
end


-- Return cleaned display value
-- For display
function p.normalizeDisplay(frame)
function p.normalizeDisplay(frame)
     local rarity = frame.args[1]
     local original = frame.args[1]
    if not original or type(original) ~= "string" then return "Unknown" end


     if not rarity or type(rarity) ~= "string" then
     local trimmed = mw.text.trim(original)
        return "Unknown"
    local hasTilde = trimmed:sub(1, 1) == "~"
     end
     local cleaned = hasTilde and trimmed:sub(2) or trimmed


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


     if rarity == "1" then
     -- Format numbers like 1/10000 → 1/10,000
         return "Always"
    local prefix, num = cleaned:match("^(1/)(%d+%.?%d*)$")
    if prefix and num then
        local withCommas = mw.language.getContentLanguage():formatNum(tonumber(num))
         return (hasTilde and "~" or "") .. prefix .. withCommas
     end
     end


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


return p
return p