July 25, 2020

👭 Knight Challenge #11 👬

Want to try your hand at these challenges? There's a couple of things you can do!
From writing, to research, to images, find your preferred way to contribute with our eleventh theme: Couples!

Latest Announcements

Module:UtilsRegion

From Zelda Wiki, the Zelda encyclopedia
Jump to: navigation, search

Source of truth for Zelda-related regions. See Module:UtilsRegion/Data.

Contents

region

region(code)

Returns
  • The name of the region for the given code, or nil if none found.
Examples
InputOutputResultStatus
region("BR")
"Brazil"
Brazil
Green check.svg
region("NOA")
"North America"
North America
Green check.svg
region("foo")
nil
Green check.svg

flag

flag(code, [size])

Parameters
Returns
  • Image thumbnail of the flag associated to the given region code, or nil if none found.
Examples
InputOutputResultStatus
flag("CA")
'<span title="Canada" class="explain">[[File:Canada Flag.png|20px|Canada]]</span>'
Canada
Green check.svg
flag("EUR", "60px")
"[[File:European Union Flag.png|60px|Europe]]"
Europe
Green check.svg
flag("foo")
nil
Green check.svg

local p = {}

local utilsLayout = require("Module:UtilsLayout")
local utilsMarkup = require("Module:UtilsMarkup")
local utilsTable = require("Module:UtilsTable")

local data = require("Module:UtilsRegion/Data")

function p.region(code)
	local region = data[code]
	if region and region.name then
		region = region.name
	end
	return region
end

function p.flag(code, size)
	local imgSize = size or "20px"
	
	local region = data[code]
	if not region then
		return nil
	end
	
	local file = region.flag or ("File:%s Flag.png"):format(region)
	local caption = region.name or region
	local img = utilsMarkup.file(file, {
		size = imgSize,
		caption = caption
	})
	if size then
		return img
	else
		return utilsMarkup.tooltip(img, caption)
	end
end

function p.enum()
	local enum = utilsTable.keys(data)
	enum.reference = "[[Module:UtilsRegion/Data]]"
	return enum
end

function p.Data()
	local rows = {}
	for code in pairs(data) do
		table.insert(rows, {
			code,
			p.region(code),
			p.flag(code)
		})
	end
	rows = utilsTable.sortBy(rows, 1)
	
	return utilsLayout.table({
		sortable = true,
		headers = {"Code", "Region", "Flag"},
		rows = rows
	})
end

p.Schemas = {
	Data = {
		type = "map",
		required = true,
		keys = { type = "string" },
		values = {
			oneOf = {
				region = {
					type = "record",
					properties = {
						{
							name = "name",
							type = "string",
							required = true,
							desc = "Name of the region.",
						},
						{
							name = "flag",
							type = "string",
							required = true,
							desc = "Filename for the region's flag.",
						},
					},
				},
				country = {
					type = "string",
					desc = "The name of the country as a string. Must also be the prefix of its [[:Category:Flags|flag]]'s filename.",
				},
			},
		}
	},
	flag = {
		size = {
			type = "string",
			default = "20px",
		},
	}
}

p.Documentation = {
	region = {
		params = {"code"},
		returns = "The name of the region for the given code, or nil if none found.",
		cases = {
			{
				args = {"BR"},
				expect = "Brazil",
			},
			{
				args = {"NOA"},
				expect = "North America",
			},
			{
				args = {"foo"},
				expect = nil
			},
		},
	},
	flag = {
		params = {"code", "size"},
		returns = "Image thumbnail of the flag associated to the given region code, or nil if none found.",
		cases = {
			{
				args = {"CA"},
				expect = '<span title="Canada" class="explain">[[File:Canada Flag.png|20px|Canada]]</span>',
			},
			{
				args = {"EUR", "60px"},
				expect = "[[File:European Union Flag.png|60px|Europe]]",
			},
			{
				args = {"foo"},
				expect = nil
			},
		}
	},
}

return p