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(..."
 
Undo revision 4483 by Ryan (talk)
Tag: Undo
 
(13 intermediate revisions by the same user not shown)
Line 1: Line 1:
local p = {}
local p = {}


-- Get numeric sort value
-- Clean up the rarity string: strip ~, commas, trim
local function clean(rarity)
if not rarity or type(rarity) ~= "string" then return nil end
rarity = mw.text.trim(rarity)
rarity = rarity:gsub("^~", "") -- remove ~
rarity = rarity:gsub(",", "") -- remove commas
return rarity
end
 
function p.getSortValue(frame)
function p.getSortValue(frame)
    local rarity = frame.args[1]
local raw = frame.args[1]
if not raw or type(raw) ~= "string" then return 999999999 end


    if rarity == nil or type(rarity) ~= "string" then
local rarity = clean(raw)
        return 99
if not rarity then return 999999999 end
    end


    rarity = mw.text.trim(rarity)
local lowered = rarity:lower()
    if rarity:sub(1, 1) == "~" then
        rarity = rarity:sub(2)
    end


    local lowered = string.lower(rarity)
if lowered == "1" or lowered == "always" then
return 1
elseif lowered == "rare" then
return 999999998
elseif lowered == "never" then
return 0
end


    if lowered == "1" then
-- Match 1/X, return raw number (not string)
        lowered = "always"
local numStr = lowered:match("^1/(%d+%.?%d*)$")
    end
if numStr then
local num = tonumber(numStr)
if num then return math.floor(num) end
end


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


-- Return cleaned display value
-- For logic styling
function p.getCleanValue(frame)
local rarity = clean(frame.args[1])
if not rarity then return "Unknown" end
if rarity == "1" then return "Always" end
return rarity
end
 
-- 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 return "Always" end
    if rarity:sub(1, 1) == "~" then
        rarity = rarity:sub(2)
    end


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


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


return p
return p