Jump to content

Module:Storm categories: Difference between revisions

From Wikipedia, the free encyclopedia
moving to submodules
m Anthony Appleyard moved page Module:Tropical cyclone categories to Module:Storm categories without leaving a redirect: Requested by Chlod at WP:RM/TR: Module's scope has now expanded to include other non-tropical storms (such as tornadoes). As such, the name "Tropical cyclone categories" is unfitting. This module includes many subpages (some are submodules, others are documentation pages) which require the page mover right to move (or at least, it'...
(No difference)

Revision as of 00:33, 3 February 2022

local categoryData = require("Module:Tropical cyclone categories/categories")
local colors = require("Module:Tropical cyclone categories/colors").colors
local icons = require("Module:Tropical cyclone categories/icons").icons
local cats = categoryData.cats
local defaultCategory = categoryData.defaultCategory
local p = {}

function p.color(frame)
	return p._color(frame.args[1] or frame:getParent().args[1], false)
end

function p.name(frame)
	return p._name(
		frame.args[1] or frame:getParent().args[1],
		frame.args[2] or frame:getParent().args[2],
		false
	)
end

function p.sortkey(frame)
	return p._sortkey(frame.args[1] or frame:getParent().args[1], false)
end

function p.icon(frame)
	return p._icon(frame.args[1] or frame:getParent().args[1], false)
end

function p._color(colorCode, nullIfMissing)
	-- This looks confusing, but it's actually nested ternaries (for nil checks)
	local color = (colorCode ~= nil and string.len(colorCode) ~= 0) and 
		string.gsub(string.lower(colorCode), "[^%w]", "")
		or defaultCategory
		
	return colors[color] or ((cats[color] or (
		nullIfMissing
		and { color = nil }
		or cats[defaultCategory]
	)).color)
end

function p._name(category, basin, nullIfMissing)
	local name_def = (cats[
		(category ~= nil and string.len(category) ~= 0) and 
			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 (nullIfMissing and nil or error("No default name for basin-based category name."))
		) 
		or name_def
end

function p._sortkey(category, nullIfMissing)
	-- This looks confusing, but it's actually nested ternaries (for nil checks)
	return (cats[
		(category ~= nil and string.len(category) ~= 0) and 
			string.gsub(string.lower(category), "[^%w]", "")
			or defaultCategory
	] or (nullIfMissing and { sortkey = nil } or cats[defaultCategory])).sortkey
end

function p._icon(iconCode, nullIfMissing)
	-- This looks confusing, but it's actually nested ternaries (for nil checks)
	local icon = (iconCode ~= nil and string.len(iconCode) ~= 0) 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 (nullIfMissing and nil or cats[defaultCategory].icon))
end

function p.demo(frame)
	return require("Module:Tropical cyclone categories/demo").demo(frame)
end

return p