Module:RarityHandler: Difference between revisions
No edit summary |
Tag: Undo |
||
(2 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
local p = {} | local p = {} | ||
-- Clean: | -- Clean up the rarity string: strip ~, commas, trim | ||
local function clean(rarity) | local function clean(rarity) | ||
if not rarity or type(rarity) ~= "string" then return nil end | if not rarity or type(rarity) ~= "string" then return nil end | ||
rarity = mw.text.trim(rarity) | rarity = mw.text.trim(rarity) | ||
rarity = rarity:gsub("^~", "") -- remove ~ | |||
rarity = rarity:gsub(",", "") -- remove commas | |||
rarity = rarity:gsub(",", "") -- | |||
return rarity | return rarity | ||
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 return 999999999 end | |||
local rarity = clean(raw) | local rarity = clean(raw) | ||
if not rarity then return 999999999 end | if not rarity then return 999999999 end | ||
Line 28: | Line 27: | ||
end | end | ||
local | -- Match 1/X, return raw number (not string) | ||
if | local numStr = lowered:match("^1/(%d+%.?%d*)$") | ||
local | if numStr then | ||
if | local num = tonumber(numStr) | ||
if num then return math.floor(num) end | |||
end | end | ||
Line 39: | Line 37: | ||
end | end | ||
-- | -- For logic styling | ||
function p.getCleanValue(frame) | function p.getCleanValue(frame) | ||
local rarity = clean(frame.args[1]) | local rarity = clean(frame.args[1]) | ||
Line 47: | Line 45: | ||
end | end | ||
-- For display | -- For display | ||
function p.normalizeDisplay(frame) | function p.normalizeDisplay(frame) | ||
local original = frame.args[1] | local original = frame.args[1] | ||
if not original or type(original) ~= "string" then return "Unknown" end | if not original or type(original) ~= "string" then return "Unknown" end | ||
local trimmed = mw.text.trim(original) | local trimmed = mw.text.trim(original) | ||
local hasTilde = trimmed:sub(1, 1) == "~" | local hasTilde = trimmed:sub(1, 1) == "~" | ||
Line 58: | Line 55: | ||
if cleaned == "1" then return "Always" end | if cleaned == "1" then return "Always" end | ||
local prefix, num = cleaned:match("^(1/)(%d+%.?%d*)$") | local prefix, num = cleaned:match("^(1/)(%d+%.?%d*)$") | ||
if prefix and num then | if prefix and num then | ||
local | local withCommas = mw.language.getContentLanguage():formatNum(tonumber(num)) | ||
return (hasTilde and "~" or "") .. prefix .. | return (hasTilde and "~" or "") .. prefix .. withCommas | ||
end | end | ||
return hasTilde and | return hasTilde and "~" .. cleaned or cleaned | ||
end | end | ||
return p | return p |