Module:RaritySort: Difference between revisions

From HighSpell Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 2: Line 2:


function p.getSortValue(rarity)
function p.getSortValue(rarity)
    -- Handle nil or non-string input safely
     if rarity == nil or type(rarity) ~= "string" then
     if rarity == nil or type(rarity) ~= "string" then
         return 99
         return 99
     end
     end


     -- Trim whitespace and lowercase it
     local raw = rarity
     rarity = mw.text.trim(rarity)
     rarity = mw.text.trim(rarity)
     local r = mw.ustring.lower(rarity)
     local r = mw.ustring.lower(rarity)


     -- Matching known rarities
     -- Debug: remove these after testing
    if mw.title.getCurrentTitle().text == "Sandbox" then
        return "RAW: " .. tostring(raw) .. " | TRIMMED: " .. rarity
    end
 
     if r == "always" then
     if r == "always" then
         return 1
         return 1
Line 21: Line 24:
         return 4
         return 4
     else
     else
         return 99 -- unknown or custom text
         return 99
     end
     end
end
end


return p
return p

Revision as of 17:16, 15 May 2025

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

local p = {}

function p.getSortValue(rarity)
    if rarity == nil or type(rarity) ~= "string" then
        return 99
    end

    local raw = rarity
    rarity = mw.text.trim(rarity)
    local r = mw.ustring.lower(rarity)

    -- Debug: remove these after testing
    if mw.title.getCurrentTitle().text == "Sandbox" then
        return "RAW: " .. tostring(raw) .. " | TRIMMED: " .. rarity
    end

    if r == "always" then
        return 1
    elseif r:match("^1/%d+$") then
        return 2
    elseif r == "rare" then
        return 3
    elseif r == "never" then
        return 4
    else
        return 99
    end
end

return p