Module:RarityHandler: Difference between revisions

No edit summary
No edit summary
Line 15: Line 15:


function p.getSortValue(frame)
function p.getSortValue(frame)
     local rarity = frame.args[1]
     local raw = frame.args[1]
     if not rarity or type(rarity) ~= "string" then
     if not raw or type(raw) ~= "string" then
         return 999999999
         return 999999999
     end
     end


     -- Trim whitespace and strip leading ~
     local rarity = clean(raw)
    rarity = mw.text.trim(rarity)
     if not rarity then return 999999999 end
     if rarity:sub(1, 1) == "~" then
        rarity = rarity:sub(2)
    end


    -- Normalize lowercase
     local lowered = rarity:lower()
     local lowered = rarity:lower()


Line 38: Line 34:
     end
     end


     -- Match and convert fractional chance like "1/2.5", "1/128"
     -- Match and convert fractional chance like "1/2.5", "1/10000"
     local num = lowered:match("^1/(%d+%.?%d*)$")
     local num = lowered:match("^1/(%d+%.?%d*)$")
     if num then
     if num then
         local n = tonumber(num)
         local n = tonumber(num)
         if n then
         if n then
             return n
             return math.floor(n)
         end
         end
     end
     end


    -- Unknown or badly formatted
     return 999999999
     return 999999999
end
end