Jump to content

Module:Storm categories/sandbox: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
add single ID normalizer
bad find and replace
Line 41: Line 41:
function p._color(category, nullIfMissing)
function p._color(category, nullIfMissing)
-- This looks confusing, but it's actually nested ternaries (for nil checks)
-- This looks confusing, but it's actually nested ternaries (for nil checks)
local color = p.normalizeId(category, defaultCategory)
local color = p._normalizeId(category, defaultCategory)
return colors[color] or ((cats[color] or (
return colors[color] or ((cats[color] or (
Line 52: Line 52:
function p._name(category, basin, nullIfMissing)
function p._name(category, basin, nullIfMissing)
local name_def = (cats[
local name_def = (cats[
p.normalizeId(category, defaultCategory)
p._normalizeId(category, defaultCategory)
] or cats[defaultCategory]).name
] or cats[defaultCategory]).name
return type(name_def) == "table" and
return type(name_def) == "table" and
Line 66: Line 66:
-- This looks confusing, but it's actually nested ternaries (for nil checks)
-- This looks confusing, but it's actually nested ternaries (for nil checks)
return (cats[
return (cats[
p.normalizeId(category, defaultCategory)
p._normalizeId(category, defaultCategory)
] or (nullIfMissing and { sortkey = nil } or cats[defaultCategory])).sortkey
] or (nullIfMissing and { sortkey = nil } or cats[defaultCategory])).sortkey
end
end
Line 72: Line 72:
function p._icon(category, nullIfMissing)
function p._icon(category, nullIfMissing)
-- This looks confusing, but it's actually nested ternaries (for nil checks)
-- This looks confusing, but it's actually nested ternaries (for nil checks)
local icon = p.normalizeId(category, defaultCategory)
local icon = p._normalizeId(category, defaultCategory)
return icons[icon] or (cats[icon] ~= nil and (
return icons[icon] or (cats[icon] ~= nil and (
Line 88: Line 88:
-- `false` will be returned if at least one of the categories are nil or
-- `false` will be returned if at least one of the categories are nil or
-- if the category is not defined.
-- if the category is not defined.
_category1 = p.normalizeId(category1)
_category1 = p._normalizeId(category1)
_category2 = p.normalizeId(category2)
_category2 = p._normalizeId(category2)
-- `_category1 ~= nil` checks if both categories are undefined (since
-- `_category1 ~= nil` checks if both categories are undefined (since
-- it is assumed that both table accesses return the same value)
-- it is assumed that both table accesses return the same value)

Revision as of 08:02, 23 April 2023

local categoryData = require("Module:Storm categories/categories/sandbox")
local colors = require("Module:Storm categories/colors/sandbox").colors
local icons = require("Module:Storm categories/icons/sandbox").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.isEqual(frame)
	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, nullIfMissing)
	-- This looks confusing, but it's actually nested ternaries (for nil checks)
	local color = p._normalizeId(category, 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[
		p._normalizeId(category, defaultCategory)
	] or cats[defaultCategory]).name
	return type(name_def) == "table" and 
		(
			name_def[string.lower(basin or "default")]
			or name_def["default"]
			or (not nullIfMissing and error("No default name for basin-based category name.") or nil)
		) 
		or name_def
end

function p._sortkey(category, nullIfMissing)
	-- This looks confusing, but it's actually nested ternaries (for nil checks)
	return (cats[
		p._normalizeId(category, defaultCategory)
	] or (nullIfMissing and { sortkey = nil } or cats[defaultCategory])).sortkey
end

function p._icon(category, nullIfMissing)
	-- This looks confusing, but it's actually nested ternaries (for nil checks)
	local icon = p._normalizeId(category, defaultCategory)
		
	return icons[icon] or (cats[icon] ~= nil and (
		cats[icon].icon or cats["tropicalcyclone"].icon
	) or (not nullIfMissing 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.
	_category1 = p._normalizeId(category1)
	_category2 = p._normalizeId(category2)
	-- `_category1 ~= nil` checks if both categories are undefined (since
	-- it is assumed that both table accesses return the same value)
	return cats[ _category1 ] == cats[ _category2 ] and _category1 ~= nil
end

function p.demo(frame)
	return require("Module:Storm categories/demo/sandbox").demo(frame)
end

return p