Module:UtilsRegion

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 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 = ' ', },			{				args = {"EUR", "60px"}, expect = "", },			{				args = {"foo"}, expect = nil },		}	}, }

return p