Module:RaritySort: Difference between revisions

From HighSpell Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
local p = {}
local p = {}


function p.getSortValue(frame)
-- Utility to clean up value for processing
     local rarity = frame.args[1]
local function clean(rarity)
 
     if not rarity or type(rarity) ~= "string" then return nil end
     if rarity == nil or type(rarity) ~= "string" then
    rarity = mw.text.trim(rarity)
         return 99
     if rarity:sub(1, 1) == "~" then
         rarity = rarity:sub(2)
     end
     end
    return rarity
end


    -- Remove leading ~ if present
-- Get numeric sort order
--    rarity = mw.text.trim(rarity)
function p.getSortValue(frame)
--    if rarity:sub(1, 1) == "~" then
    local rarity = clean(frame.args[1])
--        rarity = rarity:sub(2)
    if not rarity then return 99 end
--    end


     local lowered = string.lower(rarity)
     local lowered = string.lower(rarity)


    -- Normalize aliases
     if lowered == "1" or lowered == "always" then
     if lowered == "1" then
        lowered = "always"
    end
 
    -- Final sort logic
    if lowered == "always" then
         return 1
         return 1
     elseif lowered:match("^1/%d+$") then
     elseif lowered:match("^1/%d+$") then
Line 33: Line 29:
         return 99
         return 99
     end
     end
end
-- Display value — keep ~ except when it's "1"
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 raw = trimmed:gsub("^~", "")  -- remove ~ only for checking
    if raw == "1" then
        return "Always"
    end
    return trimmed  -- includes ~ if present
end
end


return p
return p

Revision as of 17:55, 15 May 2025

Documentation for this module may be created at Module:RaritySort/doc

local p = {}

-- Utility to clean up value for processing
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
    return rarity
end

-- Get numeric sort order
function p.getSortValue(frame)
    local rarity = clean(frame.args[1])
    if not rarity then return 99 end

    local lowered = string.lower(rarity)

    if lowered == "1" or lowered == "always" then
        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

-- Display value — keep ~ except when it's "1"
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 raw = trimmed:gsub("^~", "")  -- remove ~ only for checking
    if raw == "1" then
        return "Always"
    end

    return trimmed  -- includes ~ if present
end

return p