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 = {} | ||
-- | -- Internal: cleans rarity string for logic | ||
function | local function clean(rarity) | ||
if not rarity or type(rarity) ~= "string" then return nil end | |||
if rarity | |||
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" | if lowered == "1" or 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 | ||
-- | -- Display with ~ if originally present, except for "1" → "Always" | ||
function p.normalizeDisplay(frame) | function p.normalizeDisplay(frame) | ||
local | local original = frame.args[1] | ||
if not original or type(original) ~= "string" then return "Unknown" end | |||
if not | |||
local trimmed = mw.text.trim(original) | |||
local hasTilde = trimmed:sub(1, 1) == "~" | |||
local cleaned = hasTilde and trimmed:sub(2) or trimmed | |||
if | if cleaned == "1" then | ||
return "Always" | return "Always" | ||
end | end | ||
return | return hasTilde and ("~" .. cleaned) or cleaned | ||
end | end | ||
return p | return p |