Module:RarityHandler: Difference between revisions
Jump to navigation
Jump to search
No edit summary Tag: Reverted |
No edit summary |
||
(One intermediate revision by the same user not shown) | |||
Line 1: | Line 1: | ||
local p = {} | |||
-- 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.containsSlash(frame) | |||
local val = frame.args[1] or "" | |||
if val:find("/") then | |||
return "true" | |||
else | |||
return "false" | |||
end | |||
end | |||
function p.getSortValue(frame) | function p.getSortValue(frame) | ||
local raw = frame.args[1] | local raw = frame.args[1] | ||
if not raw or type(raw) ~= "string" then | if not raw or type(raw) ~= "string" then return 999999999 end | ||
local rarity = clean(raw) | local rarity = clean(raw) | ||
Line 18: | Line 36: | ||
end | end | ||
-- Match 1/X, return raw number (not string) | |||
local numStr = lowered:match("^1/(%d+%.?%d*)$") | local numStr = lowered:match("^1/(%d+%.?%d*)$") | ||
if numStr then | if numStr then | ||
local | local num = tonumber(numStr) | ||
if | if num then return math.floor(num) end | ||
end | end | ||
return 999999999 | return 999999999 | ||
end | end | ||
-- 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) | |||
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 | |||
local prefix, num = cleaned:match("^(1/)(%d+%.?%d*)$") | |||
if prefix and num then | |||
local withCommas = mw.language.getContentLanguage():formatNum(tonumber(num)) | |||
return (hasTilde and "~" or "") .. prefix .. withCommas | |||
end | |||
return hasTilde and "~" .. cleaned or cleaned | |||
end | |||
return p |
Latest revision as of 12:29, 26 June 2025
Documentation for this module may be created at Module:RarityHandler/doc
local p = {}
-- 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.containsSlash(frame)
local val = frame.args[1] or ""
if val:find("/") then
return "true"
else
return "false"
end
end
function p.getSortValue(frame)
local raw = frame.args[1]
if not raw or type(raw) ~= "string" then return 999999999 end
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
-- Match 1/X, return raw number (not string)
local numStr = lowered:match("^1/(%d+%.?%d*)$")
if numStr then
local num = tonumber(numStr)
if num then return math.floor(num) end
end
return 999999999
end
-- 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)
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
local prefix, num = cleaned:match("^(1/)(%d+%.?%d*)$")
if prefix and num then
local withCommas = mw.language.getContentLanguage():formatNum(tonumber(num))
return (hasTilde and "~" or "") .. prefix .. withCommas
end
return hasTilde and "~" .. cleaned or cleaned
end
return p