Module:Storm categories: Difference between revisions

Content deleted Content added
per edit request on talk page - fixes
per edit request on talk page - implement three significant changes per Module talk:Storm categories#Template-protected edit request on 23 April 2022
 
Line 26:
end
 
function p._colorisEqual(colorCode, nullIfMissingframe)
return p._isEqual(
frame.args[1] or frame:getParent().args[1],
frame.args[2] or frame:getParent().args[2]
) and "yes" or ""
end
 
function p._normalizeId(category, fallback)
-- Normalize if normalizable, fall back to default if not.
return (category ~= nil and string.len(category) ~= 0) and
string.gsub(string.lower(category), "[^%w]", "") or fallback
end
 
function p._color(category, nilIfMissing)
-- This looks confusing, but it's actually nested ternaries (for nil checks)
local color = (colorCode ~= nil and stringp.len_normalizeId(colorCode)category, ~= 0defaultCategory) and
string.gsub(string.lower(colorCode), "[^%w]", "")
or defaultCategory
return colors[color] or ((cats[color] or (
nilIfMissing
nullIfMissing
and { color = nil }
or cats[defaultCategory]
Line 39 ⟶ 50:
end
 
function p._name(category, basin, nullIfMissingnilIfMissing)
local name_def = (cats[
orp._normalizeId(category, defaultCategory)
(category ~= nil and string.len(category) ~= 0) and
] or (nullIfMissingnilIfMissing and { sortkeyname = nil } or cats[defaultCategory])).sortkeyname
string.gsub(string.lower(category), "[^%w]", "")
or defaultCategory
] or cats[defaultCategory]).name
return type(name_def) == "table" and
(
name_def[string.lower(basin or "default")]
or name_def["default"]
or (not nullIfMissingnilIfMissing and error("No default name for basin-based category name.") or nil)
)
or name_def
end
 
function p._sortkey(category, nullIfMissingnilIfMissing)
-- This looks confusing, but it's actually nested ternaries (for nil checks)
return (cats[
orp._normalizeId(category, defaultCategory)
(category ~= nil and string.len(category) ~= 0) and
] or (nilIfMissing and { sortkey = nil } or cats[defaultCategory])).sortkey
string.gsub(string.lower(category), "[^%w]", "")
or defaultCategory
] or (nullIfMissing and { sortkey = nil } or cats[defaultCategory])).sortkey
end
 
function p._icon(iconCodecategory, nullIfMissingnilIfMissing)
-- This looks confusing, but it's actually nested ternaries (for nil checks)
local icon = (iconCode ~= nil and stringp.len_normalizeId(iconCode)category, ~= 0defaultCategory) and
string.gsub(string.lower(iconCode), "[^%w]", "")
or defaultCategory
return icons[icon] or (cats[icon] ~= nil and (
cats[icon].icon or cats["tropicalcyclone"].icon
) or (not nullIfMissingnilIfMissing and cats[defaultCategory].icon) or nil)
end
 
function p._isEqual(category1, category2)
-- Checks if two IDs are equal.
-- An {{#ifeq}} check does not consider aliases. This function compares two
-- IDs in a way that considers category aliases. This works because alias
-- assignments are references to the actual table containing the category
-- info found in the main category table.
--
-- `false` will be returned if at least one of the categories are nil or
-- if the category is not defined.
local _category1 = p._normalizeId(category1)
local _category2 = p._normalizeId(category2)
return (_category1 == _category2 and _category1 ~= nil) or
cats[ _category1 ] == cats[ _category2 ] and
-- `false` if categories are undefined and not equal.
cats[ _category1 ] ~= nil
end