Module:RarityHandler: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
local p = {} | local p = {} | ||
-- Clean: trim | -- Clean: trim, strip ~, remove commas | ||
local function clean(rarity) | local function clean(rarity) | ||
if not rarity or type(rarity) ~= "string" then return nil end | |||
rarity = mw.text.trim(rarity) | |||
if rarity:sub(1, 1) == "~" then | |||
rarity = rarity:sub(2) | |||
end | |||
rarity = rarity:gsub(",", "") -- strip commas | |||
return rarity | |||
end | end | ||
-- RaritySort (raw integer value) | |||
function p.getSortValue(frame) | function p.getSortValue(frame) | ||
local raw = frame.args[1] | |||
local rarity = clean(raw) | |||
if not rarity then return 999999999 end | |||
local lowered = rarity:lower() | |||
if lowered == "1" or lowered == "always" then | |||
return 1 | |||
elseif lowered == "rare" then | |||
return 999999998 | |||
elseif lowered == "never" then | |||
return 0 | |||
end | |||
local num = lowered:match("^1/(%d+%.?%d*)$") | |||
if num then | |||
local n = tonumber(num) | |||
if n then | |||
return math.floor(n) | |||
end | |||
end | |||
return 999999999 | |||
end | end | ||
-- | -- Used for bg-class logic | ||
function p.getCleanValue(frame) | 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 | end | ||
-- For display | -- For display (add ~ and formatted commas back) | ||
function p.normalizeDisplay(frame) | function p.normalizeDisplay(frame) | ||
local original = frame.args[1] | |||
if not original or type(original) ~= "string" then return "Unknown" end | |||
local trimmed = mw.text.trim(original) | |||
local hasTilde = trimmed:sub(1, 1) == "~" | |||
local cleaned = hasTilde and trimmed:sub(2) or trimmed | |||
if cleaned == "1" then return "Always" end | |||
-- Format 1/#### with commas | |||
local prefix, num = cleaned:match("^(1/)(%d+%.?%d*)$") | |||
if prefix and num then | |||
local formatted = mw.language.getContentLanguage():formatNum(tonumber(num)) | |||
return (hasTilde and "~" or "") .. prefix .. formatted | |||
end | |||
return hasTilde and ("~" .. cleaned) or cleaned | |||
end | end | ||
return p | return p |
Revision as of 13:57, 19 May 2025
Documentation for this module may be created at Module:RarityHandler/doc
local p = {}
-- Clean: trim, strip ~, remove commas
local function clean(rarity)
if not rarity or type(rarity) ~= "string" then return nil end
rarity = mw.text.trim(rarity)
if rarity:sub(1, 1) == "~" then
rarity = rarity:sub(2)
end
rarity = rarity:gsub(",", "") -- strip commas
return rarity
end
-- RaritySort (raw integer value)
function p.getSortValue(frame)
local raw = frame.args[1]
local rarity = clean(raw)
if not rarity then return 999999999 end
local lowered = rarity:lower()
if lowered == "1" or lowered == "always" then
return 1
elseif lowered == "rare" then
return 999999998
elseif lowered == "never" then
return 0
end
local num = lowered:match("^1/(%d+%.?%d*)$")
if num then
local n = tonumber(num)
if n then
return math.floor(n)
end
end
return 999999999
end
-- Used for bg-class logic
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 (add ~ and formatted commas back)
function p.normalizeDisplay(frame)
local original = frame.args[1]
if not original or type(original) ~= "string" then return "Unknown" end
local trimmed = mw.text.trim(original)
local hasTilde = trimmed:sub(1, 1) == "~"
local cleaned = hasTilde and trimmed:sub(2) or trimmed
if cleaned == "1" then return "Always" end
-- Format 1/#### with commas
local prefix, num = cleaned:match("^(1/)(%d+%.?%d*)$")
if prefix and num then
local formatted = mw.language.getContentLanguage():formatNum(tonumber(num))
return (hasTilde and "~" or "") .. prefix .. formatted
end
return hasTilde and ("~" .. cleaned) or cleaned
end
return p