Module:Country

From The Satanic Wiki
Revision as of 02:23, 30 April 2021 by WikiGOD (talk | contribs) (1 revision imported)
Jump to navigation Jump to search

Documentation for this module may be created at Module:Country/doc

--[[<pre> https://github.com/lukes/ISO-3166-Countries-with-Regional-Codes
Description : Gets data about a country, see format in Module:Country/data
Syntax : 
{{#invoke:country|main|country-isocode|[name":"New Zealand/alpha-2/alpha-3/country-code
/sub-region-code/region-code/iso_3166-2/region/sub-region}} 
{{#invoke:country|countrycode|china}} 
* country-isocode (e.g. fr =="France")

]]--

local p = {}
local countryData = mw.loadData("Dev:Country/data")
  
function p.main(frame)
	local args = frame.args

	if args then
		local countryCode = args[1] or args.code
		local info = args[2] or args.info

		if countryCode and info then
			return getCountryData(countryCode, info)
		end
	end
end

function p.countrycode(frame)
	local args = frame.args

	if args then
		local countryCode = args[1] or args.name

		if countryCode then
			return getCountryCode(countryCode, args.match)
		end
	end
end

function getCountryData(code, info)
	if info and code then
		-- if the code doesn't exist try looking for a country name
		if not countryData[code]  then
			code = getCountryCode(code)
		end
		return (countryData[code] or {})[info]
	end
end

function getCountryCode(name, bCloseMatch)
	name = name:lower()
	
	for sCode, sData in pairs(countryData) do
		if sData.name:lower() == name or bCloseMatch and sData.name:match(name) then
			return sCode:lower()
		end
	end
end

return p