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
Line 1: Line 1:
local p = {}
local p = {}


-- Get numeric sort value
-- Internal: cleans rarity string for logic
function p.getSortValue(frame)
local function clean(rarity)
    local rarity = frame.args[1]
     if not rarity or type(rarity) ~= "string" then return nil end
 
     if rarity == nil or type(rarity) ~= "string" then
        return 99
    end
 
     rarity = mw.text.trim(rarity)
     rarity = mw.text.trim(rarity)
     if rarity:sub(1, 1) == "~" then
     if rarity:sub(1, 1) == "~" then
         rarity = rarity:sub(2)
         rarity = rarity:sub(2)
     end
     end
    return rarity
end
-- Get numeric sort value
function p.getSortValue(frame)
    local rarity = clean(frame.args[1])
    if not rarity then return 99 end


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


     if lowered == "1" then
     if lowered == "1" or lowered == "always" then
        lowered = "always"
    end
 
    if lowered == "always" then
         return 1
         return 1
     elseif lowered:match("^1/%d+$") then
     elseif lowered:match("^1/%d+$") then
Line 33: Line 31:
end
end


-- Return cleaned display value
-- Display with ~ if originally present, except for "1" → "Always"
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
        return "Unknown"
    end


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


     if rarity == "1" then
     if cleaned == "1" then
         return "Always"
         return "Always"
     end
     end


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


return p
return p